Consider the following hierarchy of classes:
// Person: base class adapted from Weiss's book
public class Person {
private String name; // name of person
private String ssn; // SSN of person
private int age; // age of person
public Person (String n, String s, int a) {
name = n; ssn = s; age = a;
}
public String getName() { return name;}
public String getSsn() { return ssn;}
public int getAge() { return age;}
public String toString() {
return "Name: " + name + ", SSN: " + ssn + ", Age: " + age;
}
}
// Employee: class that extends Person
public class Employee extends Person {
private double salary;
public Employee (String n, String ssn, int a, double sal) {
super(n, ssn, a); salary = sal;
}
public double getSalary() { return salary;}
public String toString() {
return super.toString() + ", Salary: " + salary;
}
}
// Student: class that extends Person
public class Student extends Person {
private double gpa;
public Student (String n, String ssn, int a, double g) {
super(n, ssn, a); gpa = g;
}
public double getGpa() { return gpa;}
public String toString() {
return super.toString() + ", GPA: " + gpa;
}
}
// Persons: use the classes Person, Student, and Employee
public class Persons {
public static void main(String[] args) {
Person[] p = new Person[5];
p[0] = new Person("Williams, John", "534-54-2344", 23);
p[1] = new Employee("Johnson, Jane", "232-33-3343", 43, 16000.0);
p[2] = new Student("Wilkins, Jean", "123-89-5688", 19, 3.5);
p[3] = new Employee("Jacobs, Joe", "112-21-4023", 58, 47000);
p[4] = new Student("Wilson, James", "003-00-8755", 21, 2.75);
for (int i = 0; i < p.length; i++)
System.out.println(p[i]);
}
}
- Write a class Staff that extends
Employee above. The new class should have
an extra private int data field
percentTime that gives the percent time the
staff member works (100 = full-time, 50 = half-time, etc.).
- Show additions to the
Persons class to add another person to
the array: a new staff member with name
Wheeler, Jason,
ssn 458-67-2100, age 34, salary
17000, and percent time 75.
- Suppose we want to add objects of type Person
to a list of Comparable items, as we did
in class. Suppose we then want to remove the objects in
decreasing order of age, using the removeMin
method in the general CompList class we
had before. Indicate clearly what changes and additions are needed
to the class hierarchy above? (Then the list becomes a priority queue,
where the priority is the age of the person, so that
older people get preference over younger ones.)
Are any changes needed to the code for the list itself.
- Assuming that the five people in the code above, along with
the extra person in part (b) above have all been added to an
instance of the CompList class, give code that will
remove all these people in decreasing
order of age, printing information about them as they are removed.
[Note: the CompList class works with a list of
Comparable objects. The class has methods
add, remove,
isEmpty, and removeMin.]
Points for each problem: 1-20, 2-30, 3-50.