CS 134 Test Program #1
Due: Friday, March 19, 2004 at 4PM

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, our on-line examples, and the web pages associated with the course web page, but use of any other source (human or otherwise) 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. The use of any other outside help or sources is a violation of the Honor Code.

Complete each of the following problems, documenting your code well. Click on this link to download the starter folders for all three of the problems described below. Inside the folder you download, you will find three sub-folders containing JBuilder projects for each of the three programs you are asked to write. Be sure to move this folder into your account's "cs134" folder before beginning work.

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: Hi Striker

For this problem we would like you to write a program to simulate the behavior of an attraction found at traditional county fairs and carnivals, the "Hi Striker" strength tester. This is the game where you show how strong (or not) you are by trying to propel a weight as high as you can up a rail toward a bell by hitting one end of a lever that launches the weight. Below, we have included a picture of one of these devices next to a running version of the program we hope you produce (if your browser handles applets correctly):

The four parts of the drawing that matter the most to this program are:

To launch the weight, a person using your program will simply drag the hammer toward the target and then release the mouse while the mouse cursor is actually contained within the target. When this happens, your program should determine how much the y coordinate of the mouse changed between the moment when the user first grabbed the hammer and then released the mouse. If the hammer's position when it is released is higher than when it was grabbed, the weight should not move at all. Otherwise, the weight should move toward the bell by either a distance equal to the change in the hammer's y coordinate or the total distance to the bell, whichever is smaller. The weight should return to its initial position as soon as the mouse button is again depressed.

To save you a bit of time, we have included code to draw a sketch of a Hi-Striker in the starter folder provided for this test program. You will have to add code to draw the hammer to what we have already placed in the begin method, and then you will have to write the mouse event handling methods required to implement the behavior described above.

Problem 2: Popping up all over

It's almost spring and the flowers will be coming up through the snow at any moment. This program will help you prepare for that day. A running version of the program we hope you produce is provided below (if your browser handles applets correctly):

The canvas should initially start empty. When you click on the canvas, a flower should start growing at that point. Initially it will be just a sprout, but as you drag the mouse around, it should grow. When it reaches its full height, the stem should stop growing and petals should appear. The color for the petals should be chosen randomly. The flower won't grown any more, but if you click on the petals after it has bloomed, the flower will change color. When you are happy with the flower's color, you can grow another one by clicking somewhere else in the window. When the mouse moves out of and then back into the window, the scene should reset itself to be empty.

Your program should be divided into two classes: a window controller called Spring and a Flower class. We have actually provided a complete Spring class in the starter folder. You should not modify our Spring class in any way. Instead, you have to implement the Flower class so that it works with our Spring. In particular, the Flower constructor should expect three parameters: the Location where the Flower's stem should be planted, a double specifying the maximum height of the Flower, and the canvas. The Flower class should define the following methods used by the controller to implement the functionality described above:

  1. public void changeColor(): sets the color of the flower petals to a random color.
  2. public boolean flowerContains(Location point): returns true if the petals or center of the flower contains the point.
  3. public void grow(): make the flower grow a bit.
The flower will only grow when the mouse moves, so Flower should not extend ActiveObject.

Feel free to add other graphic items to the Flower class to make the flowers look more attractive.

Problem 3: Three-cow Monty

NOTE: If your cows are not appearing when you create VisibleImages, try downloading the following cow.gif picture to your computer (on a mac, you can just drag the image onto the desktop). Then drag the cow.gif file to the "ThreeCowMonty" folder inside Eclipse's Package Explorer window (on the left side of the screen). The original starter project had a corrupted version of the cow picture.

The goal of your final program is to play a simple game called "Three-Card Monty" (sometimes also known as the Shell Game). Of course, this is Williams, so our version actually plays "Three-Cow Monty". The program begins with three seemingly identical purple cows lined up in a row as shown below. However, one of the cows is different. If you click on the middle cow, it moos and you score a point for picking the correct cow. If you click on either other cow, they do not moo and your incorrect score increases by one. In itself, this is not much of the game, but if you click outside any cow the program shuffles their order in front of you. The goal is to follow the moving cows and click on the one that moos after they have been shuffled.

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

Your program should be divided into two classes- the ThreeCowMonty window controller class and the Shuffler (or Moover!) active object class. The ThreeCowMonty class creates the three cows and handles the mouse interaction. The Shuffler moves the three cows, as illustrated in our demo. It selects two cows and gradually moves each of them from its original location to the opposite cow's location. Once those two cows are repositioned, it randomly selects two new cows and moves them again, repeating this process perhaps 10 or 20 times before stopping. While it is more natural to describe the shuffler as randomly picking two cows to move, you may find it is easier to write the required code if you instead think of the shuffler as randomly picking one cow that it won't move and then moving the two that were not picked.

The ThreeCowMonty class should prevent anything from happening if the user clicks while a Shuffler is shuffling the cows. In other words, the window controller should not test whether you click on the correct cow or create a second Shuffler if a Shuffler is currently running.

You may wish to start by just moving the cows horizontally until the shuffling works. The up-and-down motion makes the game more entertaining, but it can be added after the rest of the program is working. The motions of the cows in the demo look like smooth curves, but they actually move in straight lines, as in the following picture:

We provide you with the cow picture and the mooing noise in the starter folder, but feel free to shuffle your choice of livestock, frogs, basketballs, or anything else.

If the code for your run method begins to get complicated, remember that you can define private methods to perform common operations and then invoke these methods from run.

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)
Hi Striker
Drawing the hammer
Hammer move when (and only when) grabbed
Weight rises when mouse is released on the target
Weight never moves above the bell or below the base
Initial stem and bulb created correctly
Stem grows and bulb rises
Flower opens when stem reaches height
Flower color changes correctly
Three-cow Monty
Cows placed correctly initially
One cow moos
Scores awared and displayed Correctly
Pairs to move chosen randomly
Motion of cows animated correctly
Motion of cows animated simultaneously
Miscellaneous (4 pts total)
Extra Credit (4 pts maximum)