Lecture 7 (Wednesday 09/11/02)

Preliminaries

Topics

Review of WindowController Organization

Stuff to do at beginning of program execution goes in method:

   

public void begin()

Stuff to do in response to user actions goes in one of:

   

   public void onMouseClick(Location point)

   public void onMouseEnter(Location point)

   public void onMouseExit(Location point)

   public void onMousePress(Location point)

   public void onMouseRelease(Location point)

   public void onMouseMove(Location point)

   public void onMouseDrag(Location point)

Strings

When we construct new Text, we specify the String to be displayed. A string is simply a sequence of characters. In Java we surround string literals by double quotes:

There are many contexts in which strings are useful. We'll spend a fair bit of time learning more about them during the second half of the semester. For now, it will be sufficient for us to discuss how strings can be put together in order to create interesting and useful output.

In the MouseMeter example from lecture 3, we wanted to display the x and y coordinates of the mouse, and we wanted the displayed information look like a parenthesized coordinate pair. To do so, we needed to create a string that was made up of the x and y values, as well as the textual left parentheses.

We can concatenate two string using the operator +. Thus

gives us the string above:

We can also convert numbers to strings by concatenating them with strings. Therefore, if the mouse is at Location (10,20), then


   "Mouse x coordinate is " + point.getX()

gives us the string:


   "Mouse x coordinate is 10"

So to create a string like (x, y) from the mouse's x and y coordinates, we had to say:


   "(" + point.getX() + "," + point.getY() + ")"

 

Review of Naming

Can't send a message to an object unless you give it a name!

   FilledRect myRect;

Associating a value with that name:

   myRect = new FilledRect(10,10,30,20,canvas);

Sending a message or invoke a method or call a method:

    myRect.moveTo(50,90);

Numeric types

We've been discussing graphical objects exclusively.

What if we want to manipulate numbers? It's possible, but instead Java treats basic numeric types differently.

First base type is int, representing integers, with operations +, -, *, /, and % (representing remainder after division).

What do we do with integer division? Truncate any remainders. I.e., 14 / 4 = 3 and 14 % 4 = 2.

Here's an example that makes use of ints:

Demo. Click Counter

Differences between Reference and Numeric Types

Declaring an object name such as

FilledRect box1;

Gives you a named spot in memory that can contain a reference / pointer to a FilledRect object (you don't have one yet).

Declaring a numeric name such as

int num1;

Gives you a named spot in memory that can hold the actual value of the integer.

This leads to many subtle confusions among students. For example, consider the following code:

FilledRect box1, box2;

int num1, num2;

box1 = new FilledRect(100, 100, 50, 50, canvas);

box2 = box1;

box1.setColor(Color.red);

num1 = 5;

num2 = num1;

num1 = num1 + 1;

What is the value of num2?

What is the color of box2?

Constants

Recall the simple bridge program we wrote:

What if the numbers we're using will always be the same?

There are two kinds of named values: variables and constants. So far we have only used variables.

There are two advantages to using constants:

We can declare a name to be a constant by writing it in the following form:
    private static final int ROAD_HEIGHT = 30;

Let's take a moment to explain all the magic words here.

Private means that no class outside of the class being defined here gets to see this constant. Alternatives are public (everyone gets to see it) and protected. We'll explain protected in more detail later in the term. Right now only certain methods from WindowController are labelled with protected. It has to do with the ability to override behavior defined in the class you are extending.

static means that only one copy of the value is kept around (for efficiency). It's hard to imagine why we might want more than one copy of a given constant, so they are always declared static.

final means that its value cannot be changed. This is the essence of a constant.

The rest of the declaration just says that the constant is going to be of type int, has name ROAD_HEIGHT, and has the value 30. Because the values of constants can never be changed, they must be given an initial value in their declarations. We can also give initial values to names representing variables in their declarations, but we are not required to do so.

Having gone to all this effort to explain all the keywords in a constant definition, really all you need to do is always write down the magic incantation

    private static final ...

at the head of all constant declarations. The following program is a revision of the two previous versions which uses constants:

Demo. Prettier bridge program

Not everything can be a constant. Constants can't depend on anything created when program starts up (except other constants). In particular, a constant may not depend on canvas. For example, all of our constructor routines for graphics primitives depend on canvas. Thus we may never have a constant of type FilledRect, FramedOval, etc.