CS 51 Test Programs #1
Due: Friday, March 3 at 5PM

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 instructors. 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: You're Sooooo Hot

I can remember when I was a kid playing "Hot and Cold." You would hide something in the house somewhere while the "it" person was out. When they came back in, they would search for the hidden treasure to shouts of "You're getting hot." when they when they got close, or "You're getting cold." if they wandered in the wrong direction. The enthusiastic might get creative and say "You're on a block of ice." or "You are burning up." The basic idea, however, was to give the person some clue how well they were doing.

For your first test program, we want you to write a simple, computer version of this game. The hidden item will be a point on the program's window picked at random by the computer. The program won't draw anything on the screen at that point. That would make it too easy to find. Instead, the program's window will be completely blank except for ...

The program will draw a small colored circle on the screen to represent the "it" that is searching for the hidden point. The player will be able to drag this circle around the window using the mouse (as in the laundry program, the basketball programs, etc.). The program will vary the color of this circle to give the player clues about where the hidden point is. When the circle is close to the hidden point it should be bright red. When it is far away, it should be blue. In between, the red should fade from red through various shades of purple until it finally becomes blue.

Each time the player releases the mouse, the program should check to see if the distance between the current position of the mouse and the hidden point is less than the radius of the circle. If it is, the program should display a message congratulating the player's success and then immediately pick a new random point so the player can try again. Otherwise, the computer should reassure (or insult) the player and tell the player to try again without selecting a new point.

There are two "hints" you may need to write this program. First, here is a method that we have not yet used in class that you will want to use for this program. It is named distanceTo and it is an accessor method associated with Locations. If the names point1 and point2 are associated with Locations, then

     point1.distanceTo(point2)
will compute and return the distance (measured in pixels) between the two points.

You will want to use the distance between your hidden point and the mouse location to determine the color of the ball displayed on the screen. Unfortunately, when you make a new Color, the numbers given for the amount of red, green and blue to use must be integers and distanceTo returns a double. Our second hint is how to turn a double into an int. It's simple. You just type "(int)" in front of any expression that describes a double. Java will drop the fractional part of the double to produce an int. For example,

     (int) point1.distanceTo(point2)
will produce an integer approximating the distance between two points.

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



Problem 2: A Lot of Hot Air

We would like you to help us write a program to simulate the flight of a hot air balloon like the one shown below. A demo version should appear below if your web browser supports Java.



The idea is that while the wind controls how a balloon moves horizontally, its vertical motion is determined by how fully the balloon is inflated (and how much weight it is carrying). Accordingly, in the program we have in mind, the user will be able to move the balloon back and forth by dragging the basket with the mouse, but as the balloon is moved back and forth it will either rise or fall depending on how fully inflated it is. The user will be able to simulate inflating the balloon by clicking on it. The balloon will slowly deflate whenever the mouse is moved.

We have done half the job. We have written a class BalloonRide that extends WindowController. We want you to write a new Balloon class that will work with our BalloonRide class. Our class includes a definition of a begin method that creates a Balloon and several mouse handling methods that invoke methods to move, deflate, or inflate the Balloon. To work correctly with our BalloonRide class, your Balloon class will have to implement the following methods:

public void deflate();

This method will reduce the size of the circle the represents the balloon as long as the width of the balloon is at least as great as the width of the basket. The amount by which the width is decreased will be determined by a constant defined within the class. You may choose the exact value, but it should be small.

public void inflate();

This method will increases the size of the circle the represents the balloon as long as the circle does not touch the basket hanging below it. The amount by which the width is increased will be determined by a constant defined within the class. You may choose the exact value, but it should be much larger that the constant used by deflate.

public void drift(double dx);

This method will change the x-coordinate of all parts of the balloon drawing by the amount of its parameter's value. It will also change the y-coordinates depending on how inflated the balloon is. The amount that the y-coordinates changes should be proportional to the difference between the current size of the oval that represents the balloon and twice its smallest size. Each time the drift method is invoked, the balloon should also shrink a bit just as it would if deflate had been invoked.

public boolean basketContains( Location point);

This method returns true only if the point parameter is located within the basket underneath the balloon.

public boolean balloonContains( Location point);

This method returns true only if the point parameter is located within the oval that represents the balloon.

The constructor for the Balloon class will take three parameters: the x and y coordinates at which the balloon should be drawn and the canvas.

You should not modify the BalloonRide class in any way. You are free to read our code to determine how we use the methods we have asked you to implement.

Feel free to add other graphic items to the Balloon class to make it look more attractive.

Problem 3: Acid drops

Alas, we have just discovered that acid has started dripping from the first floor of the Chemistry building onto the floor in the basement. You are to write a program that simulates this dripping in an attempt to model what happens during this acid rain in the corridor. When your program is started it should create an ActiveObject class named Roof. A Roof object will generate circular drops of acid (one every 0.5 seconds) that rain on the window on the computer. Use a random number generator to generate the locations of the acid drops on the window.

When each drop appears, it should have a radius of 3 pixels (and hence a diameter of 6 pixels). Its radius should increase by 1 pixel every 200 milliseconds until the radius is 30 pixels. The increase in size should leave the center of the drop at the same position. That is it increases equally in all directions. When the radius exceeds 30 pixels, the drop should disappear from the screen (it evaporates!).

Unfortunately, a mouse is trapped in the room with the acid. The mouse's movements are controlled by the user with the computer mouse (the mouse goes wherever the cursor moves). The user attempts to keep the mouse from being killed by the acid drops. If the mouse overlaps with any acid drop it will die. To give the mouse a fair chance, there is a two second delay between the time a drop appears on the screen and it starts to grow and become dangerous to the mouse. (You might imagine that the acid reacts with something on the floor to become dangerous.) If the mouse dies, it should stop moving (i.e., it doesn't react to movements of the computer mouse). However, if the user clicks the mouse somewhere in the room the mouse will become resuscitated at that spot.

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



Tentative 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 (indenting, white space, etc.)
2 pts. Parameters used appropriately
Correctness (16 pts for each of 3 programs)
Hot and Cold
Drawing the screen initially
Dragging the colored circle
Color changes correctly
Displays correct message when mouse released
Balloons
Drawing a balloon
Moving the balloon horizontally
Inflating and Deflating
Correct vertical motion
Acid rain
Growing a drop
Having drop disappear at right time
Continuous creation of drops
Having mouse die when touches drops
Miscellaneous (4 pts total)
Extra Credit (4 pts maximum)