Question: Change the compareTo function so that the findMax method can be used to find the alphabetically last person. import java.util.ArrayList; import java.util.Collections; public class Person

Change the compareTo function so that the findMax method can be used to find the alphabetically last person.

import java.util.ArrayList;

import java.util.Collections;

public class Person implements Comparable {

private String name;

private int age;

private String address;

private String phone;

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

String tname = o.name;

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!