Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

HighIncome Interface and Sortable Interface using java

You will design two interfaces
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. A “fat cat” is slang for someone who is rich. For our purposes, being rich depends on the type of person. Thus, the details of fatCat must be determined in each class implementing it.
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 NewWorker:
Should not need any changes.
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 MyDate:
Should not need any changes.
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 accepts an integer. The returned value is the number of fat cats in the householdMembers array.
Class Manager, Student, NewWorker:
Should need no changes.
Sample output for Lab6Tester:
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

Answer:

HighIncome Interface

public interface HighIncome {
	public boolean fatCat(); 
}

Household Class

public class Household {
	private Person [] householdMembers; 
	private int howManyPeople = 0; 
	
	public Household(){
		householdMembers = new Person[10]; 
	}
	
	public Household(int size){
		householdMembers = new Person[size]; 
	}
	
	public String sortHouseholdMembers(){
		
		Sort.sortAnything(householdMembers, howManyPeople);
		String s = ""; 
		
		for(Person p: householdMembers){
			
			if(p != null){
				s += p.toString() + "\n"; 
			}
		
		}
		return s; 
	}
	
	public int findNumberOfFatCats(){
		int fatcats = 0; 
		
		for(int i = 0; i < howManyPeople; i++){
			
			if(householdMembers[i].fatCat() == true){
				fatcats++; 
			}
		}
		return fatcats; 
	}
	
	public void insertNewHouseholdMember(Person aPerson){
		
		for(int i = 0; i < householdMembers.length; i++){
			
			if(householdMembers[i] == null){
				householdMembers[i] = aPerson;
				howManyPeople++;
				break; 
			}
		}
	}
	
	/* toString method*/
	public String toString(){
		String s = ""; 
		for(Person p: householdMembers){
			if(p != null){
			s += p.toString() + "\n"; 
			}
			
		}
		return s; 
	}
	
	public int getHowManyPeople(){
		return howManyPeople; 
	}
}

Tester Class

public class Lab6Tester {
	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("\nDescription of the family before sorting is:\n");
		System.out.println(myFamily);
		System.out.println("*********************************************************");
		System.out.println("\nDescription of the family after sorting is:\n");
		System.out.println(myFamily.sortHouseholdMembers());
		
		
		
	}

}

Manager Class

public class Manager extends NewWorker {
	
	/*Declare an array of size 10 of NewWorker base type*/
	private NewWorker employeesSupervised []= new NewWorker [10];
	/*stores the number of workers supervised.*/
	private int numWorkersSupervised;

	/*First Constructor accepts a string for the name of the worker, a string for the 
	date that the worker joined the company, and a double which is the salary of
	the worker*/
	public Manager (String name, String date, double salary){
		super (name, date,salary);
	}
	
	/* Second Constructor accepts only the name and the date.*/
	public Manager (String name, String date){
		super (name,date);
	}

	/* addWorker method accepts a NewWorker as an argument,which adds the received argument to the instanceVariable
	employeesSupervised. This method add the argument to the first available index in the array, and it also
	increment numWorkersSupervised */
	public void addWorker (NewWorker w){
		if (numWorkersSupervised < 10){
			employeesSupervised [numWorkersSupervised] = w;
			numWorkersSupervised++;
			super.setSalary(super.getSalary() + 100);
		}
	}
	
	/* deleteWorker method accepts a NewWorker as an argument, finds the received NewWorker in 
	employeesSupervised, and sets the indexed variable at its position to null. It also decrease
	numWorkersSupervised and reorganized the array such that there are no gaps in it */
	public void deleteWorker (NewWorker w){
		boolean removed = false;
		for (int i = 0 ; i < numWorkersSupervised; i++){
			if (((NewWorker)employeesSupervised [i]).equals(w)){
				employeesSupervised [i] = employeesSupervised[i+1];
				removed = true;
			}
			/* shift elements of array left*/
			else if (removed == true)
				employeesSupervised[i] = employeesSupervised [i+1];
		}

		/*Check if worker got removed, deduct pay by 100*/
		if (removed == true){
			numWorkersSupervised --;
			super.setSalary(super.getSalary() - 100);
		}
	}
	
	/*setting salary using NewWorker method*/
	public double getSalary(){
		return super.getSalary();
	}

	public String toString(){
		String wholeString = super.toString() + "\n" +numWorkersSupervised+ " Worker(s) Supervised by this Manager: " + "\n";
		/*cast employee supervised to NewWorker to get the name*/
		for (int i = 0 ; i < numWorkersSupervised; i++)
			wholeString += ((NewWorker)employeesSupervised [i]).getPersonName() + "\n";
		return wholeString;
	}
}

MyDate Class

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;
	}
}

Name Class

import java.util.*;

public class Name implements Sortable{	
	private String firstName;
	private String middleName;
	private String lastName; 
	
	/* Constructor */
	public Name(String FullName){
		StringTokenizer myTokens = new StringTokenizer(FullName," ");
		
		/* if we have first and last name*/
		if(myTokens.countTokens() == 2) {
			firstName = myTokens.nextToken();
			lastName = myTokens.nextToken(); 
			middleName = null;
		}
		
		/*  if we have full name */
		else if(myTokens.countTokens() == 3){
			firstName = myTokens.nextToken();
			middleName = myTokens.nextToken(); 
			lastName = myTokens.nextToken(); 
		}
		
		else {
			System.out.println("Not Valid name");
		}
	}
	
	/* Copy Constructor */
	public Name(Name aName){
		firstName = aName.firstName; 
		middleName = aName.middleName; 
		lastName = aName.lastName; 
	}
	
	/* setName Method  */
	public void setName(String firstName,String middleName, String lastName){
	     this.firstName = firstName;
	     this.middleName = middleName; 
	     this.lastName = lastName; 
	}
	
	@Override
	public boolean lessThan(Sortable anObject) {
	    
	    Name temp; 
	    temp = (Name) anObject; 
	    
	    if(getLastName().compareTo(temp.getLastName()) == 0){
	        if(getFirstName().compareTo(temp.getFirstName()) < 0 ){
	            return true; 
	        }
	        else{
	            return false; 
	        }
	    }
	    if(getLastName().compareTo(temp.getLastName()) < 0 ){
	        return true;
	    }
	    else{
	        return false; 
	    }
	}

	/*toString method*/
	public String toString(){
		if(middleName != null){
			return lastName + ", " + firstName + " " + middleName.charAt(0) + "." ;
		}
		else{
			return lastName + ", " + firstName + " " ;
		}
	}		

String getFirstName(){
	return firstName; 
}


String getMiddleName(){
	return middleName; 
}

String getLastName(){
	return lastName; 
}



}

NewWorker Class

public class NewWorker extends Person {

	private static int howManyWorkers = 0; 
	private int workerNumber;  
	private MyDate dateJoiningCompany; 
	private double Salary; 
	private NewWorker Supervisor;  
	
	/*Constructor with 3 parameters */
	public NewWorker(String workerName,String dateJoiningCompany,double Salary){
		super(workerName); 
		this.dateJoiningCompany = new MyDate(dateJoiningCompany); 
		this.Salary = Salary; 
		
		if(howManyWorkers == 0){
			workerNumber = 1;
			howManyWorkers++; 
		}
		
		else{
			howManyWorkers++;
			workerNumber = howManyWorkers; 
			 
		}
		
	}
	
	/*Constructor with 2 parameters */
	public NewWorker(String workerName, String dateJoiningCompany){
		super(workerName); 
		this.dateJoiningCompany = new MyDate(dateJoiningCompany);
		Salary = 0.0; 
		
		if(howManyWorkers == 0){
			workerNumber = 1;
			howManyWorkers++; 
		}
		
		else{
			howManyWorkers++;
			workerNumber = howManyWorkers; 
			 
		}
	}
	
	/* setSalary Method */
	public void setSalary(double Salary)
	{
		this.Salary = Salary; 
	}
	
	/* setSupervisor method */
	public void setSupervisor(NewWorker Supervisor){
		this.Supervisor = Supervisor; 
	}
	
	/* getSupervisor method*/
	public Name getSupervisor(){
		return new Name(Supervisor.getPersonName());
	}
	/* getHowManyWorkers method */
	public static int getHowManyWorkers() {
		return howManyWorkers;
	}
	
	public String toString(){
		if(Supervisor == null){
			return "\nWorker Number " + workerNumber + "\nWorker Name " +  getPersonName() + "\n" + "Date Joined: " + dateJoiningCompany.toString() + "\n" + "No Supervisor "+"\nsalary: $" + Salary+"\n";
		}
		
		else{
			return workerNumber + ": " + super.toString() + dateJoiningCompany.toString() + " "+ Supervisor.getPersonName() + " $" + Salary;
		}	
	}

	/* getSalary method*/
	public double getSalary(){
		return Salary; 
	}

	@Override
	public double getFamilyIncome() {
		
		double allTheMoney;
		allTheMoney = this.getSalary(); 
		if(spouse != null)
			allTheMoney += spouse.getSalary(); 	
		return allTheMoney;
	}
	
}

Person Class

public abstract class Person implements HighIncome, Sortable{
	
	private Name personName;
	protected Person spouse; 
	
	/* Abstract Methods*/
	public abstract double getSalary(); 
	public abstract double getFamilyIncome(); 
	
	/* Constructor */
	public Person(String fullName){
		personName = new Name(fullName); 
	}
	
	/* setSpouse method */
	public void setSpouse(Person aPerson){
		this.spouse = aPerson;
		
	}
	
	/* to String Person method */
	public String toString(){
		//if(spouse != null){
		//	return "Name is " + personName.toString() + " Married to " + spouse.personName.toString(); 
		//}
			return "Name is " + personName.toString(); 
	}
	

	@Override
	public boolean lessThan(Sortable anObject) {   
	    Person temp; 
	    temp = (Person) anObject; 	    
	    if(personName.getLastName().compareTo(temp.personName.getLastName()) < 0 ){
	    	return true; 
	    }
	    if(personName.getLastName().compareTo(temp.personName.getLastName()) == 0) {
	    	if(personName.getFirstName().compareTo(temp.personName.getFirstName()) < 0){
	    		return true; 
	    	}
	    	else{
	    		return false; 
	    	}  
	    }
	    return false; 
	}
	
	/* Make sure this in student not NewWorker */
	public boolean fatCat(){	
		if(getSalary() < 3000){
			return false;
		}
		else{
			return true; 
		}
	}	
	
	public Name getPersonName(){
		return personName; 
	}
	
}
	

Sort Class

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;
    		}
  	}     
}

Sortable Interface

public interface Sortable {
	public boolean lessThan(Sortable anObject);
}

Student Class

public class Student extends Person{

	private String major; 
	
	public Student(String fullName, String major) {
		super(fullName);
		this.major = major; 
	}

	@Override
	public double getSalary() {
		return 0.0;
	}
	
	public String toString(){
		return super.toString() + "\n" + "Major: " + major+"\n";
		
	}

	@Override
	public double getFamilyIncome() {
		double allTheMoney = 0.00; 
		
		if(spouse != null)allTheMoney = spouse.getSalary(); 
		
		return allTheMoney;
	}
	
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!