CSCI 136: Assignment 2 -- Josephus

Due 2/22/98


Your next program 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 bloodier. This is the PG version!

In the picture below, the green rectangles represent people still in the circle, while the black rectangles represent people still under consideration. The red rectangles represent the people who are counting off.

Fill in the appropriate numbers and then press the "start" button to begin.



I have provided you with code for the main program, which sets up the applet graphics, as well as the code which counts off the people and determines the messenger. All that you have to do is provide the code for the data structure which handles all of the people (actually it handles the rectangles representing the people, but ...). My program presumes that you provide a class named CircularVector which implements the interface Circular which I have provided for you. Thus you must provide the implementation of the class CircularVector.

Your implementation should include two instance variables: list, a vector to hold all survivors, and current, an integer which specifies which element of the vector corresponds to the person in the circle who is 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 person (if the current person 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).

One word of warning, the Metrowerks applet viewer seems to have a bug which causes it to hiccup occasionally (and unpredictably) during execution of this program and give the wrong answer. The keyword "synchronized" added to the beginning of each method seems to keep this from happening quite as often (it really should be necessary only with parallel programs with several threads executing concurrently). If you get this strange hiccup, run the program again (a slower speed may help) and it should go away. If it repeats consistently, it is a bug in your program! None of the browsers seems to have this bug, so if in doubt open your ".html" file with Internet Explorer or Netscape.