2017-04-03 30 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

相关问题