CSCI 201 Lab 9 -- Writing Classes

Getting ready

If a directory called c:\files\Lab09 presently exists on your computer, delete it.

Download a self-extracting archive of the Lab 9 project and store it in the C:\files directory. In Windows Explorer, double-click on the file and extract the project folder to the c:\files directory. This should create the new directory: C:\files\Lab09. Check to make sure that this directory exist before continuing.

Modifying the Circle class

Start up JCreator by double-clicking on the ".jcw" file (i.e., Lab09.jcw). Execute the program by selecting the Execute Project option under the Build Menu of JCreator's toolbar and observe the output. As you can see, the program generates a somewhat uninteresting arrangement of red dots on a grey background. Your job is to improve this display.

The program consists of two classes, Mosaic and Circle. Mosaic is an applet, and its job is to display a fixed number of randomly positioned circle objects (i.e., objects defined by the Circle class). Right now, all circle objects are red and have the same size; your first assignment is to modify the Mosaic and Circle classes so that the circles have a randomly selected size and color.

Lets begin by looking at the code in the Mosaic class. The class begins with the declaration of several instance variables, almost all of which are declared to be final and are therefore constants. 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 lab08. The private method randomColor() is included for you to use when randomly generating a color for the Circle objects; it is not used in the current version of the program.

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 the 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 Chapter 6, 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 (i.e., the value of the 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, i.e., the value enclosed in []'s. For example, Circles[0] refers to the first Circle object stored in the array, Circles[1] refers to the second object, Circles[2] the third object and so on.

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


   for(i=0; i < arraySize; i++)
      // call each circle's draw method
      Circles[i].draw(page);

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's currently not 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.


Lab instructor check-off 1

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.


Lab instructor check-off 2

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