Background
If we have a list of names, it is sometimes nice to see them in sorted order by first (given) name, and other times by last (surname). So let’s write a program which will read in a text file of names, turning each line into Name objects, putting those into an array sorting the array by the selected field, and writing the reservations to the screen, and if desired, back out to a text file.
In order to do this, we will need to create three new classes:
NameSort
Contains main(). This should be a GUI based program. It can be created using Swing (possibly using the Mattisse builder built into NetBeans) or using JavaFX or JavaFXM with Scene Builder.
The program starts with no file selected, with a label listing the file as none. There should be a menu option to open a file. This should bring up a file chooser. If the file does not exist or is not readable, a message should pop up saying so. If the file can be, it should be opened, and the contents of the file should be added into an ArrayList
The program should then sort the array using the Optimized quicksort version described in class, a method which you have defined in the Sorts class. It will sort by first or last name depending on which RadioButton is selected. The sorted names should be written out to the TextArea.
A File/save menu option should save the sorted array as a text file which has the same name as the text file read in, except that it has had any extension removed and ends in “_fn.txt” or “_ln.txt”, depending on which way it is sorted.
Name
Implements Serializable, Cloneable and Comparable
Should have the following private instance variables:
- a String to hold a first name
a String to hold a last name
Should have the following private static variable:a static String to keep track of which field is the selected field for sorting and printing.
Should have at least the following methods:a constructor with two String parameters (first and last name) have the last name be the default selected field.
- A static mutator for the selected field.
- the overriding methods:
- toString – returns a String in the format “Last, First” if last is the selected field; “First Last” otherwise.
equals - first and last name must be equal in order to return a true - compareTo – compares first name, then last name if first is the selected field; last name, then first name otherwise. returns -1 if the current object comes before, 0 if equal, 1 if it comes after. Utilizes the compareToIgnoreCase method of the String properties. throws a ClassCastException if the two objects are not both Names.
- Clone - returns a copy of the name.
Sorts
Must contain at least the methods public static void
quickSort(Comparable[] array)
throws a java.lang.ArrayIndexOutOfBoundsException if indices not in array which merely calls quickSort(array, 0, array.length - 1) (see below)
public static void quickSort(Comparable[] array, int from, int to)
throws a java.lang.ArrayIndexOutOfBoundsException if indices not in array which implements the Optimized quick sort algorithm as presented in class, utilizing the following methods:
public static void insertionSort(Comparable[] array, int from, int to)
throws a java.lang.ArrayIndexOutOfBoundsException if indices not in array
private static int partition(Comparable[] array, int from, int to)
throws a java.lang.ArrayIndexOutOfBoundsException if indices not in array which sets a midpoint, calls sortFirstMiddleLast, moves data around the pivot value, and returns the pivot index
private static void swap(Comparable[] array, int from, int to)
throws a java.lang.ArrayIndexOutOfBoundsException if indices not in array
private static void sortFirstMiddleLast(Comparable[] array, int from, int mid, int to)
throws a java.lang.ArrayIndexOutOfBoundsException if indices not in array
Points Possible: 100
A sample executable jar is located on Canvas