Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions