Question
4.36 - Modify the Person class so that it can use findMax to obtain the alphabetically last person. You should need to worry about it,
4.36 - Modify the Person class so that it can use findMax to obtain the alphabetically last person.
You should need to worry about it, but I put the findMax method at the bottom of the Person class
The text contains a poor wording choice. It should say something like, "Change the Person class so that the (unaltered) findMax method can find the alphabetically last person."
Let's not go crazy and say that two people are only the same if they have the same name, same age, same address, and same phone. That would force us to figure out a sorted order for all of those.
Let's say that they are the same if their names are the same, and use the name to determine which comes first. The String class has a compareTo, or a compareToIgnoreCase method that will tell you which one comes first.
the code
import java.util.ArrayList; import java.util.Collections;
public class Person implements Comparable
public Person( String n, int ag, String ad, String p ) { name = n; age = ag; address = ad; phone = p; } public String toString( ) { return getName( ) + " " + getAge( ) + " " + getPhoneNumber( ); } public final String getName( ) { return name; } public final int getAge( ) { return age; } public final String getAddress( ) { return address; } public final String getPhoneNumber( ) { return phone; } public final void setAddress( String newAddress ) { address = newAddress; } public final void setPhoneNumber( String newPhone ) { phone = newPhone; }
@Override public int compareTo(Person o) { // TODO make this method work return 0; }
/** * Return max item in a. * Precondition: a.length > 0 */ //Don't change this method public static Comparable findMax( Comparable [ ] a ) { int maxIndex = 0; for( int i = 1; i < a.length; i++ ) if( a[ i ].compareTo( a[ maxIndex ] ) > 0 ) maxIndex = i; return a[ maxIndex ]; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started