2012-05-20 2 views
0

코드 지정을 위해이 과제가 있지만 불행히도 어려움을 겪고 있습니다.Java 캡슐화 Array 뮤 테이타 메서드

인터넷과 교과서를 샅샅이 뒤졌지만이 특정한 곤경의 예를 찾을 수없는 것 같습니다.

기본적으로 열차의 예약 엔진을 작성해야합니다. 우리는 사용할 시작 코드를 제공 받았고 기본적으로 메서드를 작성하고 적절한 클래스에 연결합니다.

주요 문제는 trainticket 객체를 보유하고있는 주 배열을 별도의 클래스로 캡슐화해야하며 기본적으로 mutator 및 접근 자 메서드를 작성해야합니다. 배열과의 모든 상호 작용이 필요하므로 배열을 액세스 할 수 없게 유지하고 시간 확보 액세스가 필요없는 경우.

내가 여러 세부 사항은 사용자를 형성 요청하는 코드를 작성하고 생성자 호출을 사용하여 객체를 instatiating 문제를 ahve 없다, 그래서 기본적으로 프로그램

Private static void menuAdd() 


{ 
     String passName,op1,op2; 
     int seatNum; 
     Boolean FCOption,waiter,indicator; 
     int duration; 
     char fClass,wService; 

    System.out.print("Please Enter a seat number :"); 
    seatNum = stdin.nextInt(); 
    stdin.nextLine(); 

    System.out.print("Please Enter the passenger name :"); 
    passName = stdin.nextLine(); 
    System.out.print(passName); 

    System.out.print("Please Enter number of legs for this trip :"); 
    duration = stdin.nextInt(); 

    System.out.println("Would you like to consider a First Class ticket for an additional $20 per leg? :"); 
    System.out.print("Please enter Y/N"); 
    op1 = stdin.next(); 
    fClass =op1.charAt(0); 

    stdin.nextLine(); 
    System.out.print("Would you like to consider a waiter service for a flat $15 Fee?"); 
    System.out.print("Please enter Y/N"); 
    op2 = stdin.next(); 
    wService =op2.charAt(0); 


    //Now we create the ticket object 

    TrainTicket ticketx = new TrainTicket(seatNum,passName,duration); 

    System.out.println("This is an object test printing pax name"+ticketx.getName()); 

    TicketArray.add(ticketx); 

}

의 드라이버 클래스 TrainTicket 개체, 을 위해 내가

TicketArray.add(ticketx); 

일식 말할 사용하여 배열 클래스의 객체를 따라 통과 할 때 내가 "유형 TicketArray에서 비 정적 메소드 추가 (TrainTicket)에 대한 정적 참조를 만들 수 없습니다"S 것은

이것은 작동하지 않는 이유를 배열 클래스는

Public class TicketArray 
{ 
    // .............................................. 
    // .. instance variables and constants go here .. 
    // .............................................. 
    int counter ; 
    int arraySize =100 ; 

    // constructor 
    public TicketArray() 
    { 
     // .................... 
     // .. implement this .. 
     // .................... 
     TrainTicket [] tickets =new TrainTicket[arraySize]; 
    } 

    // add() method: 
    // take the passed in TrainTicket object and attempt to store it in the 
    // data structure. If the structure is full, or the seat of the given 
    // TrainTicket has already been booked, the operation should return 
    // false; otherwise return true. 

    public boolean add(TrainTicket data) 
    { 
     // .................... 
     // .. implement this .. 
     // .................... 

     tickets[counter]=data; 
     // dummy return value so the skeleton compiles 
     return false; 
    } 

어떤 아이디어 모습입니다 ? 만약 누군가가 이런 식으로 배열을 캡슐화하는 방법을 설명 할 수 있다면 고맙겠, 나는 생성자의 작동 방식에 익숙하고 그들의 메소드가 작성되었지만, 어떤 이유로 나는 정렬.

미리 감사드립니다.

+0

당신은 인스턴스 메소드를 호출하기 위해 인스턴스가 필요합니다. 이 문제는 배열과 관련이 없습니다. –

+0

안녕하세요, Dave, 조금 더 명확하게 말씀해 주시겠습니까? 무엇의 인스턴스? – user1405824

+0

TicketArray, 인스턴스 메소드를 호출하려고하는 클래스. 또는 정적 방법으로 만들 수도 있지만 IMO. –

답변

2

여기서 mutator 또는 접근 자 메서드 나 배열과 관련된 문제는 아니지만 실제로 사용하려고 시도하기 전에 TicketArray 클래스의 인스턴스를 만들지 않아도됩니다. add(Ticket t)은 추가하기 전에 인스턴스가 TicketArray이어야 함을 의미하는 인스턴스 메서드로 정의됩니다.

이 시도 :

//create a new Ticket 
TrainTicket ticketx = new TrainTicket(seatNum,passName,duration); 

//create a new Ticket Array 
TicketArray tarr = new TicketArray(); 
tarr.add(ticketx); 
+0

ok, TicketArray를 인스턴스화 한 다음 tarr.add (ticketx)를 사용하여 티켓 객체를 전달하십시오. , 어떻게 내가 추가 할 수있는 개체를 배열에 추가 할 부울 추가 (TrainTicket 데이터)? – user1405824

+1

@ user1405824 글쎄 그것은 당신이 가진 것과 비슷할 것이다. 배열에 현재 색인을 추적해야합니다. 배열에 요소를 추가하면 색인이 1 씩 증가합니다. –