CS 51 Test Program #1
Due: Friday, October 13, 2006 at 4 PM

A test program is a laboratory that you complete on your own, without the help of others. It is a form of take-home exam. You may consult your text, your notes, your lab work, or our on-line examples and web pages, but use of any other source for code is forbidden. You may not discuss these problems with anyone aside from the course instructor. You may only ask the TA's for help with hardware problems or difficulties in retrieving your program from a disk or network.

Complete each of the following problems, documenting your code well. You are encouraged to reuse the code from your labs or our class examples. Submit your code in the usual way by dragging it into the Dropoff folder for your section. Please do not submit three separate folders. Instead, place the folders for all three of your complete programs into one folder, make sure that your name appears in the title of the main folder and each of the subfolders, and then place the main folder in our dropoff folder.

Problem 1: No Exit

For the first problem, you will draw a FramedRect. The rectangle should be 200 pixels wide and 300 pixels tall. Within this rectangle, you will draw a smaller FilledOval. The oval is a circle with a diameter of 50 pixels. If the user presses the mouse on the circle, s/he can move the circle by dragging it around. One catch, however, is that if the ball hits the wall, it will be dropped by the user. Dragging should only be restarted if the user presses the mouse button on the circle and starts dragging again.

A demo version should appear below if your web browser supports Java.



Problem 2: Optical Illusion

The diagram below is an example of an optical illusion. When you look at it you will probably see gray shadows at the intersections of the lines running between the black squares even though those areas are actually white.

For this problem we would like you to design a class, Boxes, to create and display a picture like that shown above in the drawing window with 6 rows and 6 columns of square boxes.

You will also need a class extending WindowController to control interaction with the boxes. When the program starts, two sets of boxes should be drawn on the screen. The first should start at (5,10), have a box size of 20 and a gap of 4. The other should start at (195,195), have a box size of 30, and a gap of 2.

The Boxes class should also support methods with signatures:

    public void showBackground(boolean value)
    
    public boolean contains(Location point)

When the first method is invoked with a parameter of true, it should result in showing a red background under the collection of 36 squares generated by the class. That is, it shows a single red FilledRect which forms a background for the 6 x 6 array of squares. The background rectangle should enclose all of the squares, but extend beyond the edges by gap around each edge. When the parameter is false, it should hide the red background.

The second method above returns either true or false depending on whether point is contained in the FilledRect which comprises the background.

Your class extending WindowController should use these methods so that when a user presses the mouse button down anywhere inside one of the collections of squares, the red background is displayed. When the mouse is released, the red background is hidden again. It does not matter where or whether the mouse is moved in between when it is pressed and released. The behavior is determined only by where it is when the mouse is pressed.

A demo version should appear below if your web browser supports Java.



Problem 3. Ball Toss

For this problem, we would like you to write a program that simulates a ball toss game at a fair. One such game might be a game in which you have to knock over a bunch of cans with a ball. Of course, there's always a trick to these games that makes them virtually impossible to win. We won't make our game impossible, but we'll make it tricky. In our game, there will be a single target that will move across the screen from left to right and then back again. The user will have to toss the ball from behind a line that is far away from the moving object.

This game will involve implementing three classes: one for the user interface, one for the target, and one for the balls that will be tossed. Both the target and the ball will be ActiveObjects. The three classes will be BallToss, Target, and Ball.

Upon startup, a playing area is displayed and a target begins to move across the screen. I've chosen to make the target a bell, for which a gif file is provided, but you can choose to make it a rectangle, an oval, or any other two-dimensional object. If the target reaches the end of the playing field without being hit, it "bounces" off the wall of the playing field and then moves in the opposite direction.

The user clicks to throw a ball. Provided that the click is within the legal throwing area, the ball should move from the point of the click. If the ball hits the target, the target should disappear and be replaced by a message indicating that the user has won. As long as the target has not been hit, the user can continue to toss balls. The game ends when the user hits the target.

A demo version should appear below if your web browser supports Java.



Grading Point Allocations

Value Feature
Style (16 pts for each of 3 programs)
2 pts. use of boolean conditions
2 pts. ifs/whiles
2 pts. appropriate vble (instance/local, public/private)
2 pts. Descriptive comments
2 pts. Good names
2 pts. Good use of constants
2 pts. Appropriate formatting
2 pts. Parameters used appropriately
Correctness (16 pts for each of 3 programs)
No Exit
4 pts. Drawing the screen initially
4 pts. Dragging
4 pts. Not allowing the ball to leave the rectangle
4 pts. Ending the drag when the ball touches the rectangle
Optical illusion
4 pts. Drawing a grid
4 pts. Drawing 2nd grid with different dimensions
4 pts. Showing red background on press
4 pts. Hiding red background on release
Ball toss
5 pts. Target moves back and forth
5 pts. Ball thrown when clicked in right space
4 pts. Program notices when ball hits target
2 pts. Appropriate messages displayed
Miscellaneous (4 pts total)
Extra Credit (4 pts maximum)