Question: Implementing the Comparable interface CODE TO HELP REFERENCE: public class Person { public String firstName; public String lastName; public Person(String firstName, String lastName) { this.firstName

Implementing the Comparable interface Implementing the Comparable interface CODE TO HELP REFERENCE: public class Person { CODE TO HELP REFERENCE:

public class Person { public String firstName; public String lastName;

public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }

public String getFirstName() { return firstName; }

public void setFirstName(String firstName) { this.firstName = firstName; }

public String getLastName() { return lastName; }

public void setLastName(String lastName) { this.lastName = lastName; } }

-----------------------------------------------------------------------------------------

public class Athlete extends Person { private String sports; private double hoursTrained;

public Athlete(String firstName, String lastName, String sports) { super(firstName, lastName); this.sports = sports; }

public String getSports() { return sports; }

public double getHoursTrained() { return hoursTrained; }

public void train(double hoursTrained) { this.hoursTrained = hoursTrained; }

@Override public String toString() { return "Athlete: " + getSports() + ", getHoursTrained: " + getHoursTrained(); } }

-----------------------------------------------------------------------------------------------------------------

public class Runner extends Athlete { protected double milesRaced; private int numberOfRaces;

public Runner(String firstName, String lastName) { super(firstName, lastName,"Running"); this.milesRaced = 0.0; this.numberOfRaces = 0; }

public double getMilesRaced() { return milesRaced; }

public int getNumberOfRaces() { return numberOfRaces; }

private void setMilesRaced(double milesRaced) { this.milesRaced = milesRaced; }

private void setNumberOfRaces(int numberOfRaces) { this.numberOfRaces = numberOfRaces; }

public void race(double milesRaced) { setNumberOfRaces(getNumberOfRaces() + 1); setMilesRaced(getMilesRaced() + milesRaced); } public void train(double hoursTrained) { System.out.println("Runner train method"); super.train(hoursTrained); } public String toString() { String retString = "Runner: "; retString += getFirstName() + " " + getLastName() + " " + getSports() + " " + getHoursTrained() + " " + getMilesRaced() + " " + getNumberOfRaces(); return retString; } }

Array Example Modify your Athlete subclass to extend the Comparable interface. Make sure to qualify the comparing type Comparable to match your class name 1. 2. Implement the compareTo) method to satisfy the interface "contract" You may use any field (numeric or string) from your class to compare objects When using a string, you may shortcut by using the compareTo() method from the String class a. b. 3. Create a Tester class for your athlete. a. In the main() method i. Create an Array of your Athlete subclas ii. Populate the elements of the array with instances of your Athlete subclass ii. Call the method displayToString() with your array as an argument iv. Use the Arrays.sort() method to sort the array v. Call the method displayToString) with your array as an argument b. Create a method displayToString() that takes an array of your Athlete subclasss as a parameter i. Iterate through the array to display each of your athletes 4. Optional: Change the compareTo) method to compare using a different attribute. Then run your tester again, you should see a difference in the output. The implementation of the Comparable interface is useful and common, but it is limited in its scope as this comparison is "fixed". The java Comparator interface can be used to specify multiple ways for comparisons. We will explore this later in class. a

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!