Computer Science Tripos Part 2 (General)

Further Java

Assessed exercise 3

Another electronic die

Outline

Following the enormous success of its electronic die in the Christmas market, a company specialising in hi-tech trinkets for overpaid executives has decided to launch an on-line version. This will consist of a Java applet displaying the conventional pattern for a die in a Web browser, like this:

Seven spots on a die
(This is just a picture of the screen; Java should be enabled to run the applet itself.)

Whenever the mouse is clicked over the die, the pattern will cycle through the numbers one to six at high speed, stopping after a random period.

The problem

The exercise is to write this applet, which should be controlled by three integer parameters in addition to the usual width and height:

NameTypeDescriptionDefault
minintMinimum roll time in seconds1
maxintMaximum roll time in seconds4
frequencyintNumber of changes per second while rolling7

Details

The applet should use the Java 1.1 input model for handling mouse events and should also be capable of being run as a free-standing program. (If you are trying to run the applet above from an old Web browser, you may notice that it gives rise to security violations because they do not support the new event model.)

The screen cannot be updated while the mouse handler is running, so it will also be necessary for the mouse handler to start a separate thread to animate the screen image. This can call its own wait method with a long integer argument specifying a time-out in milliseconds to pause for a given elapsed time between updates.

The paint method should arrange to fill the Applet's working rectangle with the picture of the die. Again, it should use the Java 1.1 getBounds method to determine this.

Documentation of the JDK libraries is available on-line. The drawArc and fillArc methods of a Graphics object in the java.awt package will be useful for drawing spots on the die. The static random method of the Math class in the java.lang package returns a double length, floating-point pseudo-random number between 0.0 and 1.0.

Documentation

A report should be submitted describing the program. This should consist of the full source code of the program, suitably annotated with comments. You should also indicate a Web address on Thor (using the public_html facility) where a working version of the program can be tested. This will include the HTML and class files but need not include the source code.

Deadline and oral examination

The report on the completed exercise must be handed to Ms Jenni Cartwright in Room 201 of the Computer Laboratory Arup Building by noon on Friday 26 February 1999. Interviews with an assessor will be held on the afternoon of Friday 5 March 1999.

Peter Robinson
December 1998