-2

I can't figure out how to pass the users input... please help!?!? This is in java. My professor provided this i am to not make any changes to it.

 /** 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());
  }

}

This is what I have so far for the main

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

These are my sub class.

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
    You already have several lines of code where you create a new object and pass a parameter to the constructor. Why do you think this one instance of this task is different? – takendarkk Apr 03 '17 at 21:23
  • 1
    You need to create a subclass of Course first. – SedJ601 Apr 03 '17 at 21:30

2 Answers2

4

You can create a new Course in your main method. In your case, Course is abstract, which means it cannot be instantiated, but it can be subclassed. You could create a class, CourseImpl, that extends Course!

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

And then in your main method you can instantiate this implementation of Course.

 {
 ....
 theSubject = in.nextLine();
 ....
 Integer courseNumber = in.nextInt();
 CourseImpl aCourse = new CourseImpl (theSubject, courseNumber);
 }
Ishnark
  • 661
  • 6
  • 14
2

Your Course class is abstract, for which you can't create an object.

If you shouldn't make any changes to the class file, then the answer is NO, because you can't instantiate the Course class.

But the other way to call the Course class constructor is through inheritance as shown below:

public class Course {
 //add your code
}

public class Maths extends Course {

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

You can create the object using Maths maths = new Maths(theSubject, courseNumber); inside the while loop.

However, in your while loop string comparison is incorrect, strings should be compared with .equals() i.e., like while(theSubject.equals("DONE")).

You can refer the below code:

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

Also, remember that scanner.nextInt() returns int (primitive), for which you don't need to collect into Integer i.e., it requires unnecessary boxing operation, so you can write as int courseNumber = in.nextInt();

Vasu
  • 21,832
  • 11
  • 51
  • 67