CS 124, Fall 2009
Lab 15: Partially Full Arrays

For the final lab of the term, you will work on a short exercise involving partially full arrays. You should show me your working program when you have finished it. Otherwise, there is nothing to turn in for this lab. The exercise should not take the entire lab period; you can use any extra time to work on your final project.

You should begin by creating a project or folder named "lab15". You will need to add three files to your project: MovingBall.java, BallPen.java, and BallPenPanel.java. These can be found in /classes/f09/cs124/files_for_lab_15. (Note that MovingBalls.java comes from Exercise 7.4 in the textbook.)

You will be working on the file BallPenPanel.java. The main() routine is in the file BallPen.java, so that is the file that you should run as an application.


In the program as it currently exists, there is a display area that can show one moving ball at a time. Every time you click on the display area, a new ball is created. The newly created ball replaces the ball that already exists (if there is one). Your job is to make the program support any number of balls simultaneously. When the the user clicks the display area, the newly created ball should be added to any balls that already exist. Although this could probably be done most easily using an ArrayList, you are required to do it using a partially full array.

Start by replacing the instance variable named ball with an array of balls, of type MovingBall[]. Also add a counter to keep track of the number of balls that exist. The array will hold (pointers to) all the balls that are currently shown in the display.

Then, modify the rest of the program appropriately. Wherever the old variable, ball, was used, you will have to do something different, using the array that you have added to the program. Also, you will have to implement the "Undo" command, which should simply remove the last ball from the array (assuming that the number of balls is not already zero.) Note that your program should support any number of balls, so you will have to expand the array when necessary.

Programming Note

Note that there are several things done in this program that you haven't seen before. These are just to show you how to do several things that are mentioned in the reading but that we haven't managed to cover in class: