Next: None. Up: Objects review. Previous: Using objects.


Defining a class

When we design our own systems, we define our own new classes. For example, we might want to define the Robot type. Beginning the class definition is simple.

public class Robot {
    // : (everything else goes between these two braces)
}

Instance variables

The first question we ask about a new class is: What does it need to do? In the case of the Robot class, we've already said what it needs to do.

The second question we need to ask is: What does it need to remember in order to accomplish this feat? Each thing that the object remembers is an instance variable. (This distinguishes it from a local variable, which is something that is remembered only for the duration of executing a method.)

What do robots need to remember? Each will need to remember its x- and y-coordinates, which window it is living within, and which direction it is facing.

public class Robot {
    private DrawableFrame window;
    private int x;
    private int y;
    private int direction; // 0 for east, 1 north, 2 west, 3 south

    // : (more to come here)
}

Constructor methods

When we create a Robot object, its constructor method is called. Within our class definition, we need to define what the method does. In particular, the method should initialize all the instance variables to their proper values when a new robot is created.

public class Robot {
    private DrawableFrame window;
    private int x;
    private int y;
    private int direction; // 0 for east, 1 north, 2 west, 3 south

    public Robot(DrawableFrame in_window, int in_x, int in_y) {
        window = in_window;
        x = in_x;
        y = in_y;
        direction = 0;
    }

    // : (more to come here)
}

Notice that we have not done the following.

public class Robot {
    private DrawableFrame window;
    private int x;
    private int y;
    private int direction; // 0 for east, 1 north, 2 west, 3 south

    public Robot(DrawableFrame in_window, int in_x, int in_y) {
        DrawableFrame window = in_window;  // BAD!!!
        int x = in_x; // BAD!!
        int y = in_y; // BAD!!
        int direction = 0; // BAD!!
    }
}
In this example, we have declared local variables within the constructor method whose names just happen to be identical to the instance variable names. But this coincidence of names is of no significance to Java; it thinks of them as being totally different variables, and as a result, the instance variables are not initialized with this technique.

Instance methods

We also need to define how the robot handles the various messages. Each method definition is a sequence of statements that the robot performs when requested. These statements may refer to the instance variables or call instance methods.

public class Robot {
    private DrawableFrame window;
    private int x;
    private int y;
    private int direction; // 0 for east, 1 north, 2 west, 3 south

    public Robot(DrawableFrame in_window, int in_x, int in_y) {
        window = in_window;
        x = in_x;
        y = in_y;
        direction = 0;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void moveForward(int dist) {
        int init_x = x;
        int init_y = y;

        if(dist == 0) {
            x += dist;
        } else if(dist == 1) {
            y -= dist;
        } else if(dist == 2) {
            x -= dist;
        } else if(dist == 3) {
            y += dist;
        }

        Graphics g = window.getGraphics();
        g.drawLine(init_x, init_y, x, y);
    }

    public void turnLeft() {
        dist++;
        if(dist == 4) dist = 0;
    }
}
Notice how moveForward() declared some local variables - you know they're local, since they're defined inside the method. (If they were declared outside the method, they'd be instance methods.) These are temporary variables, since we only want to remember them while the robot is moving forward. We don't really have any reason for the robot to remember them later, so we make them local variables instead.


Next: None. Up: Objects review. Previous: Using objects.