A constructor, Person(String name, String address) Getters and Setters for name
ID: 3811940 • Letter: A
Question
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, 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 otherwiseExplanation / Answer
Person Class:
public class Person {
private String name_l;
private String address_l;
Person(String name, String address)
{
System.out.println("Person constrcutor called...");
name_l = name;
address_l = address;
}
void setName(String name)
{
name_l = name;
}
void setAddress(String address)
{
address_l = address;
}
String getName()
{
return name_l;
}
String getAddress()
{
return address_l;
}
void show()
{
System.out.println("Method show called...");
}
public static void main(String args[])
{
}
}
Student class:
public class Student extends Person {
int course_num = 0;
String[] course; // initialize using constructor as none
char[] grades; // initialize using constructor as 'A'
Student(String name, String address) {
super(name, address);
course_num = 0;
for(int i = 1; i<=6; i++)
{
course[i] = "none";
grades[i] = 'A';
}
// TODO Auto-generated constructor stub
}
String[] getCourse()
{
return course;
}
char[] getGrades()
{
return grades;
}
// not enrolled in course
// not already enrolled in 6 courses
// new course added to leftmost available slot in array
// this returns true if successful else false
boolean addCourse(String newCourse)
{
if(course_num==6)
return false;
int i = 1;
// compare newCourse in course array of strings
//for(i = 0; i<course_num;i++)
while(i<=course_num)
{
if(course[i].equals(newCourse))
{
return false;
}
else
i++;
}
if(i>course_num) // course not present, shift strings to insert new at first position
{
int j;
for(j = course_num+1; j>1; j--)
{
course[j] = course[j-1];
}
course[j] = newCourse;
}
return true;
//System.out.println("Course " +i +" is " +course[i]);
}
}
Professor class
public class Professor extends Person {
private String l_course;
private String l_rank;
private double l_salary;
Professor(String name, String address, String course, String rank) {
super(name, address);
// TODO Auto-generated constructor stub
l_course = course;
l_rank = rank;
}
void setSalary(double salary)
{
l_salary = salary;
}
double getSalary()
{
return l_salary;
}
void setCourse(String course)
{
l_course = course;
}
String getCourse()
{
return l_course;
}
void setRank(String rank)
{
l_rank = rank;
if(rank.equals("Professor"))
{
l_salary = 100000;
}
}
String getRank()
{
return l_rank;
}
}
course class
public class Course {
Person[] per = new Person[100];
Professor prof;
Student[] stud = new Student[100];
String l_courseName;
int l_numEnrolled;
Course(Professor professor, String courseName)
{
l_courseName = courseName;
prof = professor;
}
String getcourseName()
{
return l_courseName;
}
int getnumEnrolled()
{
return l_numEnrolled;
}
boolean enroll(Student s)
{
if(s.course_num==6)
return false;
else
s.addCourse(l_courseName);
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.