Incremental Java
Objects Respond to Commands

Making a Class

We're going to create a very simple class called Rectangle. Just like a designer of a remote control, we need to think about the kinds of operations you want to perform on a Rectangle object. A remote control designer should think about what the remote control user should be allowed to do.

The designer lists the operations and decides what buttons need to be pressed, and in what order, to tell the remote control about the operation to be performed.

Since this is the first time you've thought about designing an object, and you may not know what kind of operations to write, I'll write the operations down for you.

Pick the width and Pick the height lets the user select the height and width of the rectangle. Look up the width and Look up the height may seem unusual to you. Why would you need to look the values up if you were the one that set it?

That might work if you only care about a Rectangle or two. But suppose you had several hundreds. You'd have to look it up. Have you ever called a credit card company to get information about your account? They ask for your account number. Why? To look it up. Didn't they set up your account? Well, yes, but they set up millions of accounts. It's easier for them to look up your information if you tell them the account number.

The last operations are Compute the area and Compute the perimeter. These thing can be computed once you know the height and width of the rectangle.

Operations are Methods

The first step in designing a class is to think about what kind of object you want, and the operations you want the object to react to. It takes a little experience to come up with a good set of operations.

In Java, the operations are called methods. (They're called member functions in C++). We'll call them methods from now on.

Method names are identifiers. They follow the same rule as variable names. That is, you must start with an alphabetic character, underscore, or dollar sign, followed by zero or more alphabetic characters, digits, underscores or dollar signs.

Java style says that methods should follow the same rule as variable. Capitalize the first character of each word, except the first character of the first word. Don't use underscores or dollar signs.

Let's rewrite the operations as identifiers.

Setters and Getters

There are two important categories of methods: setters and getters. Setters attempt to set a value inside the object. This causes the object's internal information to change. For example, if you set the height to 10, and the height was originally 5, the object has been modified.

Getters look up information, or possibly compute information, but without modifying the object. So if we getHeight on an object, we are retrieving the height information from the Rectangle object. This should not change any internal values of the object.

Occasionally, we have methods that return a boolean type. these are called query methods. A query is a question. Query methods ask if an object has a certain property. For example, we have a method called isSquare which we use to determine if the height and width are the same.

Not all methods are getters or setters. However, they are common enough that we mention them now.