COP 3530 Section U03 package spring09generics; The Triple Generic Class ======================== I. The class package generics2; public class Triple < E extends Comparable > { // the fields private E first; private E second; private E third; /** Creates a new instance of Triple with the parameters item1, item2, item3 */ public Triple(E item1, E item2, E item3) { first = item1; second = item2; third = item3; } // sort first, second, and third such that the smallest is in first, the in between // in second and the largest is in third public void sort() { // if first > second swap them if (first.compareTo(second) > 0) { // swap them E temp = first; first = second; second = temp; } // if second > third, swap them if (second.compareTo(third) > 0) { // swap them E temp = second; second = third; third = temp; } // if first > second, swap them if (first.compareTo(second) > 0) { // swap them E temp = first; first = second; second = temp; } } // get the first field public E getFirst() { return first; } // get the second field public E getSecond() { return second; } // get the third field public E getThird() { return third; } } II. The driver package generics2; public class Generics2 { // use Triple to sort 3 name and 3 integers public static void main(String[] args) { // sort 3 names Triple names = new Triple("Mark","John","Bill"); names.sort(); System.out.println("The sorted names are : " + names.getFirst() + ", " + names.getSecond() + ", " + names.getThird() + "."); // now sort 3 integers Triple ints = new Triple<>(5, 3, 4); ints.sort(); System.out.println("The sorted integers are: " + ints.getFirst() + ", " + ints.getSecond() + ", " + ints.getThird() + "."); } } III. The output: run: The sorted names are : Bill, John, Mark. The sorted integers are: 3, 4, 5. BUILD SUCCESSFUL (total time: 1 second)