Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COSC2006 Lab 1 – Review of Java Object-Oriented Principles

Coding – Submit all .java files to the LMS.

The Person Class. Create a public class named Person that contains two private data variables

called id (of type int) and name (of type String). Create a constructor for the person class that

receives two parameters, id (type int) and name (type String). In your constructor, assign the

values to the appropriate class variables. Add the id() and name() methods to the class to return the value of the appropriate variables. Also add a setName() method to set the name variable.    Note that the id variable must not be able to be changed. (What modifier is used to make that    happen?). Override Object’s equals method. Two Person objects are considered equal if: (a)

they are both Person objects (or subclasses of Person - Java has a keyword to check this for

you), (b) their ID numbers match, and (c) their names match exactly. All three conditions must     be met: if any one of them fails, the objects are not equal. Finally, add a method such that when

a person object is printed (with System.out.println) it will display something like Person[id=1234, name=John Smythe].

The Student Class. Create a new public class called Student that extends the Person class.

Create a private double variable for the student’s average (this should be initialized to zero) and a constructor that takes in the person’s id and name. (Hint: The only line in this constructor

should be a super call). Implement the appropriate set and get methods. Override the toString method to display something like Student[id=2345, name=Anna Templeton, GPA=78.85%]

The Instructor Class. Create a new public class called Instructor that extends the Person class. Create a private string variable for the instructor ’s department and a constructor that takes in the person’s id, name, and department. (Hint: There should only be two lines of code in this

constructor). Implement the appropriate set and get methods. Override the toString method to display something like Instructor[id=3456, name=Jamie Remstone, department=Computer

Science]

The Course Class. Create a new public class called Course that contains a private final integer

called MAX_ENROLLED_STUDENTS and set it equal to 5. The class should also have a private

array variable to hold the students in the course, an enrollment variable (of type int) to hold

how many students are currently enrolled in the course, a variable of type Instructor to hold the course’s instructor and two string values to hold its course code and the name of the course.

Add a constructor that receives two strings: one for the code and one for the name. Set the

class variables and initialize the array variable. Add an enroll(Student student) method to add a student to the course, making sure to check that the student can be enrolled first. Add the

unenroll(Student student) method to remove the student from the course, checking if (a) there  are students in the course, and (b) the student being removed is actually in the course. Add the  assignGrade(Student student, double grade) method to assign the student a grade in the course and change their average according to the formula (average + grade) / 2. Add any other

appropriate set and get methods, keeping in mind the principle of information hiding, and what we do or don’t want our users to access or change. Finally, add a toString method to print

something like Course[code=COSC1234, name=COSC course 1, instructor=Jamie Remstone, enrolled=2]

Putting it alltogether. Create a test class that:

(a)  Creates 7 students with different id numbers and names. One of these students must have your name on it - the rest can be random. Print each one to the console.

(b) Set the student’s averages to any values you like (range from 0-100). Use a variety of

values and print the students to the console again. If you’d like to use random numbers, you can use this code to get a random number between 0 and 100:

Math.random() * 101. Note: we don’t cast to inthere because average is a double value.

(c)  Creates an instructor with a different id and name to the students. Print it to the console.

(d) Create two courses with any code and names you wish - be creative!    (e) Assign the instructor to both courses. Print the courses to the console.

(f)  Assign 5 of the 7 students to the first course and the other 2 to the second. Print the courses after you are done.

(g) Assign all students grades. Print all the students to the console.

(h)  Unenroll two students (your choice) from the first course. Print the course to the console.