CPSC 124, Fall 2001
Lab 8: A Simple Class

In this lab, you will write a complete class definition that works with a main program, which I have already written. This exercise will probably not occupy you for the entire lab. You should use any extra time to work on your second programming assignment, or to work on the "Some fun" part of the lab.

The files for this lab can be found in the directory /home/cs124/lab8. Begin by copying this directory into your own account, and cd into your copy of the directory.

This lab is due next Wednesday, October 24. Turn in a printout of your Java source code file, Account.java. Don't forget to include comments in your class definition and to otherwise follow the rules of good programming style.

Exercise: Bank Account

The exercise for this week is to write a class that simulates a bank account (in a very simple way). The "account" is created with an initial balance. It is possible to deposit and withdraw funds, to add interest, and to find out the current balance. This should be implemented in class named Account that includes:

The addInterest method takes the interest rate, r, as a parameter and changes the balance in the account to balance*(1+r).

All the methods in the class are simple, one-line subroutines. The main point is to get the syntax of the class and the parameters of the subroutine correct. Your class must work with my main program, BalanceAccount. Once you have compiled your class, you should be able to run BalanceAccount. It should run exactly like the following applet:

Some Fun: Mosaic Applets

This is not a required part of the lab (except to read about it), but you can do it if you want to add some interest to your web site.

Lab 6 --- as well as the textbook and programming assignment two --- uses a class named Mosaic to manage a mosaic window. Mosaic contains static subroutines and is meant to be used in stand-alone programs. Suppose you want to write a mosaic applet that you can put on your web page?

I have written a class called SimpleMosaicApplet that can be used as a basis for writing mosaic applets. To do so, create a class that extends this class, and include a "public void program()" subroutine in your class. The program() subroutine will be run when the applet is created. It can use the functions getRed(row,col), getGreen(row,col), and getBlue(row,col) and the subroutines setColor(row,col,r,g,b) and fillRandomly() to work with the mosaic. It can use the delay(milliseconds) to insert a delay in the program. (It's a good idea to include some delays in an applet, since it gives time for other things in the Web browser to run.) Note that these subroutines are defined in SimpleMosaicApplet, not in any separate Mosaic class, and they should be called directly. Also, note that there is no equivalent to Mosaic.open() or Mosaic.isOpen() since there is no separate window. The mosaic is set up to use 40 rows and 40 columns by default, but you can change this by including an init() method in your subclass and assigning values to the instance variables rows and columns.

Here is how a basic random walk applet could be written, based on SimpleMosaicApplet:

       import java.awt.*;
       public class RandomWalk extends SimpleMosaicApplet {
          // Set up a 20-by-20 grid, instead of the default 40-by-40.
          public void init() { 
             rows = 20;
             columns = 20;
          // Define the random wald program.
          public void program() {
             int row = rows / 2;    // start at center of grid
             int col = columns / 2;

             setColor(row,col,255,0,0); // make the square red
             while (true) {

                delay(30);  // insert a delay of 30 milliseconds

                setColor(row,col,0,0,0); // return current rect to black

                switch ( (int)(4*Math.random()) ) { // move to new rect
                   case 0:
                      if (row > 0)
                   case 1:
                      if (col > 0)
                   case 2:
                      if (row < 19)
                   case 3:
                      if (col < 19)
                setColor(row,col,255,0,0);  // make new square red

          } // end program
       } // end class RandomWalk

Here is what this applet looks like (try right-clicking it):

If you have a Mosaic program that you would like to convert into a mosaic applet, here is a guide for doing so:

David Eck, 18 October 2001