Java代写:CS142 Name Sorter

代写排序算法,需要用Swing或者JavaFX实现界面。

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<Name>, one line per name. First, trim the String. If there are no spaces, treat everything as the last name. If there are any spaces, the token after the last space is the last name (with everything else as the first name). Trim both Strings. The program should immediately close the file, and create a new array of type Name whose size is the number of names from the file (minus any duplicates, if you are doing the extra credit). The ArrayList should be copied into the array.

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

Extra Credit: 5 points

If you get the program together and still have the energy, you may gain up to 5 points extra credit.

  • 5 points: Simply make it so that the program removes all duplicate entries from list. You can use Name’s overriding equals method to test for duplication.

Deliverables:

Electronic:

  • All .class, .jar, .html (javadocs) and .java files. The project will not be graded if source files are missing.
  • Sample Output (as .rtf – run the program, invert colors, copy the window using [Alt|PrtScn],, open Wordpad, paste, save.)
  • A simple test plan including explanations of any discrepancies and reasons for each test. Show actual input and ALL values output as well as ALL expected output. Test each possible action. Save as .xls, xlsx, .doc or .docx file
  • Zip all of the above files together. Do not use rar or any archive format other than zip. Rename the file: “[YourName]_p3.zip”.
  • Submit this single zip file by going to Canvas, select this class, select the Assignment tab on the left, select the Assignment 3, select the submission tab at the top, find the file, and Submit.