CS 51 Test Program #1
Due: Friday, March 2, 2007, 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 dropbox folder. 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 dropbox folder.

Problem 1: RaceTrack

For this first problem you will draw a square racetrack around which the user can drag a black circle with the goal of getting it to overlap a red target. However, if the black circle ever touches a wall of the racetrack, it will be reset to its original location.

A running version of the program we hope you produce is provided below (if your browser handles applets correctly):



The applet window should be 400 pixels wide and 400 pixels tall. Centered within the window you will draw a FramedRect that is 300 pixels wide and 300 pixels tall. Note that this describes a square racetrack that is 50 pixels wide.

There should also be a filled black circle with a diameter of 30 pixels in the upper left hand corner of the racetrack, and a filled red circle of the same size in the lower right hand corner. If the user presses the mouse on the black circle, they can move it by dragging it around. If they drag the black circle and release it when the red and black circles overlap, a message saying "Got it!" should be displayed in the middle of the window, and the black circle should be reset to the upper left hand corner. The "Got it!" message should disappear when the user next presses the mouse on the black circle.

Notice that if, while dragging the black circle, it touches either the inner or outer wall of the racetrack, the black circle should reset to the original corner.

Problem 2: Follow the Leader

Kids love to follow each other around. For this question, you should write a program displaying three children holding hands. One of the children is the leader. The other two children should follow the leader. The leader moves in the direction of the mouse when the mouse is clicked. The second child should then move to the leader's previous location and the third child should move to where the second child was.

The leader only moves left, right, up, or down, never diagonally. The children should not move if the mouse is clicked in any diagonal direction. When moving left and right, a child moves a complete body width (32 pixels). When moving up and down, a child moves a complete body height (48 pixels). The leader is polite. So if you click the mouse in a direction that would cause the leader to step on the second child, the leader does not move.

The children don't like being pushed around, either. If you press the mouse on any of the children, the leader says "Hey!" by displaying the "Hey!" message to the right of the leader. When you release the mouse, the message should go away.

This question tests your ability to define classes. Thus you should include a class Children that represents the group of three children. It will include instance variables for the VisibleImages representing the three children, a constructor, and the following methods. A method moveToward to move the children (similar to the hopToward method of the Frog class), a contains method to determine if the user has clicked on any of the children, and a shoutHey method that triggers the display of the "Hey" message.

We provide four images of children: girl1.gif, girl2.gif, boy1.gif, boy2.gif. They are all the same size. You can decide which images to use, but we recommend that you pick one to be the leader and another to be the other two children. This will make it easy for you to remember which one is the leader so you can be sure the children are moving correctly.

Your applet should be 400 pixels wide by 400 pixels tall.

A running version of the program should be visible below if your browser handles applets correctly:



You may notice that when the leader says "Hey!", the message is always to the right of the leader. This means that if the leader is at the right edge of the canvas, the message will not be seen. Also, if the second child is to the right of the leader, the message will be on top of that child. For extra credit, make sure that the "Hey!" message is always visible and not on top of the second child.

Problem 3: Bubble Blower

Your last test program will be a simple program that blows bubbles and then allows them to float off the screen. When the user first presses the mouse button a small bubble is drawn on the screen that is centered around the mouse location. While the mouse continues to be held down, the bubble should slowly grow in diameter, while remaining centered at the mouse's location.

When the user releases the mouse button the bubble should stop growing and should slowly float up the screen. When the top of the bubble hits the top of the window, the bubble should pop. You should accomplish this by removing the framed oval from the window and just leaving the word "pop" where the center of the bubble was when it popped. The "pop" should disappear 500 milliseconds after the framed oval disappears. If the bubble has already touched the top when it is released, it should pop immediately.

A running version of the program should be visible below if your browser handles applets correctly:



The demo shown in the on-line version does even more than explained above. If the user drags the mouse around then the bubble will follow it (remaining centered at the mouse position). You can get 1 point extra credit if you implement this as well. You can get another 1 point of extra credit if the bubble pops as soon as it touches any edge, even if it is still growing.

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)
RaceTrack
4 pts. Drawing the screen initially
4 pts. Dragging
4 pts. Correct behavior when two circles overlap
4 pts. Resetting if hit an edge of the racetrack
Follow the leader
4 pts. Drawing the screen initially
4 pts. Leader moving in the correct direction
4 pts. Other children following
4 pts. Saying "hey" when pressing mouse on children
Bubbles
3 pts. Keeping the bubble centered on the mouse
4 pts. Growing bubble while holding down mouse
3 pts. Floating bubble on release
3 pts. Breaking bubble at right time
3 pts. Saying pop when breaks
Miscellaneous (4 pts total)
Extra Credit (4 pts maximum)