Lecture Date: Friday, March 20
We continue with our exploration of classes today.
Let’s consider something really simple: a
First let’s draw a class diagram for
Rectangle. To do this, we need to consider what makes up the state and behavior of our object. That is:
- what makes this
Rectangledifferent than any other and
- what actions can we do with a
So, what are the fields that make up a
Rectangle? How about the x and y coordinates of the lower-left and upper-right corners?
We declare these at the top of the class like this:
1 2 3 4 5 6 7 8 9
In general, you want your fields to be private. It allows you to “protect” them from accidental change. We’ll look at this a bit later.
How about methods?
getArea() are nice.
But there are some other methods we need. Every class typically has a set of common methods that do effectively the same thing for each class.
The constructor is the method that “boots up” the class. It has no return type and might or might not have parameters. A constructor with no parameters is called a null constructor or default constructor. This is the absolute “default” version of a class. You should set the values of all the fields to default values here.
A constructor that takes a value for every field is called a full constructor. In this constructor, you simply set the fields equal to the appropriate passed-in parameter.
So, our constructors for
Rectangle might look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
The other types of methods are called getters and setters. These methods allow for the, well, getting and setting of field values. We use methods to do this to “protect” the fields.
Say for instance someone was using a
Rectangle and accidentally tried to set the point “below” (0,0). We don’t want that (in our example at least). So in the setter, we can guard against it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
How about getting the area?
1 2 3 4 5 6 7
Now what if we wanted to determine if two
Rectangles were overlapping?
1 2 3 4 5