Question
I'm trying to finish the compareTo() method using all three of the instance variables. I need to access them using the getX() methods. Compare two
I'm trying to finish the compareTo() method using all three of the instance variables. I need to access them using the getX() methods. Compare two employees by first looking at their last names. If the names are different, return the value the String method compareTo() returns with the last names. If the names are equal, look at the first name and return the value the String method compareTo() returns. If both parts of the name are equal, return the difference of the birthYears so that the older employee precedes the younger employee.
import java.util.Arrays;
class EmployeeTester { public static void main ( String[] args ) { Employee[] workers = new Employee[12]; workers[0] = new Employee( "Fred", "Adams", 1963); workers[1] = new Employee( "John", "Adams", 1959); workers[2] = new Employee( "Elmer", "Adams", 1976); workers[3] = new Employee( "Nancy", "Devon", 1963); workers[4] = new Employee( "Andrew", "Lewis", 1983); workers[5] = new Employee( "Douglas", "Page", 1981); workers[6] = new Employee( "Donald", "Wolder", 1963); workers[7] = new Employee( "Henry", "Wolder", 1972); workers[8] = new Employee( "Robert", "Wolder", 1959); workers[9] = new Employee( "Howard", "Cohen", 1933); workers[10] = new Employee( "Howard", "Cohen", 1958); workers[11] = new Employee( "Donald", "Rice", 1935); Arrays.sort( workers ); for ( int j=0; j } class Employee implements Comparable String getFirstName() { return firstName; } String getLastName() { return lastName; } int getBirthYear() { return birthYear; } public Employee( String f, String l, int year ) { firstName = f; lastName = l; birthYear = year; } public String toString(){ finish this } public int compareTo( Employee other ) { finish this } }
Step 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