IUP Computer Science
COSC 310    Fall 2006

Project #3
(Disk Sorter)
(Due 13 October 2006)

You have been hired by a manufacturing company to write a program to control their disk sorter.  This is a device that works with round, flat objects (generically referred to as disks) and mechanically sorts them.  (The disks are manufacturing components that are used in various places within the company.)  The collection of disks to be sorted is the "pile" and the purpose of the sorter is to perform a series of actions on the pile to cause them to become "standardized".  A standardized pile has all the disks of the largest diameter on the bottom and above them all of the disks of the next larger size and above them all disks of the next larger size, etc. until all the smallest disks are at the top of the pile.

Before the company will let you program the sorter, you need to demonstrate that your algorithm for the sorter will work.  To do the demonstration, you are required to create a class with methods that reflect the actions which the sorter is capable of.  To help you, the company has provided the following interface which your class must implement.

public interface SorterActions {
        // The sorter can pick up a group of disks of various sizes from a
        //  bin of disks.  The input method should simulate this by
        //  prompting the person at the keyboard for a sequence of
        //  characters that represents the group of disks; each character in
        //  the sequence represents one disk of a particular size.
    public void input ();
        // The sorter can place a group of disks that it has picked up onto
        //  the pile that it is forming on the "adjuster plate."  The add
        //  method should regard the group just entered as a pile that is to
        //  be placed on top of any disks already on the main pile
        //  (simulating the adjuster plate).
    public void add (Pile more);
        // A disk is out-of-order if it is immediately on top of a disk
        //  which is smaller than it.  The sorter can find the disk of the same
        //  size as test which is highest (nearest the top) in the pile and is
        //  out of order.  The highestOutOfOrder method must return the
        //  position of this disk within the pile.
    public int highestOutOfOrder (Disk test);
        // The lowestOutOfOrder method must return the position of the disk
        //  with the same size as test which is lowest in the pile and out of
        //  order.
    public int lowestOutOfOrder (Disk test);
        // The sorter can determine which disk is of a smaller size than test
        //  and is lowest in the pile.  The lowestSmaller method must return
        //  the position of this disk.
    public int lowestSmaller (Disk test);
        // The sorter can pick any number of disks up from the top of the
        //  pile down to a specified position, flip the entire group of disks
        //  over and place them back on top of the pile.  The flip method
        //  should do the same with some group of disks on top of the pile.
    public void flip (int position);
        // After sorting, the sorter places the entire pile on a pallette for
        //  distribution to other areas of the company. The display method
        //  must display the names of the disks on the pile from top to bottom
        //  to show that it worked
    public void display ();
}

Every disk has a name, a designator (in simulation, this is the first letter of its name - all are unique), and a relative size.  Because the disk sorter must be able to work with a wide variety of disks, it must be able to accept a specification of what the different disks are before it starts a sorting task.  In the simulation, a file of disk names provides this information (the file's name is  DefineDisks.txt ).  For illustration purposes, throughout the rest of this description I will assume the disks are coins (penny, nickel, dime, quarter, half, and dollar) so we have something concrete to use in the description.  Then, the file would contain one line looking like this:

dime penny nickel quarter half $dollar

These are the names for the disks; their first characters are the designators (note what was done so dollar doesn't conflict with dime); and their order in the file is always from smallest to largest in size.  The program must read this file and set up to sort whatever disks are specified.  The company guarantees that there are never more than 12 different disks in a bin.

The simulation test environment is as follows.  Your program should read the DefineDisks.txt file and then repeatedly prompt the person at the keyboard to enter a sequence of designators representing a group of disks.  The program should use input to get the designators and add to place them on the pile.  When the person at the keyboard indicates there are no more disks to be placed on the pile, the program should use the other methods to standardize the pile.  The other methods (except display) represent the only actions the disk sorter can perform.  After the standardization, the program should display the pile by listing vertically the names of the disks on the pile, top to bottom.

Example, suppose the person at the keyboard enters:
qnpn            indicating quarter, nickel, penny, nickel with the quarter on the bottom
then the person enters     pq$dp$        and then enters      ddnpp
and finally indicates there is no more input.

After standardizing, the program should display:
dime
dime
dime
penny
penny
penny
penny
penny
nickel
nickel
nickel
quarter
quarter
$dollar
$dollar

Note that although the above demonstration was for coins.  If the DefineDisks.txt file is changed and contains a sequence of lid sizes (margarine, icing, cottage-cheese, nuts, etc.) and the person at the keyboard entered combinations of m, i, c, n, etc. the program must work with no changes.  Also, the demonstration is a little unrealistic in that the sorter can actually pick up several dozen disks on each input, rather than just a few.  But, if your program works with small samples, it ought to work for the real thing.

Requirements:
Your program is required to do the following:

  1. It must throw an exception if any of the characters input from the keyboard are improper designators.  The exception must be caught outside of the class containing the input method.
  2. It must use an ArrayList to hold the disks that are in a pile.
  3. The method which interacts with the person at the keyboard and the method that causes the standardization of the pile must be outside the class which implements SorterActions.
In addition, you are required to do an algorithm analysis of the standardization method.  Attach this analysis to the printout of the program that you hand in.  The analysis should regard the use of   flip, lowestSmaller, lowestOutOfOrder and highestOutOfOrder as single steps.  The analysis should determine  T(N) for N disks being standardized, worst case.

Hand in the printout of your program and the captured output from the console window in which at least three groups are specified through the keyboard and there are at least 20 disks overall.  Note that with the printout, you must include the analysis.  Copy the .java files for your project to a folder in your named folder on the P drive.