import java.util.Arrays; /* Java application that illustrates how to make use of classes that ** are instantiations of the generic class Sorter. Specifically, ** it takes its command line arguments and sorts them twice, once ** using an instance of StringSorterLexico and once using an instance ** of StringSorterLenLexico. (The same variable, of type Sorter, ** is used in each case, illustrating how a variable of type Sorter ** can, at different points in time, refer to objects from different ** classes, as long as those classes are instantiations of Sorter.) ** ** Then it sorts an array containing the lengths of its command line ** arguments and sorts those using an instance of IntSorterAbsVal. */ public class SorterClient { public static void main(String[] args) { // Display the command line arguments. System.out.println("Original array: " + Arrays.toString(args)); // Declare a variable of type Sorter. Such a variable // can refer to an object of any descendant class of Sorter that // instantiates it by the data type String. Sorter strSorter; // Assign to sorter a new StringSorterLexico object and have it // sort a clone of args[]. strSorter = new StringSorterLexico(); String[] argsClone = args.clone(); strSorter.sort(argsClone); // Display the sorted command line arguments (which will be // in ascending lexicograph order). System.out.println("After sorting into lexicographic order: " + Arrays.toString(argsClone)); // Now assign to sorter a new StringSorterLenLexico object and // have it sort args[]. strSorter = new StringSorterLenLexico(); argsClone = args.clone(); strSorter.sort(argsClone); // Display the sorted command line arguments (which will be // in ascending length-lexicographic order). System.out.println("After sorting into length-lexicographic order: " + Arrays.toString(argsClone)); // Create an array whose values correspond to the lengths of // the command line arguments. Integer[] argLengths = new Integer[args.length]; for (int i = 0; i != argLengths.length; i++) { argLengths[i] = args[i].length(); } // Display the array. System.out.println("Original argsLength array: " + Arrays.toString(argLengths)); // Create an object that can sort an array of type Integer[] // according to the absolute values of the elements. Sorter intSorter = new IntSorterAbsVal(); intSorter.sort(argLengths); // Display the sorted array. System.out.println("After sorting argsLength: " + Arrays.toString(argLengths)); } }