CPSC 124 (Winter 1998): Lab 7
Arrays
FOR THE SEVENTH LAB in Computer Science 124, you'll be working with one-dimensional arrays. The first exercise, to get you started, is a simple console-based application. In the second, you'll be using arrays to keep track of data about multiple moving objects in an animation.
You'll need the project folders named "Histogram Starter" and "Bouncing Balls Starter" from the cpsc124 folder on the network. Copy them into the java folder on your M drive before beginning the lab.
The exercises at the end of the lab are due in class on the Monday following the lab.
Outline of the Lab
Displaying Data as a Histogram
A "histogram" is a visual display of data. In a histogram, each number in a set of data is represented by a bar. The length of the bar is proportional to the number it represents. This makes it easy to see certain relationships among the data.
In the program you will work with, each bar is a line of asterisks. For example, the ten numbers 5, 10, 15, 20, 30, 40, 50, 35, 20, 5 could be displayed in a histogram as:
***** ********** *************** ******************** ****************************** **************************************** ************************************************** *********************************** ******************** *****In the program, the data is obtained from the user. Since all the data must be input before the histogram can be output, the data must be stored in an array as it is read. Then the numbers in the array are used to create the histogram.
Much of the program is already written for you in the "Histogram Starter" project. (You can also read the program here.) You have to fill in the parts of the program that fill the array with data and use that data to create the histogram. A working sample solution, written as an applet, is available on a separate page.
A Note about Applet Layouts)
An applet can contain various Graphical User Interface components, such as buttons and scroll bars. When such components are used, there is the problem of controlling how they are laid out in the applet. The most common approach in Java is to use a LayoutManager. A LayoutManager is an object that keeps track of which components are in the applet and decides where to draw them. There are several different LayoutManager classes, and each type of LayoutManager has its own policies for arranging components.
All this is discussed in some detail in Chapter 6 of the on-line text. You won't officially have to read that for a while yet. However, this week's lab illustrates some basic techniques for dealing with components and layouts. You can read about it in the file BouncingBallsApplet.java in the "Bouncing Balls Starter" folder. (You can also read that file here.) Although you are not required to learn this stuff just yet, you should probably take a look. You might want to do something similar in your final project.
Arrays and Bouncing Balls
Open the "Bouncing Balls Starter" folder, open the .dsw file, and execute the applet. You'll see a red ball moving in a black rectangle. For your second exercise in the lab, you'll be changing the one ball into many independently moving balls. A sample solution, showing twenty-five moving balls, is available on a separate page. (There is also an enhanced version, which incorporates some ideas that you can work on for extra credit, if you want. You are only required to do the basic version.)
The black rectangle in the applet is an object of type Canvas. A Canvas is a type of GUI component. In this example, the canvas is actually a member of a subclass of Canvas called BBCanvas. The BBCanvas class is part of the "Bouncing Balls Starter" project. It is defined in the file BBCanvas.java. (You can also read that file here.) Most of the programming for the applet can be found in this class. That is, a BBCanvas "knows" how to display an animation of a moving ball, how to make the ball move faster or slower, and so on. For this exercise, you will work only on the file BBCanvas.java.
(The applet class, which is defined in the file BouncingBallsApplet.java, merely creates components, arranges them on the screen, and then responds to events by calling methods in the BBCanvas class. It is not essential for you to understand exactly how this works at this time.)
In the original version of BBCanvas, the position of the ball is given by two variables, x and y, of type double. In each frame of the animation, the values of x and y are changed a bit. This is what makes the ball move. the amount to be added to x is specified by another variable, dx, of type double. If the ball moves to the left or right edge of the canvas, dx is changed to move back in the direction of the center of the canvas. This makes the ball look like it has bounced off the edge of the canvas. Simlarly, the variable dy specifies how much y is to change between frames. Initially, the ball is placed in the center of the canvas and the values of dx and dy are chosen at random.
What will it mean to have many independently moving balls? For one thing, it means that you will need x, y, dx, and dy variables for each of them. In practice, this means that the data should be stored in arrays, instead of in single variables. If x, y, dx, and dy are arrays, then the data for ball number i can be stored in x[i], y[i], dx[i], and dy[i]. The size of each array is the number of balls. Let's say that the number of balls is given by ballCount. (You should declare ballCount to be a static final int variable in the BBCanvas class, and set its value to about 25. Use the name ballCount throughout the program, rather than an actual number, so that it will be easy to change the number of balls by changing just one line in the program.)
The first change you should make to BBCanvas.java is to change the declarations of x, y, dx, and dy to make these variables into arrays of the proper size. For example, instead of saying "double x;", you would say
double[] x = new double[ballCount];Later in the program, wherever the variables x, y, dx, and dy are used, you'll have to change the program so that the entire list of balls is processed instead. This means using a for loop. For example, where the original program says:
dx = 1.25 * dx; dy = 1.25 * dy;you will have to change this to:
for (int i = 0; i < ballCount; i++) { dx[i] = 1.25 * dx[i]; dy[i] = 1.25 * dy[i]; }You need to do this in each of the subroutines initialize(), doTimeStep(), faster(), and slower(). Once you've made all the necessary changes, your applet will be ready to go, and ready to post on the World Wide Web.
Exercises to Turn in
Exercise 1. Turn in a printout of your completed histogram program, as described above. (This is for 8 points.)
Exercise 2. Turn in a printout of the modified BBCanvas.java file, as described above, showing all the changes you made. Also post your applet on the World Wide Web, and tell me the URL that I should use to view your work. (This is for 12 points. For some ideas that might earn you extra credit, see the enhanced version of the applet.)
Exercise 3. Write a short essay explaining why arrays are necessary and some of the things that you can do with arrays that you can't do without them. Give some examples, including some example not taken from this lab. (This is for 10 points.)