Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 image text in transcribed 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

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

Transactions On Large Scale Data And Knowledge Centered Systems Xxxviii Special Issue On Database And Expert Systems Applications Lncs 11250

Authors: Abdelkader Hameurlain ,Roland Wagner ,Sven Hartmann ,Hui Ma

1st Edition

3662583836, 978-3662583838

More Books

Students also viewed these Databases questions

Question

3. Describe the communicative power of group affiliations

Answered: 1 week ago