Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 2 Now you will (1) create a Grizzly class and a Student subclass, (2) modify the Course class, and (3) write a class named

image text in transcribedimage text in transcribedimage text in transcribed

image text in transcribedimage text in transcribed

Part 2 Now you will (1) create a Grizzly class and a Student subclass, (2) modify the Course class, and (3) write a class named Hw1Test to test the Grizzly, Student and Course classes. Create a Part 2 subpackage p2 under the hw1 package and copy here the Course class from subpackage p1. In the p2 subpackage, create a public class named Grizzly, and add the following private data fields to the Grizzly class: - A private static data field named numGrizzlies of type int, representing the number of Grizzly objects that have been created. - A protected data field named id of type int, representing the ID that uniquely identifies a GGC member. - A protected data field named name, which is of type String and represents the name of a GGC member. Create the following methods in the Grizzly class: - A constructor public Grizzly (String name) that - Increments the static numGrizzlies field by 1 and assigns the result to the id instance variable. - Initializes the name field with the value of the argument. - Getters for the id and name fields. - Override the equals method so that it compares only the id field and returns true if both Grizzly objects have the same id. Create a public class named Student as a subclass of the Grizzly class, and add the following private data field to the Student class: - courses, which is an Array List of Course objects each representing a course the student is enrolled in. 3 Ireate the following methods in the Student class: - A constructor public Student (String name) that uses the constructor of the Grizzly superclass to create a Grizzly object with the given name, and initializes the courses field to an empty list. - Getter for the courses field. - A method public void addCourse (Course course) that allows the student to enroll in the course represented by the Course object in the argument. The method should do the following: - Check whether the student is already enrolled in the course - this can be done by checking whether the courses list of the student already contains the name of the course. If so, print an error message "You are already enrolled in __" where the underscore should be replaced with the name of the course. - If the student is not yet enrolled in the course, then invoke the add method of the given Course object to add the student to the course. If the invocation succeeds, then also add the Course object to the courses list of the student. - A method public void dropCourse (Course course) that allows the student to drop the course represented by the Course object in the argument. Assume here that the course to be dropped is indeed a course the student is currently enrolled in. The method should remove the student from the roster of the course and remove the course from the student's course list. - A toString method that returns a string of the format Student { id: __ , name: __ , courses: __ } where the underscores after "id:" and "name: " should be replaced with the values of the respective fields, and that after "courses: " should be replaced with a list consisting only of the names of the courses the student is enrolled in. In the Course class, - Change the type of the roster field to an Array List of Student objects. - Change the header of the add method to public boolean add (Student student) - Change the header of the remove method to public void remove (Student student) - Change the toString( ) method so that it returns a string of the format Course\{name: __, capacity: __, roster: __\} where the underscores after "name: " and "capacity: " should be replaced with the values of the respective fields, and that after "roster: " should be replaced with a list consisting only of the names of the students enrolled in the course. - Override the equals method so that it compares only the name field and returns true if both Course objects have the same name. Create a public class named Hw1Test. In the main( ) method of the class: - Create four Student objects whose names are Alice, Bob, Carol, and Daniel, respectively. Print the four Student objects. Your code should print Student { id: 1, name: Alice, courses: []\} Student {id:2, name: Bob, courses: []\} Student { id: 3, name: Carol, courses: []\} Student { id: 4, name: Daniel, courses: []\} - Create three Course objects whose course names are "ITEC 4320", "ITEC 4700" and "ITEC 4860", respectively. Use 3 as the capacity for all three courses. Print the three Course objects. Your code should print Course\{name: ITEC 4320, capacity: 3, roster: []\} 5 Part 3 Now you will (1) create an Instructor class as a subclass of the Grizzly class and (2) modify the Course and Hw1Test classes. Create a Part 3 subpackage p3 under the hw1 package and copy here the Course, Grizzly, Student and Hw1Test classes from subpackage p2. In the p3 subpackage, create a public class named Instructor as a subclass of the Grizzly class, and add the following private data field to the Instructor class: - courses, which is an Array List of Course objects each representing a course the instructor teaches. Create the following methods in the Instructor class in the p3 subpackage: - A constructor public Instructor (String name) that uses the constructor of the Grizzly superclass to create a Grizzly object with the given name, and initializes the courses field to an empty list. - A toString( ) method that returns a string of the format Instructor { id: __ , name: __ , courses: __} where the underscores after "id: " and "name: " should be replaced with the values of the respective fields, and that after "courses: " should be replaced with a list consisting only of the names of the courses the instructor is teaching. In the Course class in the p3 subpackage: - Add a private data field named instructor which is an object of the Instructor class that represents the instructor of the course. - Add getter and setter for the instructor field. - Change the toString method so that it returns a string of the format Course\{name: __ instructor: __, capacity: __, roster: where the underscore after "instructor: " should be replaced with the name of the instructor field if it is not null. Otherwise (i.e., if the instructor has not been assigned), then use "TBD" for the name. In the Instructor class, add a method public void teachCourse (Course course) that assigns this Instructor object to the instructor field of the Course object in the argument, and adds the given course to the courses field of this Instructor object. Run the main method of the Hw1Test class in the p3 subpackage. You should see "instructor: TBD" in the output of each Course object. Add the following code to the end of the main method of the Hw1Test class in the p3 subpackage: - Create two Instructor objects whose names are Evelyn and Richard, respectively. Print the two Instructor objects. - Have Evelyn teach ITEC 4320 and ITEC 4700, and have Richard teach ITEC 4860. - Print the two Instructor objects. Your code should print Instructor\{id: 5, name: Evelyn, courses: [ITEC 4320, ITEC 4700]\} Instructor\{id: 6, name: Richard, courses: [ITEC 4860]\} - Print the three Course objects. Your code should print Course\{name: ITEC 4320, instructor: Evelyn, capacity: 3, roster: [Carol, Daniel, Bob]\} Course\{name: ITEC 4700, instructor: Evelyn, capacity: 3, roster: [Alice, Bob, Daniel]\} Course\{name: ITEC 4860, instructor: Richard, capacity: 3, roster: [Carol, Daniel, Alice]\} Submission Compress the folder for the hw1 package and submit the .zip file to D2L

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions