Interface Example - Sorting Arrays of Different Types
Sortable.java:
// interface Sortable -- makes an array of objects sortable by providing // a function that can compare them public interface Sortable { public int compare(Sortable other); }
Sort.java:
// class Sort -- a class containing static sorting methods that can // sort any array of Sortable objects public class Sort { // selection sort function static void selection_sort(Sortable[] values) { int minloc; Sortable temp; int size = values.length; for (int j = 0 ; j < size - 1 ; j++) { minloc = j; for (int k = j + 1 ; k < size ; k++) if (values[k].compare(values[minloc]) < 0) minloc = k; if (minloc != j) { temp = values[minloc]; values[minloc] = values[j]; values[j] = temp; } } } // we could add other sorting methods here } // end class Sort
IntNum.java:
// class IntNum -- a class for Sortable ints public class IntNum implements Sortable { private int theNum; // field to hold the integer public IntNum() { theNum = 0; } public IntNum(int number) { theNum = number; } // compare function required by Sortable interface public int compare(Sortable another) { return ((IntNum)this).theNum - ((IntNum) another).theNum; } // allows us to print an IntNum public String toString() { return Integer.toString(theNum); } } // end class IntNum
Employee.java:
// class Employee -- a class for Employees public class Employee implements Sortable { private int SSNum; // the social security number private String firstname; private String lastname; private int salary; public Employee() { } public Employee(int SSN, String first, String last, int sal) { SSNum = SSN; firstname = first; lastname = last; salary = sal; } // compare function required by Sortable interface // orders Employees by social security number public int compare(Sortable another) { return ((Employee)this).SSNum - ((Employee) another).SSNum; } // allows us to print an Employee public String toString() { String emp; emp = Integer.toString(SSNum) + " " + lastname + " " + firstname + " " + Integer.toString(salary); return emp; } } // end class Employee
SortTest.java:
// class to show use of Sortable interface public class SortTest { public static void main(String[] args) { // create an array of IntNums, print them, sort them, and print them again IntNum[] nums = new IntNum[5]; nums[0] = new IntNum(8); nums[1] = new IntNum(3); nums[2] = new IntNum(6); nums[3] = new IntNum(1); nums[4] = new IntNum(5); System.out.println("Values before sorting:"); for (int i = 0 ; i < nums.length ; i++) System.out.println(nums[i]); Sort.selection_sort(nums); System.out.println("Values after sorting:"); for (int i = 0 ; i < nums.length ; i++) System.out.println(nums[i]); // create an array of Employees, print them, sort them, and print them again Employee[] people = new Employee[4]; people[0] = new Employee(444556666,"Shiva","Chaudhuri",75000); people[1] = new Employee(222334444,"Rosie","Rubel ",64000); people[2] = new Employee(888776666,"Xiang","Zhang ",67000); people[3] = new Employee(555667777,"Seham","Gorab ",84000); System.out.println("Values before sorting:"); for (int i = 0 ; i < people.length ; i++) System.out.println(people[i]); Sort.selection_sort(people); System.out.println("Values after sorting:"); for (int i = 0 ; i < people.length ; i++) System.out.println(people[i]); } } // end class SortTest
Output:
Values before sorting: 8 3 6 1 5 Values after sorting: 1 3 5 6 8 Values before sorting: 444556666 Chaudhuri Shiva 75000 222334444 Rubel Rosie 64000 888776666 Zhang Xiang 67000 555667777 Gorab Seham 84000 Values after sorting: 222334444 Rubel Rosie 64000 444556666 Chaudhuri Shiva 75000 555667777 Gorab Seham 84000 888776666 Zhang Xiang 67000
Email Me | Office Hours | My Home Page | Department Home | MCC Home Page