Question: JAVA Write a class Person.java that acts as a person with Strings to hold their name and address. This class should also contain: A constructor,
JAVA
Write a class Person.java that acts as a person with Strings to hold their name and address. This class should also contain:
A constructor, Person(String name, String address)
Getters and Setters for name and address, respectively.
String getName()
void setName(String name)
String getAddress()
void setAddress(String address)
In addition, you will also create two subclasses of Person called Student.java and Professor.java.
The Student class should contain:
Unique Instance Variables:
String[] courses. A new students course array should have all entries as none.
char[] grades. A new students grades array should have all entries as A
A student can take at most 6 courses, therefore both courses and grades should be of length 6.
A constructor, Student(String name, String address). You should initialize courses and grades here.
Getters for courses and grades
A unique method, boolean addCourse(String course). This method will add the course to the Student's courses array, so long as:
They are not currently enrolled in the course.
They are not already taking 6 courses, i.e. one or more entries in their courses array is none
New courses should be added to the leftmost available slot in the courses array
This method returns true if they successfully add the course, and false otherwise
The Professor class should contain:
Unique Instance Variables:
double salary. A professors salary is dependent on their rank, which can be either Assistant or Professor. Those with the rank Assistant have a salary of 70,000. Those with the rank of Professor have a salary of 100,000.
String course
String rank. Changes to a professors rank should result in the corresponding salary, regardless of what their salary was previously. If an Assistant has a salary of 95,000 and becomes a Professor, then their new salary is 100,000.
A constructor, Professor(String name, String address, String course, String rank)
Getters and Setters for salary, course and rank
Finally, we will implement a class Course.java to wrap the previous classes together. This will act as a college course, with an associated name, Professor and Students array, as well as a way to keep track of how many students have enrolled in the course. Course.java is not an extension of Person
The Course class should contain:
Unique instance variables:
Professor professor
Student[] students. The students array must be of size 100!
String courseName
int numEnrolled
A constructor, Course(Professor professor, String courseName)
Getters for the above instance variables
A unique method, boolean enroll(Student s). Enrolls the student in the course, given
The course is not full
The student successfully adds the course
This method returns true if they are successfully enrolled, and returns false otherwise
Each course can hold 100 Students and initially has 0 Students enrolled.
Consider the following code block:
String courseName = "MA"; Professor p = new Professor("Dr. Malcom", "MATH", courseName, "Professor"); Course c = new Course(p, courseName); // creates a new Course Student s = new Student("Bobby Jones", "Earhart Hall"); // Creates new Student System.out.println(s.getCourses()[0]); // prints "none" System.out.println(c.enroll(s)); // prints "true" System.out.println(s.getCourses()[0]); // prints "MA" System.out.println(c.getStudents()[0].getName()); // prints "Bobby Jones" System.out.println(c.getProfessor().getName()); // prints "Dr. Malcom" Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
