1. Create a file \"Course.java\" and a file \"University.java\". The University.
ID: 3753939 • Letter: 1
Question
1. Create a file "Course.java" and a file "University.java". The University.java file will be your driver class, so put a University class in it and add a "main" method in that class. 2. In the Course.java file, write the code for a class Course, that contains 3 instance variables: ID (e.g., "CS1073"), schedule (e.g., "MWF12:30PM"), and instructor (e.g., "J. Tasse"). All 3 variables should be of type String. Add a constructor in your Course class, that takes 3 parameters, and that initializes the3 instance variables with the 3 values passed in the parameters. 3. In the driver class (main method), create a Course object with the information above for CS1073 4. Add an accessor method for the schedule variable (i.e., a getSchedule method) in your Course class. 5. In the main method, call the getSchedule method on the object you created in Step 4. Save the schedule returned in a variable and print this variable on the screen. Run your program to see if it works as expected. 6. 7. Add a mutator method for the schedule variable (i.e., a setSchedule method) in your Course class. In the main method, make the proper call to change the schedule of your object (representing CS1073) to "MWF3:30PM". Note: before you panic, this change is not being made for real! 8. 9. Add a "toString" method to your Course class. Make it build a string like "CS1073 at MWF12:30PM taught by J. Tasse" 10. In the main method, print the following on the screen: I am registered to the course CS1073 at MWF3:30PM taught by J. Tasse this semester. In order to do so, only use concatenation (of some string and of your object). As a reminder, trying to print your object will automatically call the toString method. Run your program againExplanation / Answer
Course class:
//Course class-START
public class Course {
//instance variables
String ID,schedule,instructor;
//constructor-parameterized
public Course(String iD, String schedule, String instructor) {
super();
ID = iD;
this.schedule = schedule;
this.instructor = instructor;
}
//accesor method for schedule variable
public String getSchedule() {
return schedule;
}
//mutator method for schedule variable
public void setSchedule(String schedule) {
this.schedule = schedule;
}
//toString() method
@Override
public String toString() {
return ID +" at "+schedule+" taught by "+instructor;
}
}
//Course class-END
University class:
//University class-START
public class University
{
//main method starts
public static void main(String[]arg)
{
//Creating Course object
Course course = new Course("CS1073","MWF12:30PM","J.Tasse");
//calling getSchedule() method on Course object
/*and saving schedule returned value in a variable and printing */
String schedule = course.getSchedule();
System.out.println("Schedule:"+schedule);
//calling setSchedule() method on the Course object
course.setSchedule("MWF3:30PM");
System.out.println("I am registered to course "+course+" this semester.");
}
}//University class-ENDS
Output:
Schedule:MWF12:30PM
I am registered to course CS1073 at MWF3:30PM taught by J.Tasse this semester.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.