Lecture 3
Accessor and Mutator Methods
Mutator methods:
- tell the computer to do something
- are used as commands
- change an existing object
Accessor methods:
- return values representing information about the object.
Examples: getWidth, getHeight, getX, getY
- used where pieces of information are required such as actual parameters and
the right hand side of assignment statements
Class Example:
Crosshairs
Numeric types and Conditionals
Numbers are different from graphical objects.
- You cannot construct new numbers!
- Numbers do not have methods!
However, you can create instance variables that refer to a number, and you can perform
operations on numbers and instance variables that reference numbers.
You can also combine numbers with strings in the output.
Class Examples:
ClickCounter and
MouseMeter
Constant Names
Constants in programs should be given names so that the program is more easily
readable by humans. Constant names should be meaningful, and are traditionally given in
all capital letters, with underscores for the spaces between words.
For example: MAX_SCORE
In java:
- You can assign an initial value to a variable as part of of its declaration.
- You can mark a variable "static final" to indicate that its value will never change.
We call these named constants.
Conditionals
We use the "if-statement" in Java to execute selected statements only under certain conditions.
Class Example:
Basketball
The "if" statement is, itself, a statement. It can be icluded in those statements to be
conditionally executed!
Class Example:
BetterBasketball
Simple conditions in "if" statements can be combined using logical operators for and (&&),
or (||), and not (!).
Color
You can use predefined colors from the java.awt.Color class such as:
        Color.RED
You can also define your own colors by creating a new color object such as:
        new Color(200, 0, 0)
which produces a light shade of purple.