You are on page 1of 4

CS 271 Structures Lab Exercise 3

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 ();

*/

/* * 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 The number of e l e m e n t s in the list . */
public int length ();
/* * @return The current element . */
public E getValue ();
}

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.

class LinkedCircList <E > implements CircList <E > {


// Declare data members here
/* * C o n s t r u c t o r */
LinkedCircList () {
}
/* * Insert " it " at current p o s i t i o n */
public void insert ( E it ) {
}
/* * Remove and return current element */
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 () {

*/

}
/* * 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.

Part B Use the LinkedCircList to develop a solution of the Josephus problem


public class Josephus {
// Given a c i r c u l a r list of people s names and a skip factor ,
// return a string r e p r e s e n t a t i o n of the order in which people are
// removed from the list . For example , if t h e _ c i r c l e was the list :
//
// janelle jim jane jack june jason jean joe jeremy
//
// and skip was 5 , then the string r e t u r n e d must be p r e c i s e l y one space
//
// < jason jim joe june jack jean janelle jane jeremy >
//
// Note the opening and closing angle b r a c k e t s . All spacing between items
// is p r e c i s e l y one space .
Not a d h e r i n g to this format will result in failing
// JUnit test cases even though the people in your answer may be in the right
// order .
public static String count_off ( LinkedCircList < String > the_circle , int skip ) {
StringBuffer out = new StringBuffer ();
// Fill out c o r r e c t l y ...
// And then return it
return out . toString ();
}
}

Part C JUnit testing your Josephus solution


To test your Josephus solution, you will learn about and use JUnit testing. Heres a start you need to add more test
cases to make sure your solution is correct. How many more? Enough to ensure that, when I release my test cases in a
week, that they wont break your code!
import static org . junit . Assert .*;
import org . junit . Before ;
import org . junit . Test ;
public class JosephusTest {
private LinkedCircList < String > L1 ;
private LinkedCircList < String > L2 ;
@Before
public void setUp () throws Exception {
L1 = new LinkedCircList < String >();
L2 = new LinkedCircList < String >();
}

setUp is called before each method with a Test annotation

@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.

You might also like