Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a Part 1 subpackage p1 under the hw1 package. In the p1 subpackage, create a public class named Course, and add the following private
Create a Part 1 subpackage p1 under the hw1 package. In the p1 subpackage, create a public class named Course, and add the following private data fields to the Course class: - name, which is of type String and represents the name of a course. We assume here for simplicity that every course has a unique name that identifies it. - capacity, which is an integer representing the maximum number of students that can enroll in the course. - roster, which is an Array List of strings each representing the name of a student enrolled in the course. Assume here that every student has a distinct name so that a student's name identifies the student. Add the following public methods to the Course class: - A constructor public Course (String name, int capacity) that initializes the name and capacity fields with the values of the arguments, and initializes the roster field to an empty list. Assume here that capacity >0 is satisfied. - A getter for the name field. - Amethod public boolean add (String student) that adds the student in the argument to the roster and returns true if the capacity has not been reached. Otherwise, the method should print an error 1 message "_is full" without adding the student to the roster, where the underscore should be replaced with the name of the course, and return false. - Amethod public void remove (String student) that removes from the roster the student in the argument. You can assume that the student in the argument is a student on the roster. - A tostring! ) method that returns a string, in the default format of Intelli,, consisting of the values of all data fields. In the p1 subpackage, create a public class named CourseTest. In the main method of the AccountTest class: - Create a Course object named itec2150, using "ITEC 2150 as the course name and 3 as the capacity. Print the itec2150 object to the console. Your code should print Course { nane = 'ITEC 2150, capacity =3, roster =[]} - Add "Alice", "Bob", "Carol" and "Daniel" to the itec2150 roster and print the returned results of the add method. Your code should print true true true ITEC 2150 is full false - Print the itec2150 object to the console. Your code should print Course { nane = ITEC 2150, capacity =3, roster =[ Alice, Bob, Carol ]} - Remove "Bob" from the itec2150 roster and again add "Daniel" to the roster. Print the itec2150 object to the console. Your code should print Course { nane = 'ITEC 2150, capacity =3, roster=[Alice, Carol, Daniel] } 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 stotic 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 subcloss of the Grizzly class, and add the following privote 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 Create 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. - Amethod 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 {1di: _ nane: __ 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 Coursefnane: __ 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, nane: Alice, courses: []\} Student\{1d: 2, nane: Bob, courses: []\} Student\{1d: 3 , nane: Carol, courses: []\} student {1d:4, nane: 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\{nane: ITEC 4320, capacity: 3, roster: []\} 5 Course\{nane: ITEC 4706, capacity: 3, roster: []\} Course\{nane: ITEC 4866 , capacity: 3, roster: []\} - Write code to simulate the following sequence of events: - Alices adds ITEC 4320, ITEC 4700 and ITEC 4700 . The second attempt to add ITEC 4700 should cause an error message "You are already enrolled in ITEC 4700. - Bob adds ITEC 4700 and ITEC 4860. - Carol adds all three courses. - Daniel adds all three courses. The attempt to add ITEC 4700 should cause an error message "ITEC 4700 is full". - Alice drops ITEC 4320 . - Bob drops ITEC 4860. - Carol drops ITEC 4700. - Alice adds ITEC 4860. - Bob adds ITEC 4320. - Daniel adds ITEC 4700. - Print the four Student objects. Your code should print Student \{id: 1, name: Alice, courses: [ITEC 4700, ITEC 4860]\} Student (id: 2, name: Bob, courses: [ITEC 4700, ITEC 4320]) Student (id: 3, name: Carol, courses: [ITEC 4320, ITEC 4860)] Student \{id: 4, name: Daniel, courses: [ITEC 4320, ITEC 4860, ITEC 4700]) - Print the three Course objects. Your code should print Course(name: ITEC 4320, capacity: 3, roster: [Carol, Daniel, Bob)\} Course(name: ITEC 4700, capacity: 3, roster: [Alice, Bob, Daniel]\} Course(name: ITEC 4860, capacity: 3, roster: [Carol, Daniel, Alice]) Now you will (1) create an Instructor class as a subcless 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 privote 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 p 3 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 Instructorfid: __ 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\{nane: _, instructor: _, capacity: __ roster: }} 7 where the underscore after "instructor: " should be replaced with the name of the instructor field if it is not null. Otherwise fi.e., if the instructor has not been assigned), then use "TBO" 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 fid: 6, name: Richard, courses: [ITEC 4860]\} - Print the three Course objects. Your cade 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])
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started