An Animated Introduction To Computer Science
With Processing And Java

Thomas C. O'Connell


DRAFT 2024


Copyright © 2016, 2024 Thomas C. O'Connell, All Rights Reserved

Chapter 2 : Classes

2.1 Introduction

Our Monster looks a little lonely out there – it needs a friend, so let’s create another Monster. We could just copy the code we have for displaying one Monster to create two Monsters. However, if we want to create 1000 Monsters someday, it might be pretty cumbersome to have 1000 copies of this code laying around. Instead, we need a way to say to Java, “Here is a blueprint that describes what Monsters look like in general.” Then, when we want to create specific Monster objects, we just tell Java to give us a new Monster object based on that blueprint.

In Java, a blueprint for an object is called a class. We can define a new class in Java by writing the word class followed by the name of the class, which in this case is Monster. Following the name of the class, we have open and closing braces to mark the beginning and end of the block of code that represents the blueprint for the Monster object. In other words, the code would look like this:

class Monster  {

} // end class Monster

What do you think belongs in a blueprint for an object? Objects have attributes and objects have stuff they can do, which are described by methods. So, to define the blueprint for a set of objects, we should specify the attributes and methods that are associated with each those objects. For example, we can tell Monster objects to display, so we are going to define a method named display inside the Monster class. We could have named it anything we wanted, but display for displaying the object kind of makes sense. The display method is defined inside the squiggly brackets delineating the body of the class, like so:

class Monster  {

void display (float x, void y) {

} // end display()

} // end class Monster

Inside the display method, we will put the code to draw the ellipses, rectangles, and line that we had previously included in draw.

Figure 3.1 shows the code that defines the Monster class, including a method named display that displays the Monster at the current mouse position.

class Monster {


  void display() {
    ellipse(mouseX, mouseY,50,50);
    line(mouseX, mouseY, mouseX, mouseY+10);
    ellipse(mouseX-10,mouseY,5,5);
    ellipse(mouseX+10,mouseY,5,5);
    rect(mouseX-10,mouseY+15,5,6);
    rect(mouseX-3,mouseY+15,5,6);
    rect(mouseX+4,mouseY+15,5,6);
  } // end display()
  

} // end class Monster 


Figure 3.1: The Monster class.

Remember, a class is only a blueprint for creating an object; it is not an object itself. Our main Processing object needs to use this blueprint to create actual objects. This is done by including the statement new Monster(). We’ll put this statement in setup to create a new Monster object when the program first starts up. Since we want to create two objects, we will include this statement twice.

Presumably, the main Processing object creates these Monsters for a reason. We want these objects to do something, so we need to be able to tell an object what to do. But to tell an object what to do, we have to have some way to refer to the object. You can kind of think of it like giving the object a name – “Hey louis, show yourself.” To refer to a Monster object, we must have a Monster object reference. This is what we use to identify the object to which we are talking when we want to talk to a Monster object.

We tell Java the identifier we are going to use to refer to the Monster object and also tell Java that this object reference is going to refer to Monster objects as shown in Figure 3.2. In other words, an object reference is just like any other variable in that it has a type and its use requires a variable declaration. The variable declaration is going to go outside of the setup and draw methods because these object references are things the main Processing object keeps track of, i.e., they are attributes of the main Processing object, just like centerX and centerY in Figure ?? were attributes of the main Processing object.

The type of each object references is going to be Monster. The names of the references can be whatever we want to call our Monsters; how about “louis” and “wally”?1 Figure 3.2 shows the code. Notice that the draw method now calls the display method in each object. It does so by using the object reference, followed by a dot, followed by the name of the method in the Monster object. For example, louis.display() tells the object referred to by louis to run its display method.

Monster louis; // defines a new object reference 
Monster wally; // that will refer to a Monster.

void setup() {
  size(800,400);
  // create two new Monster objects 
  louis = new Monster(); // louis refers to 1st
  wally = new Monster(); // wally refers to 2nd   
} // end setup() 

void draw() {
  background(127,120,200);
  louis.display();
  wally.display();
} // end draw()


Figure 3.2: The main Processing object with Monster references.

Figure 3.3 shows the window created when we run the program, but there is only one Monster displayed, not two like there should be. What did we do wrong? Both Monsters are drawn at exactly the same location.

Your browser does not support the canvas tag.



Figure 3.3: There is only one Monster but should be two.

Back To Top