Professional Documents
Culture Documents
Prior to starting the lab, you need to pick up some files from D2L.
CircList.java the interface for the circular list ADT
LinkedCircList.java a skeleton for a linked list implementation of the circular list
Josephus.java a skeleton containing specifications for the count off method that solves the Josephus problem using
a circular list
JosephusTest.java a start at two JUnit test cases for count off
Two Java archives that you will need to add to the build path in your Eclipse project to enable JUnit test cases
After youve brought these files into a new Eclipse project, your package explorer window should look something like:
The Node.java file is a class you will need to create yourself as a node that you use in your linked list implementation.
When you add the files to the project in Eclipse, be sure to choose New interface for CircList.java and New JUnit test
case for JosephusTest.java. The two jar files are added to the project by right-clicking on the CircList project, then
choosing Properties Java Build Path Libraries Add External JARS.
Part A Implementing the circular list
The interface for this class was discussed in the slides of September 20.
/* * C i r c L i s t ADT */
public interface CircList <E > {
/* * Insert an element at the current l o c a t i o n .
@param item The element to be i n s e r t e d . */
public void insert ( E item );
/* * Remove and return the current element .
@return The element that was removed . */
public E remove ();
/* * Move the current p o s i t i o n one step c o u n t e r c l o c k w i s e .
public void prev ();
*/
You must implement this interface using a linked list developed on your own no Java arrays, ArrayLists, or LinkedLists
allowed. The skeleton you start with for the LinkedCircList class provides a toString method that is very useful for
debugging and testing. You are responsible for developing all of the other methods; clearly the code inside them will
depend on how you define your Node class.
*/
}
/* * Move the current p o s i t i o n one step c l o c k w i s e . */
public void next () {
}
/* * @return List length */
public int length () {
}
/* * @return Current element value */
public E getValue () {
}
// Not part of the interface , but a very useful method for d e b u g g i n g
// and testing
/* *
* G e n e r a t e a human - r e a d a b l e r e p r e s e n t a t i o n of this list s c o n t e n t s
* that looks like this : < 1 2 3 4 5 6 >, where each node
* on the list is printed s t a r t i n g with the current node .
*
* @return The string r e p r e s e n t a t i o n of this c i r c u l a r list
*/
public String toString ()
{
StringBuffer out = new StringBuffer ();
out . append ( " < " );
for ( int i = 0; i < length (); i ++) {
out . append ( getValue ());
out . append ( " " );
next ();
}
out . append ( " >" );
return out . toString ();
}
}
Your Node class will determine the specifics of your linked list implementation. A bonus lab point if you can convince
myself or Marcus that your technique makes both next and prev O(1) operations.
After you have the LinkedCircList class completed, it is suggested that you test it with a simple main program that you
add to the project. Then go on to Part B.
@Test
Test expected value against returned value with assert
public void test1 () {
L1 . insert ( " jim " );
L1 . insert ( " jane " );
L1 . insert ( " jack " );
L1 . insert ( " june " );
L1 . insert ( " jason " );
L1 . insert ( " jean " );
L1 . insert ( " joe " );
L1 . insert ( " jeremy " );
L1 . insert ( " janelle " );
assertEquals ( " < jason jim joe june jack jean janelle jane jeremy >" ,
Josephus . count_off ( L1 , 5));
}
@Test
public void test2 () {
L2 . insert ( " jim " );
L2 . insert ( " jane " );
L2 . insert ( " jack " );
L2 . insert ( " june " );
L2 . insert ( " jason " );
L2 . insert ( " jean " );
L2 . insert ( " joe " );
L2 . insert ( " jeremy " );
L2 . insert ( " janelle " );
assertEquals ( " < jack jean janelle june jeremy jason jane joe jim >" ,
Josephus . count_off ( L2 , 3));
}
}
To run your Junit tests, from the Run menu, choose Run As Junit tests. Or in the JosephusTest.java file window,
right click and choose Run As Junit tests. If the tests all pass, you should see:
If they dont all pass, youll be informed which tests failed. In Junit terms, a failure means the assertion did not succeed;
an error means that your code died with an exception before the assertion was even reached.