Simple Java Inheritance practice problem Description Write a class Person.java t
ID: 3598450 • Letter: S
Question
Simple Java Inheritance practice problem
Description 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. . o String getName () o void setName (String name) o String getAddress () o 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: o String[] courses » o char[] grades » A constructor, Student(String name, string address) o You should initialize courses and grades here. - A new student's course array should have all entries as "none'" 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 » Getters for courses and grades » A unique method, boolean addCourse (string course) o This method will enroll the student in the given course 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 O New courses should be added to thelowest-numbered available slotin the courses arrayExplanation / Answer
Do UPVOTE everything's working. If any doubt leave a comment i will be happy to answer
public class Person
{
private String name;
private String address;
public Person(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
public class Course {
private Professor professor;
private Student[] students;
private String courseName;
private int numEnrolled;
public Course(Professor professor, String courseName) {
this.professor = professor;
this.courseName=courseName;
this.numEnrolled=0;
students=new Student[100];
}
public Professor getProfessor() {
return professor;
}
public void setProfessor(Professor professor) {
this.professor = professor;
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public int getNumEnrolled() {
return numEnrolled;
}
public void setNumEnrolled(int numEnrolled) {
this.numEnrolled = numEnrolled;
}
boolean enroll(Student s)
{
if(this.numEnrolled<=100)
{
for(int i=0;i<100;i++)
{
if(s.addCourse(getCourseName()) && students[i]==null)
{
students[i]=s;
numEnrolled++;
return true;
}
}
}
return false;
}
public static void main(String[] args)
{
String courseName="MA16200";
Professor p =new Professor("Dr. Malcolm","MATH",courseName,"Professor");
Course c= new Course(p,courseName);
Student s=new Student("Bobby","Earhart Hall");
System.out.println(s.getCourses()[0]);
System.out.println(c.enroll(s));
System.out.println(s.getCourses()[0]);
System.out.println(c.getStudents()[0].getName());
System.out.println(c.getProfessor().getName());
}
}
public class Professor extends Person
{
private double salary;
private String course;
private String rank;
public Professor(String name, String address, String course, String rank) {
super(name, address);
this.course = course;
this.rank = rank;
if(rank.equals("Assistant"))
salary=70000;
else
salary=100000;
}
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;
}
}
public class Student extends Person {
private String[] courses;
private char[] grades;
public Student(String name,String address)
{
super(name,address);
courses=new String[6];
grades=new char[6];
for(int i=0;i<6;i++) {
courses[i]="none";
grades[i]='A';
}
}
public String[] getCourses() {
return courses;
}
public char[] getGrades() {
return grades;
}boolean addCourse(String course)
{
for(int i=0;i<6;i++)
{
if(!courses[i].equals(course) && courses[i].equals("none"))
{
courses[i]=course;
return true;
}
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.