2017-04-03 21 views
-1

我不知道如何通過用戶輸入...請幫助!?!?這是用java編寫的。我的教授提供了這個我不做任何改變。如何獲取用戶輸入並將其傳遞給java中的構造函數?它說artCourse是undefined

/** 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

您需要先創建課程的子類。 – Sedrick

回答

2

Course類是abstract,以便您不能創建一個對象。

如果不應對類文件進行任何更改,則答案爲NO,因爲您無法實例化Course類。

但是其他的方式來調用Course類的構造函數是通過繼承,如下圖所示:

public class Course { 
//add your code 
} 

public class Maths extends Course { 

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

您可以使用Maths maths = new Maths(theSubject, courseNumber);while循環內的對象。

但是,在您的while循環字符串比較不正確時,應將字符串與.equals()進行比較,即與while(theSubject.equals("DONE"))一樣。

您可以參考下面的代碼:

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); 
} 

此外,請記住scanner.nextInt()回報int(原始),爲此你不需要收集到Integer即,它要求不必要的裝箱操作,所以你可寫爲int courseNumber = in.nextInt();

+1

不要將它命名爲「課程」! – Li357

+0

該分配明確指出他無法更改給定的文件。 – csmckelvey

4

您可以在主要方法中創建一個新課程。在你的情況下,課程是抽象的,這意味着它不能被實例化,但它可以被分類。你可以創建一個課程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

相關問題