2017-04-03 2 views
-1

나는 사용자 입력을 전달하는 방법을 알아 내지 못합니다 ... 도와주세요!?!? 이것은 자바에 있습니다. 교수님이 이걸 제공했는데 변경하지 않을 것입니다.사용자 입력을 받아서 java의 생성자로 전달하는 방법은 무엇입니까? 그것은 artCourse가 정의되지 않았다고 말합니다

/** A generic course at a University 
*/ 
public abstract class Course 
{ 

    // The course subject 
    private String subject; 
    // The course number 
    private int number; 
    // The time the course meets 
    private MeetingTime time; 

    /** Create a new Course object and initialize the course 
    * name, time and difficulty. 
    * @param theSubject the course subject 
    * @param theNumber the course number 
    * @param theTime the course time 
    */ 
public Course (String theSubject, int theNumber,MeetingTime theTime) 
{ 
    subject = theSubject; 
    number = theNumber; 
    // Store a copy of the time object in the course object. 
    time = new MeetingTime (theTime.getStartTime(), theTime.getEndTime()); 
} 


    /** Get the time when the course meets. 
    * @return the time the course meets 
    */ 
    public MeetingTime getTime() 
    { 
    return new MeetingTime (time.getStartTime(), time.getEndTime()); 
    } 

}

이 내가 주

public static void main(String[] args) 
{ 
    Scanner in = new Scanner(System.in); 
    ArrayList<String> courseSchedule =new ArrayList<String>(); 
    String theSubject = " "; 
    Integer theNumber = 000; 
    Double theTime = 0000.0; 
    while (!theSubject.equals("DONE")) 
    { 
     System.out.print("Enter a course subject: "); 
     theSubject = in.nextLine(); 
     System.out.print("Enter course number: "); 
     theNumber = in.nextInt(); 
     System.out.print("Enter course start time and end time: "); 
     theTime = in.nextDouble(); 
     String temp = in.nextLine(); 
     if (theSubject.equals("ART")) 
     { 
     System.out.print("Enter the studio number: "); 
     String theStudioNumber = in.nextLine(); 
     System.out.print("Enter the instructors name: "); 
     String theInstructor = in.nextLine(); 
     ArtCourse artCourse = new ArtCourse (theSubject, theNumber, theTime, theStudioNumber, theInstructor); 
     } 

이 내 하위 클래스입니다 위해 지금까지 가지고있는 것입니다.

public class ArtCourse extends Course 
{ 
    private String studioNumber; 
    private String instructor; 

    public ArtCourse (String theSubject, 
        int theNumber, 
        MeetingTime theTime, 
        String theStudioNumber, 
        String theInstructor) 
    { 
    super(theSubject, theNumber, theTime); 
    studioNumber = "????"; 
    instructor = "????"; 
    } 
} 
+2

새 개체를 만들고 매개 변수를 생성자에 전달하는 코드 줄이 이미 있습니다. 이 작업의이 한 인스턴스가 왜 다른지 생각하십니까? – csmckelvey

+1

먼저 Course의 하위 클래스를 만들어야합니다. – Sedrick

답변

2

귀하의 Course 클래스는 객체를 생성 할 수있는 abstract이다.

Course 클래스를 인스턴스화 할 수 없으므로 클래스 파일을 변경하지 않아야하는 경우 대답은 NO입니다.

public class Course { 
//add your code 
} 

public class Maths extends Course { 

    public Maths(String theSubject, int theNumber) { 
     super(theSubject, theNumber); 
    } 
} 

당신은 while 루프 내에서 Maths maths = new Maths(theSubject, courseNumber);를 사용하여 개체를 만들 수 있습니다 : 아래 그림과 같이

그러나 Course 클래스 생성자를 호출 할 수있는 다른 방법은 상속을 통해입니다.

그러나 while 루프 문자열 비교가 올바르지 않으면 문자열을 .equals()과 비교해야합니다 (예 : while(theSubject.equals("DONE"))). 당신이 즉 Integer으로 수집 할 필요가 없습니다 않은 int (원시) scanner.nextInt() 반환, 당신 때문에, 불필요한 권투 동작을 필요로 기억 또한

while (!theSubject.equals("DONE")) { 
    System.out.print("Enter a course subject: "); 
    theSubject = in.nextLine(); 
    System.out.print("Enter course number: "); 
    int courseNumber = in.nextInt(); 
    Maths maths = new Maths(theSubject, courseNumber); 
} 

:

당신은 아래의 코드를 참조 할 수 있습니다 int courseNumber = in.nextInt();

+1

'코스'라고하지 마십시오! – Li357

+0

할당은 명시된 파일을 변경할 수 없음을 명시 적으로 나타냅니다. – csmckelvey

4

기본 방법으로 새 ​​코스를 만들 수 있습니다. 귀하의 경우 코스는 추상적 인 것으로 인스턴스화 할 수 없지만 서브 클래 싱 할 수 있음을 의미합니다. Course를 확장하는 CourseImpl 클래스를 만들 수 있습니다!

public class CourseImpl extends Course { 
     public CourseImpl(String subject, int courseNumber) { 
      super(subject, courseNumber); 
     } 
} 

그런 다음 주 방법에서이 구현을 인스턴스화 할 수 있습니다.

{ 
.... 
theSubject = in.nextLine(); 
.... 
Integer courseNumber = in.nextInt(); 
CourseImpl aCourse = new CourseImpl (theSubject, courseNumber); 
} 
+0

실제로 그런 추상 클래스를 인스턴스화 할 수 있습니까? – csmckelvey

+0

좋은 지적 사실 그가 실제로 추상적인지를 묻는 질문 만했습니다. – Ishnark

+1

수정 해 주셔서 감사합니다. – csmckelvey

관련 문제