CS102 Spring 2012

Lab #5

Bouncing Balls

The main purpose of this lab exercise is to:

  • practice using collections of data (in this case HashSet)
  • practice adding random behavior to your program
  • have fun playing with an animated, graphical program!

How to get started

  1. Login to one of the Linux machines in the classroom.

  2. Create a subdirectory under cs102 for this lab:

    $ cd cs102
    $ mkdir lab5

  3. Copy the starter project for this lab:

    $ cp -r /home/cs102/bluej/projects/chapter05/bouncing-balls/ ./lab5

  4. Launch BlueJ and open up your bouncing-balls project for this lab.

Java Exercises

The bouncing-balls BlueJ project is the basis for today’s lab and for next week’s assignment. For today’s lab, read section 5.12.3 on page 190 of the OFJ text. Complete exercises 5.62 – 5.64.

Exercise 5.62

Change the method bounce in class BallDemo to let the user choose how many balls should be bouncing.

For this exercise, you should use a collection to store the balls. This way, the method can deal with 1, 3, or 75 balls–any number you want. The balls should initially be placed in a row along the top of the canvas.

Which type of collection should you choose? So far, we have seen an ArrayList, a HashMap, and a HashSet. Try the next exercises first, before you write your implementation.

Exercise 5.63

Which type of collection (ArrayList, HashMap, HashSet) is moswt suitable for storing the balls for the new bounce method? Discuss in writing, and justify your choice.

Exercise 5.64

Change the bounce method to place the balls randomly anywhere in the top half of the screen.

Commenting and Testing

Whatever methods you implement need a purpose statement (header comment), and tests in the Examples class. Since we’ve now covered Javadoc comments, be sure you use them, and test them in BlueJ by switching between “Source Code” and “Documentation” view when editing your class definition.


Assignment 4. The remaining exercises, 5.65 and 5.66, are left for Assignment 4. You will use your completed Lab 5 as the starting point for Assignment 4.



When you’ve completed the above exercises, alert Marc or one of our coaches to take a look, and check you off as having completed the lab. After you are checked off, follow the submit procedure detailed below.

Submitting your work

From a terminal window, type the following commands at the Linux prompt:

cd
cd cs102
submit102 lab5

Log out

When you are done, close BlueJ and logout. Always remember to log out when you are finished using the system, to ensure that no one else uses your account.

courses/cs102-201201/labs/lab5.txt · Last modified: 2012/02/24 10:14 by mlsmith
VCCS Top Events Extended Site Search Login Vassar Science Web Vassar Home Driven by DokuWiki Valid XHTML 1.0