Question: In this exercise, you have to use java Interface. Examine the Employee class (Employee.java file provided with this Lab). Finish the compareTo() method using all
In this exercise, you have to use java Interface. Examine the Employee class (Employee.java file provided with this Lab). Finish the compareTo() method using all three of the instance variables. 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. Test your implementation using this class: EmployeeTester (also provided in the Lab folder).
Employee.java:
public 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 } }
EmployeeTester.java:
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 }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
