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