Arrays of Objects

In this lab you will work with arrays of objects.

The Circle Class
To Get Started ...

Download the jar file: Mosaic.jar to your csci/201 directory, unjar the file (Help), create a new NetBeans project called Mosaic, and mount the Mosaic directory created when you unjarred the archive file (Help).

Modifying the Circle class

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 task 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 Mosaic class. The class begins with the declaration of several instance variables, almost all declared to be static final. In addition to instance variables, the Mosaic class contains two public methods, init and paint, and one private method, randomRainbowColor. The init and paint methods belong to all applets. The randomRainbowColor method is presently unused. It is included for you to use when generating a randomly chosen color for Circle objects.

Now let's look at the instance variables. All of these 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 collection of Circle objects stored in an array.

public class Mosaic extends Applet 
  // Number of cirles to create and display 
  private static final int NumCircles = 50;
  // Size of the Applet's window
  private static final int WinSize = 500 ;
  // Minimin offset of a circle's center
  private static final int MinOffset = 50 ;
  // Amount the offset can exceed the minimin
  private static final int MaxOffsetGrowth = 400;

  // Array of circles to be drawn
  private Circle[] Circles = new Circle[NumCircles];

In the last declaration, we are reserving enough memory to store 50, the value of variable NumCircles, references to Circle objects. We actually create the Circle objects in a for loop inside the init method.

  for (i=0; i<NumCircles; i++) {
    // generate a random x and y for circle position
    xPosition = (int) (Math.random() * MaxOffsetGrowth) + MinOffset;
    yPosition = (int) (Math.random() * MaxOffsetGrowth) + MinOffset;

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

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.

Your Assignment ...

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 randomRainbowColor method in the Mosaic class when selecting and assigning a random color to each circle object.

Instructor Checkoff ...

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

Adding a New Class
Your Assignment ...

Now add a new class to your program and use that class to enhance your display. Create a Rectangle class and add fifty 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.

Instructor Checkoff ...

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