CS 136 Assignment 2

Description

Your next assignment is to solve the "Josephus" problem. A messenger is to be chosen from a group of people. Since the mission is dangerous, the messenger is to be chosen randomly. The technique chosen is to arrange everyone in a circle. Pick a number n. Now count off n people, and remove the n+1st from the circle. Continue counting off until only 1 person is left. That person is the messenger. The original "historically correct" version is quite sad.

There are two parts to this assignment.

First, you will need to provide the code for the data structure which handles all of the people. To do this, you will need to provide a class named CircularVector which implements the interface Circular, which I have provided for you.

Your implementation should include two instance variables: list, a vector to represent the circle, and current, an integer that specifies which element of the vector corresponds to the object in the circle currently being pointed at. Virtually all of the operations of the class are specified relative to the current element. For example next() moves the current element to refer to the next object (if the current object is last in the vector, then current must shift to the element at the beginning of the list: 0). The other operations should be clear from their names and the pre and post-conditions given for them. Notice that it is quite important to notice what happens to the current indicator after each operation. For example, after removeCurrent(), the current element becomes the element after the one removed (unless the list becomes empty, in which case there is no current element - indicated by setting current to -1).

Once you have implemented (and thoroughly tested) the CircularVector class, you will implement a class called Josephus, which will run a simulation of the Josephus problem. The class should provide a constructor that should allow a user to specify the size of the Josephus circle. The size of the circle should be > 0.

The class should also provide the following public methods:

   public void reInit()
   // Pre:   numMessengers in circle is > 0
   // Post:  circle of messengers is set up, with messengers named "1" to
   //        "numMessengers"

   public void findMessenger(int skipNum); 
   // Pre:   skipNum >= 0.
   // Post:  Messenger selected has been displayed on the screen; those
   //  not selected have had their names printed in the order in which they
   //  were excluded.

Goals

One goal of this assignment is to gain experience with the Vector class. In addition, you will gain experience in building a useful data structure that can be treated as a utility by others. Be sure your implementation of CircularVector is general. You are building it in order to make use of it when implementing the Josephus class. But the structure is a general one that can be used in many contexts.

Plan your work carefully for this lab so that you can complete part one and begin part two in the lab. For this lab I will be checking design documents. Since I have provided classes with method headers for you, your design should consist of pseudocode for the method bodies.

Getting Started

In the CS 136 Assignments folder on Cider Press, you'll find a demo version of the Josephus program. Try it out. It sets up a number of Josephus circles and counts off to find messengers in each one. At the end of all the runs you'll find an error message. It's supposed to be there. I wanted to test my code to be sure it worked properly when I asked to skip a negative number of messengers when counting off.

In the same folder, you'll also find a pair of starter folders: one for the CircularVector class and one for the Josephus class. Copy these, and fill in the details. These folders contain the appropriate references to Java libraries. They also contain the Circular interface. You will need to provide the details for CircularVector.java and for Josephus.java. Before you test your Josephus class, be sure to add your definition of CircularVector to the Josephus project.

Naming Conventions

This is a reminder that classes requested for assignments should always be named as in the assignment. This is necessary for our testing of your work. Also, Java programmers tend to follow certain naming conventions. Constants are all upper-case, classes begin with upper-case, variable and method names begin with lower case, using upper-case for multi-word names. You should also follow these conventions. Finally, names help the reader understand what you have written.

What to hand in

The program is due Sunday, February 20 for those in the Wednesday lab; Monday, February 21 for those in the Thursday lab. Programs are handed in by putting a folder, whose name is your last name, into the CS136 drop-off folder on Cider Press. The folder should contain the Metrowerks project folders for your CircularVector and Josephus classes.

Back to:

  • CS 136 home page
  • Andrea Danyluk's home page
  • CS Department home page
  • andrea@cs.williams.edu