Title: Unit 3 - Objects and Classes Lab: First Steps - Example
1Unit 3- Objects and ClassesLab First Steps-
Example
2Objects in the Dance Studio Program
Dance Studio window
Band
Foot
Dance selection pulldown list
Dancer
Go / Stop button
Control panel
Dance floor
Positioning button
Waltz, etc.
Dance group
Dance step
3Classes and Source Files
- Each class is stored in a separate file
- The name of the file must be the same as the name
of the class.
By convention, the name of a class (and its
source file) always starts with a capital letter.
(In Java, all names are case-sensitive.)
4SomeClass.java
import ...
import statements
public class SomeClass
Class header
Attributes / variables that define the objects
state can hold numbers, characters, strings,
other objects
- Fields
- Constructors
- Methods
Procedures for constructing a new object of this
class and initializing its fields
Actions that an object of this class can take
(behaviors)
5public class Foot
private Image picture private
CoordinateSystem coordinates public Foot
(int x, int y, Image pic) picture
pic coordinates new CoordinateSystem (x,
y, pic) public void moveForward (int
distance) coordinates.shift (distance,
0) public void moveSideways (int
distance) coordinates.shift (0,
distance) ...
Fields
Constructor
Methods
6Fields
- A.k.a. instance variables
- Constitute private memory of an object
- Each field has a data type (int, double, String,
Image, Foot, etc.) - Each field has a name given by the programmer
7Fields (contd)
private static final datatype name
Usually private
int, double, etc., or an object String, Image,
Foot
May be present means the field is shared by all
objects in the class
May be present means the field is a constant
private Foot leftFoot
8Constructors
- Short procedures for creating objects of a class
- Always have the same name as the class
- Initialize the objects fields
- May take parameters
- A class may have several constructors that differ
in the number and/or types of their parameters
9Constructors (contd)
// FootTest.java ... Image
leftShoe ... ... Foot leftFoot
new Foot (5, 20, leftShoe) ...
An object is created with the new operator
The number, order, and types of parameters must
match
public class Foot ... public Foot (int
x, int y, Image pic) ...
...
Constructor
10Methods
- Call them for a particular object
leftFoot.moveForward(20) amy.nextStep(
) ben.nextStep( ) go.setText("Stop")
11Methods (contd)
- The number and types of parameters (a.k.a.
arguments) passed to a method must match methods
parameters
public void drawString ( String msg, int x,
int y ) ...
g.drawString ("Welcome", 120, 50)
12Methods (contd)
- A method can return a value to the caller
- The keyword void in the methods header indicates
that the method does not return any value
public void moveSideways(int distance)
...
13Ex. Using the Walker class as a prototype, create
a new class, CrazyJumper. A CrazyJumper should
move both feet forward together by stepLength in
firstStep and all other steps. In addition, the
feet should rotate 45 degrees in the first jump
and rotate back and forth 90 degrees in following
jumps.