CS 051 Fall 2011

Lecture 4

Program Designs

You need to do a design and bring it with you for Part 1 of this weeks lab.
We discussed a sample design in class for the BetterBasketball example,
which is similar to what you'll do for Part 2 of the lab.

BetterBasketball Design Example

Java Objects

Getting familiar with "objects" in Java is essential to succeeding with
the language. To access objects after they are created, we name the object.
It is possible that multiple different names reference the same object, or
that a name has no object associated with it, or even that an object
has no name!

Here is a simple program that allows the user to explore how these changes can
occur through assignment statements: AssignmentVisualization

Boolean Variables

The original version of BetterBasketball checked the location of lastMouse
(the last known location of the mouse) every time onMouseDrag was called.
This seemed like a lot of wasted work, and could just be done once when the ball
was picked up, as demonstrated in: EvenBetterBasketball

Random Numbers

A lot of programs, especially games, will require random numbers to work well.

The RandomIntGenerator object will produce a new random integer every time its
nextValue() method is called.

The class example: Color4Scribbler
uses RadomIntGenerator, along with the more general form of the "if-else" statement,
to choose between four different predefined colors.

The class example: ColorScribbler
uses it to generate random colors by randomly selecting (R)ed, (G)reen,
and (B)lue values for newly created colors, as in:

    private RandomIntGenerator valPicker = new RandomIntGenerator(0,255);

...

    currentColor = new Color(valPicker.nextValue(), valPicker.nextValue(), valPicker.nextValue());