CPSC 124 (Winter 1998): Lab 5
Applets and Objects

FOR THE FIFTH LAB in Computer Science 124, you will be working with a class that can compute simple statistics. You've used objects in your programs before (of type Console and MosaicFrame), but this is the first time that I am asking you to look inside a class to see how its methods are defined. In next week's lab, you can expect to write a class of your own from scratch. The second part of the lab is about applets. You'll be doing the exercise on "Jumping Squares" that was canceled in Lab 3.

You'll need the project folder named "StatCalc Starter" from the cpsc124 folder on the network. Copy it into the java folder on your M drive before beginning the lab. For the second exercise in the lab, you'll need a copy of the "Jumping Square Starter" folder. You might already have this from Lab 3. If not, get a copy now from the cpsc124 folder.

The exercises at the end of the lab are due in class on the Monday following the lab.

Outline of the Lab

Some Simple Stats

A statistic is a number that summarizes or abstracts some property of a (potentially large) set of numbers. For example, the mean is a statistic that represents the average size of the numbers in the set. The standard deviation is a statistic that gives some idea how widely dispersed the numbers are about the mean. Since these statistics often need to be calculated by programs, it makes sense to put the knowledge about how to calculate them into a class that can be reused over and over.

The "StatCalc Starter" folder contains a main program that computes some statistics for a set of numbers entered by the user. (You can also read it here.) It does the computations "by hand." You should read this program and try to understand what it is doing. (You don't need to understand how the formula for standard deviation works, though.) One interesting point is that all the statistics can be computed without keeping any permanent record of the numbers that are entered. Each time a number is read, certain variables are updated. For example, to compute the mean of the set of numbers, the program only needs to keep track of the sum of the numbers entered, not all the individual numbers.

In the "StatCalc Starter" project, you'll also find the file StatCalc.java, which defines a class named StatCalc. (You can also read it here.) Your job is to rewrite the main program so that it uses an object belonging to the class StatCalc to do all the statistical calculations. Add a variable, stats of type StatCalc to the program. Read the definition of the StatCalc class to see what methods it offers. You'll need to call stats.enter(), stats.getCount(), stats.getStandardDeviation(), stats.getMaximum(), and stats.getMinimum(). Use these methods to enter numbers into the dataset and to compute statistics about the dataset.

You should remove most of the variables and a lot of the other code from the original version of the main program. The version you end up with should be quite a bit shorter than the original.

The Jumping Squares Applet (at last)

For the second part of this lab, we return to an exercise that was canceled, for lack of time, during Lab 3. In this exercise, you will be modifying the behavior of an applet by modifying the definitions of some instance methods in a class.

The instructions for the exercise are not repeated here. Please go to the section of Lab 3 headlined An Applet With Action

Exercises to Turn in

Exercise 1. The first exercise of the lab is to modify the statistics program in the "StatCalc Starter Folder", as described above. Turn in a copy of the modified main program. Make sure that it follows the rules of good programming style. Also make sure you have removed all parts of the original program that are no longer needed in the modified version.

Exercise 2. Writing a short essay comparing the original statistics program to the modified version, which uses an object of type StatCalc to do the statistical calculation. Is the new program an improvement on the old? Why or why not? Which is easier to read? It was probably easier to write the original statistics program than to write the program in two pieces (a statistics class and a program to use that class). Is the extra work worth it? Why or why not? (Think about what happens the next time you have to write a program that does statistical calculations.)

Exercise 3. For this exercise, turn in a copy of the JumpingSquare.java file that you modified in the second part of the lab. For this one time, you don't have to worry about getting the comments right. I also want to check that you have successfully added the applet to your web site on hws3.hws.edu. Please give me the URL for your page.

[ Lab Index | Online Notes ]