JAVA Write a class Person.java that acts as a person with Strings to hold their
ID: 3826174 • Letter: J
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, 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 student’s course array should have all entries as “none”.
char[] grades. A new student’s 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 professor’s 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 professor’s 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:
Explanation / Answer
//Person.java
public class Person {
String name;
String address;
public Person(String name, String address){
this.name = name;
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//Student.java
public class Student extends Person {
String[] courses = new String[6];
public char[] getGrades() {
return grades;
}
public String[] getCourses() {
return courses;
}
char[] grades= new char[6];
public Student(String name, String address) {
super(name, address);
// courses =;
grades = "AAAAAA".toCharArray();
}
public boolean addCourse(String course){
boolean flag = false;
if((courses.length < 6)){
for(int i=0;i < courses.length;i++){
if(courses[i] == course) flag = true;
}
if(flag == false)
courses[6 - (courses.length)] = course;
}
return flag;
}
}
//Professor.java
public class Professor extends Person {
double salary;
String course;
String rank;
public Professor(String name, String address, String course, String rank) {
super(name, address);
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
}
//Course.java
public class Course {
Professor professor;
Student[] students = new Student[100];
String courseName;
int numEnrolled;
Course(Professor professor, String courseName){
this.courseName = courseName;
this.professor = professor;
if(professor.getRank() == "Assistent"){
professor.setSalary(70000);
}else if(professor.getRank() == "Professor"){
professor.setSalary(100000);
}
}
public Professor getProfessor() {
return professor;
}
public Student[] getStudents() {
return students;
}
public String getCourseName() {
return courseName;
}
public int getNumEnrolled() {
return numEnrolled;
}
boolean enroll(Student s){
if(students.length < 100){
students[students.length + 1] = s;
return true;
}
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.