Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify these class to... scrolll down i know not all information is here but i w

ID: 3853703 • Letter: M

Question

Modify these class to... scrolll down

i know not all information is here but i will gladly add it if needed. if you can make do with only this information that would be cool

Household:

public class Household {
   private Person householdMembers[];
   private int numHouseholdMembers = 0;
  
   public Household(){
       householdMembers = new Person[10];
   }
  
   public Household(int maxNumberOfHouseholdMembers){
       householdMembers = new Person[maxNumberOfHouseholdMembers];
   }
  
   public void insertNewHouseholdMember(Person newMember){
       if (numHouseholdMembers < householdMembers.length) {
       householdMembers[numHouseholdMembers] = newMember;
       numHouseholdMembers++;
       } else {
       System.out.println("House not big enough .");
       }
   }
  
   public String toString(){
       String s = "";
       for (Person member : householdMembers)
           s += member + " ";

       return s;
   }

}

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

person:

public abstract class Person {
   private Name personName;
   private Person spouse = null;
  
   public abstract double getSalary();

   public Person(String name){
       personName = new Name(name);
   }
  
   public void setSpouse(Person p){
       spouse = p;
   }
  
   public Name getName(){
       return personName;
   }
  
   public double getFamilyIncome(){
       if (spouse != null) {
       return this.getSalary() + spouse.getSalary();
       } else {
       return this.getSalary();
       }
   }

   public String toString(){
       String personDescriptor;
       personDescriptor = "Name is " + personName;
       if (spouse != null) {
           personDescriptor += "married to " + spouse.personName;
       }
      return (personDescriptor + " ");
   }

}

Need to modify these current class to the following

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.

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 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.

Explanation / Answer

Here is the modified and new code. Since the description for HighIncome and Sortable is not given, I have designed it based on whatever information is available in question. Let me through comments if anything else is needed. Please rate the answer if it helped. Thank you.

Sortable.java

public interface Sortable {

   public boolean lessThan(Sortable other);

}

HighIncome.java

public interface HighIncome {

   boolean fatCat();

}

Name.java

import java.util.StringTokenizer;

public class Name implements Sortable{

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

   }

@Override

public boolean lessThan(Sortable other) {

   if(other instanceof Name)

   {

      

       Name n2 = (Name)other;

       if(lastName.compareTo(n2.lastName) < 0) //if this last name is less than other's last name

       {

           return true;

       }

       else if(lastName.compareTo(n2.lastName) > 0)//if this last name is greater than other's last name

       {

           return false;

       }

       else //when equal , tie

       {

           return firstName.compareTo(n2.firstName) < 0;

       }      

   }

   return false;

}

}

Person.java

public abstract class Person implements Sortable, HighIncome{

   private Name personName;

   private Person spouse = null;

  

   public abstract double getSalary();

   public Person(String name){

   personName = new Name(name);

   }

  

   public void setSpouse(Person p){

   spouse = p;

   }

  

   public Name getName(){

   return personName;

   }

  

   public double getFamilyIncome(){

   if (spouse != null) {

   return this.getSalary() + spouse.getSalary();

   } else {

   return this.getSalary();

   }

   }

   public String toString(){

   String personDescriptor;

   personDescriptor = "Name is " + personName;

   if (spouse != null) {

   personDescriptor += "married to " + spouse.personName;

   }

return (personDescriptor + " ");

   }

   @Override

   public boolean lessThan(Sortable other) {

       if(other instanceof Person)

       {

           return personName.lessThan(((Person)other).personName);

       }

       else

           return false;

      

   }

   @Override

   public boolean fatCat() {

       if(getSalary() >= 3000)

           return true;

       else

           return false;

   }

  

  

}

Household.java

public class Household {

   private Person householdMembers[];

   private int numHouseholdMembers = 0;

  

   public Household(){

   householdMembers = new Person[10];

   }

  

   public Household(int maxNumberOfHouseholdMembers){

   householdMembers = new Person[maxNumberOfHouseholdMembers];

   }

  

   public void insertNewHouseholdMember(Person newMember){

   if (numHouseholdMembers < householdMembers.length) {

   householdMembers[numHouseholdMembers] = newMember;

   numHouseholdMembers++;

   } else {

   System.out.println("House not big enough .");

   }

   }

   String sortHouseholdMembers()

   {

   int minIdx;

   //selection sort

   for(int i = 0; i <numHouseholdMembers; i++)

   {

       minIdx = i;

       for(int j = i+1; j < numHouseholdMembers; j++)

       {

           if(householdMembers[j].lessThan(householdMembers[minIdx]))

               minIdx = j;

       }

       if(minIdx != i)

       {

           //swap

           Person temp = householdMembers[i];

           householdMembers[i] = householdMembers[minIdx];

           householdMembers[minIdx] = temp;

       }

   }

     

   return toString();

   }

  

   int findNumberOfFatCats()

   {

   int total = 0;

   for(int i = 0 ; i < numHouseholdMembers; i++)

   {

       if(householdMembers[i].fatCat())

           total++;

   }

   return total;

   }

   public String toString(){

   String s = "";

   for(int i = 0; i < numHouseholdMembers; i++)

   s += householdMembers[i] + " ";

   return s;

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote