TopPre-Defined Data types in JavaReview of Drag2Shirts

Review of Drag2Shirts

Let's take a look at a more complex program, one that involves regular classes rather than just those corresponding to main programs. The program we will look at is a program to drag two Tshirts, again one that we wrote for Grace earlier.

Tshirt

Let's start by looking at the Tshirt class.

The class starts with two import statements:

import objectdraw.*;
import java.awt.*;

Virtually all of our programs will import objectdraw, just as in Grace we used the corresponding dialect. We will also need to import the built-in Java libraries java.awt.* because class Color is defined in that library.

This time our class header does not extend WindowController, similarly to how in Grace, classes that were not main programs did not inherit graphicApplication.

The program has many constants. In Java, Grace's def is spelled private static final. You already know what private means. The keyword final means that the value may not be updated - for variables, you may not assign to it. The static is a little funnier in that it means that there is only one copy of the constant made, and it is shared with all instances of the class. It is a way of saving space when your program is running. Generally, that is the right choice when you are creating instance variables and want a constant. However, if you are making a constant inside a method, then you should just use final and omit the static.

Technically, static means that the identifier is associated with the class rather than the objects generated from the class. Grace does not have a corresponding notion. In Grace, by contrast, if we only want one copy of something we put it in a separate object and share it - kind of like the "empty" objects we used to put in variables that didn't have anything useful in them yet.

The x and y-coordinates used in the objectdraw library are given by type doubles. It is OK to use ints, as they will be converted to type double when necessary, but when you ask an object for a coordinate, it will always return a double.

The instance variables declared in Tshirt are essentially the same as in the Grace Tshirt example. Their types are given by the classes they implement, e.g., FramedRect, FilledRect, FramedOval, etc. Variables that are of object types that are not initialized in their declarations are assigned value null by Java. (Identifiers of primitive types are initialized to 0 or false, depending on their type.) You can think of the value null as being the same as being undefined in Grace. However, you can test the value of a variable to see if it is equal to null in Java, while that is not possible in Grace. If you get a "null pointer" error in Java, it usually means that you have forgotten to initialize a variable that you are using.

By the way, variables that are declared in methods are NOT initialized automatically; the program must assign them a value before they are used or Java will complain.

After the variable declarations, you will find the Tshirt constructor:

   public Tshirt(double x, double y, DrawingCanvas canvas) {
      // create boundary rectangles
      sleeveTrim = new FramedRect(x, y + NECK_HEIGHT / 2, 
                                                   SLEEVE_WIDTH, SLEEVE_HEIGHT, canvas);
    ...

The constructor's job main job is to initialize the objects created from the class. The parameters that would be included in the class header in Grace are included in the class constructor in Java. The constructor in Java must have the same name as the class (in this case Tshirt). Parameters are declared with their types as usual, but constructors are not provided with a return type (because they always return an object of the same type as the class). Constructors are invoked with the keyword new:

   new Tshirt(50,50,canvas);

While variables can be initialized in their declarations, many are initialized in the constructor. Any other executable code that would have been included in the Grace class definition (outside of methods) will be found in the constructor in Java.


TopPre-Defined Data types in JavaReview of Drag2Shirts