



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for a java programming assignment focused on implementing an integer list abstract data type (adt) and shuffling lists using permutations. Students are required to write a java program with two files: shuffle.java and list.java. The client module, shuffle.java, uses the list adt to apply permutations and shuffle lists. The list adt is a double-ended queue with a current-position marker. Students must read permutations from an input file, shuffle the list using the given permutation, and print the arrangement representation of the permutation along with its order in the output file.
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Algorithms and Abstract Data Types Summer 2006
Introduction The purpose of this assignment is threefold: to make sure everyone is up to speed with Java, to practice modularity and ADTs, and to implement an Integer List ADT which will be used (perhaps with minor modifications) in future programming assignments. You should therefore test your ADT carefully, even though all of its features may not be used here.
In this project you will write a Java program which performs shuffles (i.e. permutations) on lists of integers. Observe that there are many ways to shuffle a given list of integers. For example the list (1 2 3) can be shuffled in 6 distinct ways: (1 2 3), (1 3 2), (2 1 3), (2 3 1), (3 1 2), (3 2 1). In general a list of length n has n! arrangements or permutations. Formally, a permutation of a set S is a bijection (i.e. a one- to-one onto mapping) from S to S. From now on we take S to be the set S = { 1 , 2 ,, n }. One way to
denote a permutation of S is to simply list its elements twice, side by side, showing the image of each element under the permutation. For instance, let n = 5 and write
in this case that
Composing again we see (^)
In this project you will compute the order of a permutation by applying it repeatedly to a list (i.e. shuffling the list) until the list is brought back into its original order.
Our notation for permutations is still somewhat cumbersome. If we fix an ordering of S (such as
of S is understood to denote the permutation which takes the standard arrangement (i.e. the one in increasing order) into the given arrangement. We will call this method for specifying permutations the arrangement representation.
There is yet another way to specify a permutation of S = { 1 ,, n }as a list of n integers. In this notation a
list is interpreted to be a set of instructions for transforming the standard arrangement of S into a given
arrangement. To distinguish this representation from the arrangement representation, we will surround the list with square brackets [ ] rather than round brackets ( ). We refer to this scheme as the operator representation of a permutation, and illustrate with the following example. The list [1 2 1] instructs us to place elements from the standard arrangement (1 2 3) into an initially empty list ( ) in three steps:
The result is the arrangement (3 1 2). Similarly [1 1 3] applied to (1 2 3) gives (2 1 3). Below are the 6 permutations of S = {1, 2, 3} written in both operator and arrangement representation.
[1 2 3] (1 2 3) [1 2 2] (1 3 2) [1 1 3] (2 1 3) [1 1 2] (2 3 1) [1 2 1] (3 1 2) [1 1 1] (3 2 1)
Observe that in the operator representation of a permutation, the integer at position i must be in the range 1 to i , since it is literally an instruction to insert something into a list of length i − 1. Thus [1 3 2] is not a valid operator representation of any permutation. (What would it say? Starting with an empty list, insert 1 into position 1 to get the list (1), then insert 2 into position 3 to get …? But inserting anything into the list (1) results in a list of length 2, which therefore has no position 3.) One nice thing about the operator representation of a permutation is that it can be easily applied to any list of length n , not just the standard arrangement of S. For instance [1 2 2] applied to (a b c) gives (a c b), and [1 1 1] applied to (x y z) gives (z y x). The reader is urged at this point to write out all 24 permutations of S = {1, 2, 3, 4} in both
Program Operation Your program will be structured in two files: a client module called Shuffle.java, and a List ADT module called List.java. Each file will contain one top level class, Shuffle and List, respectively. The client Shuffle will use List variables in two ways. On the one hand, a List will represent a permutation in operator representation. Such a List will itself be made to operate on a second List by splicing and dicing according to the ‘instructions’ in the first List. These shuffling operations will be performed in the client by calling the methods in the List module. Shuffle will be invoked at the command line by doing: Shuffle input_file output_file. Notice that one does not type java Shuffle at the command line. A Makefile is included at the end of this handout which places all .class files for this project in an executable jar file called Shuffle, making it possible to leave out java when invoking the program.
The input file will contain a number of permutations (of various sizes) in operator representation. For each such permutation your program will do the following.
int getLast() // Returns last element. Pre: !isEmpty(). int getCurrent() // Returns current element. Pre: !isEmpty(), !offEnd(). int getLength() // Returns length of this List. boolean equals(List L) // Returns true if this List has same elements as L in the // same order. Ignores the current marker in both Lists.
// Manipulation Procedures void makeEmpty() // Sets this List to the empty state. Post: isEmpty(). void moveFirst() // Sets current marker to first element. // Pre: !isEmpty(); Post: !offEnd(). void moveLast() // Sets current marker to last element // Pre !isEmpty(); Post: !offEnd(). void movePrev() // Moves current marker one step toward first element. // Pre: !isEmpty(), !offEnd(). void moveNext() // Moves current marker one step toward last element. // Pre: !isEmpty(), !offEnd(). void insertBeforeFirst(int data) // Inserts new element before first element. // Post: !isEmpty(). void insertAfterLast(int data) // Inserts new element after last element. // Post: !isEmpty(). void insertBeforeCurrent(int data) // Inserts new element before current element. // Pre: !isEmpty(), !offEnd(). void insertAfterCurrent(int data) // Inserts new element after current element. // Pre: !isEmpty(), !offEnd(). void deleteFirst() // Deletes first element. Pre: !isEmpty(). void deleteLast() // Deletes last element. Pre: !isEmpty(). void deleteCurrent() // Deletes current element. // Pre: !isEmpty(), !offEnd(); Post: offEnd()
// Other methods List copy() // Returns a new list which contains the same elements as this List, and // in the same order. The current marker in the new list is undefined, // regardless of the state of the current marker in this list. The state // of this list is unchanged. public String toString() // Overrides Object's toString method and returns a string // representation of this List consisting of a space // separated list of integers. public static void main(String[] args) // Used as a test driver for the List class.
The above operations are required for full credit, although it is not expected that all will be used by the client module in this project. The following operation is optional, and may come in handy in some future assignment:
List cat(List L) // Returns a new List which is the concatenation of this list // followed by L. The current marker in the new list is undefined, // regardless of the states of the current markers in the two lists. // The states of the two Lists are unchanged.
Your List class should contain a private Node class which encapsulates one List element. This private class should contain fields for an int (the value stored at that node), a Node (the previous element in the list), and another Node (the next element in the list). It should also define an appropriate constructor, as well as a toString() method. The List class should contain private fields of type Node which refer to the first, last, and current Nodes in the List.
Makefile The following Makefile creates an executable jar file called Shuffle. Place it in a directory containing List.java and Shuffle.java, then type gmake to compile your program.
MAINCLASS = Shuffle JAVAC = javac JAVASRC = $(wildcard .java) SOURCES = $(JAVASRC) makefile README CLASSES = $(patsubst %.java, %.class, $(JAVASRC)) JARCLASSES = $(patsubst %.class, %.class, $(CLASSES)) JARFILE = $(MAINCLASS)
all: $(JARFILE)
$(JARFILE): $(CLASSES) echo Main-class: $(MAINCLASS) > Manifest jar cvfm $(JARFILE) Manifest $(JARCLASSES) chmod +x $(JARFILE) rm Manifest
%.class: %.java $(JAVAC) $<
clean: rm *.class $(JARFILE)
Note that this Makefile will compile all .java files in your current working directory. Also be aware that if you are using the bash shell and you type make (instead of gmake), this makefile may not work properly. To be safe always use gmake. You may of course alter this Makefile as you see fit to perform other tasks (such as submit), but the Makefile your turn in must make an executable jar called Shuffle, and must include a clean utility.
You must also submit a README file for this (and every) assignment describing the files created for the assignment, their purposes and relationships, along with any special notes to myself and the grader. README is essentially a table of contents for the project. Each file you turn in must begin with your name, user id, and assignment name. Thus, for this project you are to submit four files in all: List.java, Shuffle.java, Makefile, and README.
Advice Start early and ask questions if anything is unclear. It is helpful to write simple test programs to make sure you understand each part of the problem. The main method in your List class is required because it is much easier to debug your List ADT in isolation before you use it in the Shuffle class. You should first design and build your List ADT, test it thoroughly, and only then start coding your Shuffle class. Information on how to turn in your program is posted on the webpage.