(* Read the question to the end before your start.) Use this Person class: publi
ID: 3774496 • Letter: #
Question
(* Read the question to the end before your start.)
Use this Person class:
public abstract class Person implements Comparable<Person> {
protected String firstName;
protected java.util.Date birthdate;
public Person(String firstName, java.util.Date birthdate) {
this.firstName = firstName;
this.birthdate = birthdate;
}
public java.util.Date getBirthdate() {
return birthdate;
}
public String getName() {
return firstName;
}
public String toString() {
return getName() + " was born on " + birthdate;
}
public abstract Double getSalary();
public int compareTo(Person o) {
// complete this method
}
}
Design and implement Faculty and Staff classes that extend Person. Each class should contain the following methods:
A constructor that creates an object with the specified data fields
‘monthlySalary’ of double type and ‘status’ of String type for a Faculty
‘annualSalary’ of double type and ‘experience’ of int type for a Staff
Accessor(get) methods for each data field.
A method named getSalary ( ) that returns the salary of the object as a Double (not a lower-case-d double)
Salary of a Faculty = monthlySalary*9.0
Salary of a Staff = annualSalary + annualSalary*(30.0-experience)/60.0
A method named toString( ) that returns a string description of the object. The description should contain name, birthdate, and status for a Faculty object (experience for a Staff object). For example, the toString( ) method for a Faculty should return: “John was born on Sun Jan 03 00:00:00 PST 1971: full-time”
Then complete the compareTo() method in Person so that it produces a sort order in descending order of *annual* salary.
The following test program creates 5 objects and stores them in an array of type Person.
public class PersonTest {
public static void main(String[] args) {
Person[] ps = new Person[5];
ps[0] = new Faculty("Andy", new Date("11/12/1980"), 4000.0);
ps[1] = new Faculty("Barry", new Date("1/2/1973"), 6000.0);
ps[2] = new Staff("Cathy", new Date("8/19/1968"), 2000.0, 3.0);
ps[3] = new Staff("Don", new Date("4/17/1969"), 4000.0, 2.0);
ps[4] = new Staff("Edith", new Date("10/28/1979"), 5000.0, 1.0);
// add code here
}
}
Complete the driver so that it sorts the array in descending order of salary (use Arrays.sort), then prints the toString of each object, then prints the name and annual salary of each Staff member (use instanceof), then prints the name and annual salary of each Faculty.
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
########### Person.java ############
public abstract class Person implements Comparable<Person> {
protected String firstName;
protected java.util.Date birthdate;
public Person(String firstName, java.util.Date birthdate) {
this.firstName = firstName;
this.birthdate = birthdate;
}
public java.util.Date getBirthdate() {
return birthdate;
}
public String getName() {
return firstName;
}
public String toString() {
return getName() + " was born on " + birthdate;
}
public abstract Double getSalary();
public int compareTo(Person o) {
if(getSalary() < o.getSalary())
return -1;
else if(getSalary() > o.getSalary())
return 1;
else
return 0;
}
}
############ Faculty.java ###########
import java.util.Date;
public class Faculty extends Person {
private double monthlySalary;
private String status;
public Faculty(String firstName, Date birthdate, double monthlySalary, String status) {
super(firstName, birthdate);
this.monthlySalary = monthlySalary;
this.status = status;
}
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double monthlySalary) {
this.monthlySalary = monthlySalary;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public Double getSalary() {
return monthlySalary*9.0;
}
@Override
public String toString() {
return getName()+" was born on "+getBirthdate().toString()+": "+status;
}
}
################ Staff.java ##############
import java.util.Date;
public class Staff extends Person {
private double annualSalary;
private double experience;
public Staff(String firstName, Date birthdate, double annualSalary, double experience) {
super(firstName, birthdate);
this.annualSalary = annualSalary;
this.experience = experience;
}
public double getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(double annualSalary) {
this.annualSalary = annualSalary;
}
public double getExperience() {
return experience;
}
public void setExperience(double experience) {
this.experience = experience;
}
@Override
public Double getSalary() {
return annualSalary + annualSalary*(30.0-experience)/60.0;
}
@Override
public String toString() {
return getName()+ " was born on "+getBirthdate().toString()+": "+experience;
}
}
################ PersonTest.java ###########
import java.util.Arrays;
import java.util.Date;
public class PersonTest {
public static void main(String[] args) {
Person[] ps = new Person[5];
ps[0] = new Faculty("Andy", new Date("11/12/1980"), 4000.0, "full time");
ps[1] = new Faculty("Barry", new Date("1/2/1973"), 6000.0, "on contract");
ps[2] = new Staff("Cathy", new Date("8/19/1968"), 2000.0, 3.0);
ps[3] = new Staff("Don", new Date("4/17/1969"), 4000.0, 2.0);
ps[4] = new Staff("Edith", new Date("10/28/1979"), 5000.0, 1.0);
// add code here
// displaying array info
for(Person p: ps){
System.out.println("Salary: "+p.getSalary());
System.out.println(p);
}
System.out.println();
// sorting
Arrays.sort(ps);
System.out.println();
for(Person p: ps){
System.out.println("Salary: "+p.getSalary());
System.out.println(p);
}
System.out.println();
}
}
/*
Sample run:
Salary: 36000.0
Andy was born on Wed Nov 12 00:00:00 IST 1980: full time
Salary: 54000.0
Barry was born on Tue Jan 02 00:00:00 IST 1973: on contract
Salary: 2900.0
Cathy was born on Mon Aug 19 00:00:00 IST 1968: 3.0
Salary: 5866.666666666667
Don was born on Thu Apr 17 00:00:00 IST 1969: 2.0
Salary: 7416.666666666666
Edith was born on Sun Oct 28 00:00:00 IST 1979: 1.0
Salary: 2900.0
Cathy was born on Mon Aug 19 00:00:00 IST 1968: 3.0
Salary: 5866.666666666667
Don was born on Thu Apr 17 00:00:00 IST 1969: 2.0
Salary: 7416.666666666666
Edith was born on Sun Oct 28 00:00:00 IST 1979: 1.0
Salary: 36000.0
Andy was born on Wed Nov 12 00:00:00 IST 1980: full time
Salary: 54000.0
Barry was born on Tue Jan 02 00:00:00 IST 1973: on contract
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.