CSCI 201 Lab 9 -- Writing Classes

You'll do this lab with even more class.

Getting ready

Download the Lab 9 jar file and save it into the directory C:\Files, on Windows, or the directory csci/201, on Linux. To avoid that nasty jGRASP jar bug, type the following commands in the windows labs.

mkdir C:\Files\Lab09
cd C:\Files\Lab09
C:\j2sdk1.4.0_01\bin\jar xfv C:\Files\Lab09.jar

And the following commands in the Linux labs.

mkdir -p ~/csci/201/Lab09
cd ~/csci/201/Lab09
jar xfv ~/csci/201/Lab09.jar

Modifying the Circle class

Start up jGRASP and open the project file Mosaic.gpj that you just extracted. Compile the java classes Mosaic and Circle. Mosaic is an Applet. Run it as an applet and view its display.

The task of Mosaic is to display a fixed number of randomly positioned circles, each generated by the Circle class. Right now, all circle objects are red and have the same size. That's pretty boring. Your first assignment is to modify the Mosaic and Circle classes so that the circles have a randomly selected size and color.

Let's begin by looking at the code for the Mosaic class. The class begins with the declaration of several instance variables, almost all declared to be final. In addition to instance variables, the Mosaic class contains three methods: init, paint, and randomColor. The methods init and paint belong to all applets and were discussed in Lab 8. The private method randomColor is presently unused. It is included for you to use when randomly generating a color for the Circle objects.

Let's look more closely at the instance variables. All of the constants are used in creating the display and their names are indicative of their purpose. The only instance variable that is not a constant is a data structure called Circles defined on the last line shown below.

public class Mosaic extends Applet 
  private final int arraySize = 50;
  private final int sideLength = 400;
  private final int shapeSize = 50;
  private final int positionMagnifier = 300;
  private final int colorCount = 10;
  private final int offset = 50;

  private Circle[] Circles = new Circle[arraySize];

Circles is an array. We will learn more about arrays in our text, but for now we need to know that an array allows you to store a collection of objects. In the declaration above, we are reserving enough memory to store 50, the value of variable arraySize, references to Circle objects. We actually create the Circle objects in the init method, in the last line of code shown below.

  public void init() 
    int i, xPosition, yPosition;
    for (i=0; i < arraySize; i++)
      // generate a random width and height
      xPosition = (int) (Math.random() * positionMagnifier) + offset;
      yPosition = (int) (Math.random() * positionMagnifier) + offset;

      // now create a new circle object
      Circles[i] = new Circle(xPosition, yPosition);

Circles[i] refers to the i'th circle in the Circles array. Because the value of i is controlled by the for-loop, i will initially have the value 0 and be incremented by one each time through the loop until it reaches the value 49. As a result, the for-loop above creates 50 Circle objects in the array called Circles. Each object is referred to by its index position, the value enclosed within the []'s.

In Mosaic's paint method we call each Circle object's draw() method to cause the object to be rendered in the display. This is done in the following lines of code:

  for(i=0; i < arraySize; i++)
    // call each circle's draw method

The Circle class also consists of instance variables and three methods. As you know, the instance variables represent the state of a circle object and the methods define the object's behavior. Two of the methods in the Circle class are public, they are a constructor and the draw method. The third method, letter2Color, is a private method that is not currently used. It is provided to help you establish a randomly selected color for each circle object.

In order to create circle objects with varying color and varying size, you will need to add a representation of color and size to the state of a circle. You will also have to modify the constructor and the draw methods to use that representation. Be sure to make use of the letter2Color method in the Circle class and the randomColor method in the Mosaic class when selecting and assigning a random color to each circle object.

Initial lab instructor check-off

Show your instructor the output of your modified program. Your display should now consist of a grey background with 50 circles, each of which has a randomly generated diameter and color.

Adding a New Class

Now add a new class to your program and use that class to enhance your display. Create a Rectangle class and add 50 rectangles to your display. Each rectangle should have a randomly generated size and position. You may randomly generate a color for each rectangle but that is not required. Use the Circle class as a model for creating and using your new Rectangle class.

Be sure to add your Rectangle class to the jGRASP project.

Final lab instructor check-off

Show your instructor the enhanced display created by your new program. This display should contain both circles and rectangles.