In Java, modify several predefined classes in order to leverage interfaces . Des
ID: 3916103 • Letter: I
Question
In Java, modify several predefined classes in order to leverage interfaces. Design two interfaces to use with these previously defined classes. Use the given Tester and Sort classes to run the program.
Class Student, NewWorker, MyDate:
Should need no changes.
Interface Sortable:
The interface Sortable has a single method: lessThan, which accepts as a parameter another Sortable and returns a boolean. This method will provide a means of comparison between two objects of the same type, but as Sortable is an interface you must completely define lessThan in classes that implement Sortable. The lessThan method returns true if the current object is less than the argument, and otherwise false. The criteria of “less than” depends on the class implementing the interface.
Interface HighIncome:
The interface HighIncome has a single method: fatCat, which accepts no parameters and returns a boolean. For this purpose, being rich depends on the type of person. Thus, the details of fatCat must be determined in each class implementing it.
Class Name:
The name class must now implement Sortable, because we will be using lessThan of Name objects inside of lessThan for Person objects. The lessThan method of Name must return true if the current object’s last name is less than that of the parameter (use compareTo of the String class). If the objects have the same last name, use the first name as a tie breaker.
Class Person:
Person will now implement Sortable and HighIncome. Therefore it must have definitions for the methods obtained from these interfaces. For lessThan, if the Sortable received as a parameter is a Person and the current Person has a name that is less than that of the Sortable received as a parameter, return true. Otherwise, false must be returned. For the fatCat method, if the salary of the Person is greater than or equal to $3000, return true. Otherwise, return false.
Class Household:
Household will now also have a method called sortHouseholdMembers which accepts no arguments and returns the toString of the household. This method must sort the householdMembers array, which should be very simple because Person is now a Sortable type. Further, this class has a method called findNumberOfFatCats which has no arguments and returns an integer. The returned value is the number of fat cats in the householdMembers array.
Sample output for Tester class:
Number of fat cats in family is 2
Description of the family before sorting is:
Name is Teller, Edward
Major: Physics
Name is Powell, Liz
Major: Computer Science
Name is Porter, Tom
Major: Computer Science
Worker Number 1
Worker Name Hunter, Robert W.
Date Joined: October 23, '09
No Supervisor
Salary: $5000.0
Worker Number 2
Worker Name Hull, Mary J.
Date Joined: September 6, '12
No Supervisor
Salary: $6000.0
Worker Number 3
Worker Name Hull, Liz M.
Date Joined: December 9, '14
No Supervisor
Salary: $2000.0
*********************************************************
Description of the family after sorting is:
Worker Number 3
Worker Name Hull, Liz M.
Date Joined: December 9, '14
No Supervisor
Salary: $2000.0
Worker Number 2
Worker Name Hull, Mary J.
Date Joined: September 6, '12
No Supervisor
Salary: $6000.0
Worker Number 1
Worker Name Hunter, Robert W.
Date Joined: October 23, '09
No Supervisor
Salary: $5000.0
Name is Porter, Tom
Major: Computer Science
Name is Powell, Liz
Major: Computer Science
Name is Teller, Edward
Major: Physics
Class Student:
public class Student extends Person {
private String major;
public Student(String fullName, String major) {
super(fullName);
this.major = new String(major);
}
//@Override
public double getSalary() {
return 0.0;
}
public String toString(){
return super.toString() + " Major: " + major + " ";
}
}
Class NewWorker:
public class NewWorker extends Person{
private static int howManyWorkers;
private int workerNumber;
private MyDate dateJoiningCompany;
private double salary;
private NewWorker supervisor;
public NewWorker(String nameOfWorker, String joiningDate, double workerSalary){
super(nameOfWorker);
this.dateJoiningCompany = new MyDate(joiningDate);
this.salary = workerSalary;
howManyWorkers++;
this.workerNumber = howManyWorkers;
}
public NewWorker(String nameOfWorker, String joiningDate){
super(nameOfWorker);
this.dateJoiningCompany = new MyDate(joiningDate);
this.salary = 0;
howManyWorkers++;
this.workerNumber = howManyWorkers;
}
public void setSalary(double setWorkerSalary){
this.salary = setWorkerSalary;
}
public double getSalary(){
return salary;
}
public void setSupervisor(NewWorker setWorkerSupervisor){
this.supervisor = setWorkerSupervisor;
}
public static int getHowManyWorkers(){
return howManyWorkers;
}
public Name getSupervisor(){
return new Name(supervisor.getPersonName());
}
public String toString(){
if(this.supervisor!= null){
return("Worker Number " + workerNumber + " " + "Worker Name " + getPersonName() + " " + "Date Joined: " + dateJoiningCompany + " " + "Supervisor: " + getSupervisor() + " "+ "Salary: $" + getSalary());
}
else
return("Worker Number " + workerNumber + " " + "Worker Name " + getPersonName() + " " + "Date Joined: " + dateJoiningCompany +" No Supervisor " + "Salary: $" + getSalary());
}
}
Class Name:
import java.util.StringTokenizer;
public class Name{
private String firstName = "";
private String middleName = "";
private String lastName = "";
public Name(String inputName){
StringTokenizer splitName = new StringTokenizer(inputName);
int numTokens = splitName.countTokens();
firstName = splitName.nextToken();
if(numTokens == 3){
middleName = splitName.nextToken();
}
else{
middleName = null;
}
lastName = splitName.nextToken();
}
public Name(Name nameCopy){
this.firstName = nameCopy.firstName;
this.middleName = nameCopy.middleName;
this.lastName = nameCopy.lastName;
}
public void setName(String newFirstName, String newMiddleName, String newLastName){
this.firstName = newFirstName;
this.middleName = newMiddleName;
this.lastName = newLastName;
}
public String toString(){
if(middleName != null){
return (lastName +", " + firstName + " " + middleName.charAt(0) + ".");
}
else
return (lastName +", " + firstName);
}
}
Class Person:
public abstract class Person{
private Name personName;
private Person spouse;
public Person(String fullName){
personName = new Name(fullName);
}
public void setSpouse(Person spouse){
this.spouse = spouse;
}
public Name getPersonName(){
return new Name(personName);
}
public Name getSpouseName(){
return new Name(spouse.personName);
}
public double getFamilyIncome(){
return this.getSalary() + spouse.getSalary();
}
public String toString(){
if(spouse != null){
return "Name is " + getPersonName() + " Married to " + getSpouseName();
}
else
return "Name is " + getPersonName();
}
public abstract double getSalary();
}
Class Sort:
public class Sort {
public static void sortAnything(Sortable listObjects[], int numObjects){
Sortable temp;
int indexSmallest, index1, index2;
for (index1 = 0; index1 < numObjects - 1; index1++){
indexSmallest = index1;
for (index2 = index1 + 1;
index2 < numObjects; index2++)
if (listObjects[index2].
lessThan(listObjects[indexSmallest]))
indexSmallest = index2;
temp = listObjects[index1];
listObjects[index1] = listObjects[indexSmallest];
listObjects[indexSmallest] = temp;
}
}
}
Class MyDate:
import java.util.StringTokenizer;
public class MyDate{
private int day;
private int month;
private int year;
public MyDate(String unformattedDate){
StringTokenizer splitDate = new StringTokenizer(unformattedDate, "/");
this.day = Integer.parseInt(splitDate.nextToken());
this.month = Integer.parseInt(splitDate.nextToken());
this.year = Integer.parseInt(splitDate.nextToken());
}
public MyDate(MyDate myDate){
this.day = myDate.day;
this.month = myDate.month;
this.year = myDate.year;
}
public String toString(){
String twoDigitYear = String.valueOf(this.year).substring(2,4);
String monthName [] = {"January", "February", "March", "April", "May", "June", "July", "August", "September",
"October", "November", "December"};
return monthName[month-1] + " " + day + ", " + twoDigitYear;
}
public boolean lessThan(MyDate myDate){
if(this.year < myDate.year){
return true;
}
else if (this.year > myDate.year){
return false;
}
else if (this.month < myDate.month){
return true;
}
else if (this.month > myDate.month){
return false;
}
else if (this.day < myDate.day){
return true;
}
else
return false;
}
public boolean equals(MyDate myDate) {
if (myDate == null) return false;
if((this.day == myDate.day) && (this.month == myDate.month) && (this.year == myDate.year)){
return true;
}
else
return false;
}
}
Class Tester:
public class Tester {
public static void main(String[] args){
NewWorker w1, w2, w3;
Student s1, s2, s3;
Household myFamily;
s1 = new Student("Edward Teller", "Physics");
s2 = new Student("Liz Powell", "Computer Science");
s3 = new Student("Tom Porter", "Computer Science");
w1 = new NewWorker("Robert William Hunter", "23/10/2009", 5000.00);
w2 = new NewWorker("Mary Jane Hull", "06/09/2012");
w3 = new NewWorker("Liz Mary Hull", "09/12/2014", 2000.00);
w2.setSalary(6000.00);
myFamily = new Household();
myFamily. insertNewHouseholdMember(s1);
myFamily. insertNewHouseholdMember(s2);
myFamily. insertNewHouseholdMember(s3);
myFamily. insertNewHouseholdMember(w1);
myFamily. insertNewHouseholdMember(w2);
myFamily. insertNewHouseholdMember(w3);
w1.setSpouse(w2);
w2.setSpouse(w1);
System.out.println("Number of fat cats in family is "+ myFamily.findNumberOfFatCats());
System.out.println(" Description of the family before sorting is: ");
System.out.println(myFamily);
System.out.println("*********************************************************");
System.out.println(" Description of the family after sorting is: ");
System.out.println(myFamily.sortHouseholdMembers());
}
}
Explanation / Answer
I am providing you Person and Name class along with interfaces.. you have not provided the class for Household, due to which i am not able to mathc the output.. please check and do comment if anything is needed. Thanks!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.