import java.util.*; import corejava.*; public class EmployeeSortTest { public static void main(String[] args) { Employee[] staff = new Employee[3]; staff[0] = new Employee("Harry Hacker", 35000, new Day(1989,10,1)); staff[1] = new Employee("Carl Cracker", 75000, new Day(1987,12,15)); staff[2] = new Employee("Tony Tester", 38000, new Day(1990,3,15)); Sortable.shell_sort(staff); int i; for (i = 0; i < staff.length; i++) System.out.println(staff[i]); } } abstract class Sortable { public abstract int compare(Sortable b); public static void shell_sort(Sortable[] a) { int n = a.length; int incr = n / 2; while (incr >= 1) { for (int i = incr; i < n; i++) { Sortable temp = a[i]; int j = i; while (j >= incr && temp.compare(a[j - incr]) < 0) { a[j] = a[j - incr]; j -= incr; } a[j] = temp; } incr /= 2; } } } class Employee extends Sortable { public Employee(String n, double s, Day d) { name = n; salary = s; hireDate = d; } public void raiseSalary(double byPercent) { salary *= 1 + byPercent / 100; } public String getName() { return name; } public double getSalary() { return salary; } public String toString() { return name + " " + salary + " " + hireYear(); } public int hireYear() { return 1900 + hireDate.getYear(); } public int compare(Sortable b) { Employee eb = (Employee)b; if (salary < eb.salary) return -1; if (salary > eb.salary) return 1; return 0; } private String name; private double salary; private Day hireDate; } /* * Cay S. Horstmann & Gary Cornell, Core Java * Published By Sun Microsystems Press/Prentice-Hall * Copyright (C) 1997 Sun Microsystems Inc. * All Rights Reserved. * * Permission to use, copy, modify, and distribute this * software and its documentation for NON-COMMERCIAL purposes * and without fee is hereby granted provided that this * copyright notice appears in all copies. * * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ /** * @version 1.00 07 Feb 1996 * @author Cay Horstmann */