Overloading methods and constructorsTopAnnouncementsReview of Drag2Shirts

Review of Drag2Shirts

Drag2Shirts

Let's now take a look at the main program. As usual, it is declared as a class that extends WindowController, and it has a main method that creates a new object from the class and sends it the startController method request.

As usual the class begins with a number of constant (static final) and variable definitions. This time, however, we need to do some initialization. While in most classes (like Tshirt) the initialization code goes in the constructor, classes extending WindowController do not have a constructor. Instead they have a special begin method:

    public void begin() {...}
 

As with the mouse event handling methods, this method must be declared to be public and must have exactly this header for the system to recognize it when the programs starts up.

With this program, the begin method creates new Tshirts that are associated with variables otherShirt and selectedShirt. It also colors one red and the other green. These last statements are a bit more complex than for Grace. In Java, color names, must be prefixed with the name of the class that created them. (They are public constants of the class, hence their names are in all caps.) Second, rather than being able to write otherShirt.color := red as in Grace, Java does not allow programmers to write methods whose name include :=. As a result, methods in the Java version of the objectdraw library usually use a prefix of set rather than tacking := on the end. Thus we can set the color of our shirt by writing otherShirt.setColor(Color.RED). Other substitutions include setX rather than x:=, setWidth rather than width:=, etc.

Finally, recall that assignment statements in Java use = rather than := and all statements are terminated with ";".

The objectdraw names for methods obtaining values from an object are also a bit different from Grace. Rather than just listing the name of a component, the style is to write get before the name of the component. Thus we write getX, getColor, getWidth, etc. A full listing of the Java objectdraw library methods can be found on the course webpages.

Let's now take a look at the mouse handling methods in Java. These should all look very familiar: onMousePress, onMouseDrag, etc. The only differences are those impacting all methods in Java - declaring the visibility (public), the return type (void) is written before the method name, the parameter type comes before the name, and the type Point is replaced by Location.

However, there are also some subtle differences in the way that control constructs are written in Java. For example, keywords after conditions are omitted. Thus we leave out then in if statements and do in while and for loops. Also, while Grace treated elseif as a single word, they must be written as two separate words in Java. Here are examples of if and while statements:

 if (cond1) {
    ...
} else if (cond2) {
   ...
} else {
   ...
}

while (cond3) {
   ...
}

While it is legal to drop the { } in an if statement or while loop if the block includes only a single statement, it can make your program harder to understand so we strongly recommend it. (Recall that Java does not require proper indenting - though I do - so programs laid out poorly on the page generally can't be easily understood without curly braces.) For loops are more complex in Java, so we will discuss those later.

One last thing before we finish our discussion of this program. The method onMousePress includes the declaration of a local variable, tempShirt. Local variables must be associated with a type, like all other identifiers that are introduced, but they do not have a visibility annotation like private or public. Because the scope of the local variables (like parameters) is limited to the method they are defined in, they are even more limited than private or public, so you may not associate one of those other annotations with them.


Overloading methods and constructorsTopAnnouncementsReview of Drag2Shirts