Object Oriented Programming j g g with Buggles and Bagels

Unlearn What You Have Learned
Object
j
Oriented Programming
g
g
with Buggles and Bagels
t ti
static
method
th d
class
Thu. Jan. 27, 2011
j
object
CS111 Computer Programming
variable
double
fl t
float
argument
parameter
instance
Department of Computer Science
Wellesley College
I don’t think that word means what you think it means
becky in BuggleWorld
OOP
2-2
OOP
2-4
Four properties of Buggles
position: Where becky
sits,
it specified
ifi d b
by an ((x, y))
coordinate.
o heading: The compass
di
direction
ti b
becky
k iis f
facing.
i
o color: becky and her paint
brush’s color.
o brushDown: Is becky
ready to paint?
o
BuggleWorld
becky
the Buggle
These four properties define
the state of a Buggle
OOP
2-3
Dr. Java Interactions Pane
Definitions: Objects and Methods
In Dr. Java, if you compile and run BuggleWorld,
you can perform buggle experiments in the Interactions Pane.
o
An object is a data value that
has state and behaviors
(responses to messages).
messages)
Examples of objects are
buggles, locations, colors,
directions.
o
Behaviors are defined by
methods, which are named
sequences of instructions for
an object
object. E
E.g.,
g buggle
methods include forward,
left, getPosition, etc.
An object
object’ss method is
executed by sending it a
message, also known invoking
the method. Invoking a
method on an object can
report its state or change its
state.
o
OOP
2-5
Go, buggles, go!
bernice
becky
OOP
2-6
Methods with arguments (aren’t we sassy?)
Some methods require additional information when they are
invoked.
o The additional information passed to the method is called an
o
Some sample method
invocations (all these
change state):
argument
becky.forward();
becky
forward();
becky.forward();
becky.left();
betty.backward();
betty
becky.setColor(Color.yellow);
becky.forward(3);
betty
bernice.forward();
bernice
forward()
bernice.brushUp();
bernice.forward();
bernice
b k
becky
OOP
2-7
† You need to use java.awt.Color.red unless you first execute
import java.awt.Color;
OOP
2-8
Definitions: Classes and Instances
o
A class is a description of
the shared
characteristics (state and
behaviors) of a set of
objects.
o
A class is used like a mold
for making objects.
o
An object made from a
class is called an instance
of the class.
A class is described by
instance variables
th t d
that
describe
ib th
the
state of each class
instance; and
instance methods
that define the
behaviors of instances
of
f the
h class
l
in
i
response to messages.
betty
betty
g
and some other things…
But how do we know the
instance variables and
instance methods for a
class?
bernice
becky
OOP
2-9
b k
becky
OOP
2-10
Contracts
Big idea number 1: Abstraction
Q: How do we know the instance variables and instance
methods for a class?
Contract
o
A: Every class has a contract or Application Program Interface
(API) that specifies the behavior of its methods, i.e., how instances
of
f th
the class
l ss respond
s
d to
t messages.
ss
s
o
Any user of a class can expect that objects will behave as described
in
n the contract.
o
Any implementer of the class must ensure that objects fulfill the
contract.
Implementer /
Designer
User / Client
bernice
Cl
Class
Black Box
The CS111 contracts page, http://cs.wellesley.edu/~cs111/contracts/,
has the contracts for most of the classes you will use this semester.
*Visit http://cs.wellesley.edu/~cs111/contracts for some useful Java contracts,
which are known as Application Programming Interfaces (APIs)
(APIs).
Getting Started
1-11
OOP
2-12
Creating New Buggles
A Buggle is Born
Object Land
Location
Buggle becky = new Buggle();
becky.brushUp();
becky.forward(7);
Buggle
gg
x
1
y
1
Buggle bernice = new Buggle();
bernice.setColor(Color.magenta);
bernice.forward(5);
position
h d
heading
Direction
EAST
Buggle betty = new Buggle();
betty.setColor(Color.blue);
color
Color
betty
brushDown
bernice
true
becky
OOP
2-13
OOP
2-14
OOP
2-16
Example of Class and Method Definitions
r
u
n
m
e
t
h
o
d
public class BreakfastWorld extends BuggleWorld
{
public void run ()
constructing a new
{
Buggle object
Buggle becky = new Buggle();
// becky goes outside
becky.forward(2);
becky.left();
becky forward()
becky.forward();
becky.right();
becky.forward();
becky.right();
becky.forward();
becky.left();
// walks to the bagel
becky forward(2);
becky.forward(2);
// and chows down
becky.pickUpBagel();
comments
} // run()
...
} // class BreakfastWorld
Creating becky the Buggle
3 assignment statement
Buggle becky = new Buggle();
1 variable declaration
OOP
2-15
2 constructor method invocation
Behind the curtain
Behind the curtain
new invokes constructor method
new invokes constructor method
Buggle becky = new Buggle();
Buggle becky = new Buggle();
Variable
Declaration
Variable
Declaration
becky
Location
Buggle
assignment
connects the
two
position
1
y
1
heading
g
Direction
EAST
color
Color
brushDown
OOP
x
2-17
true
OOP
2-18
Witches and Wizards
Are there classes other than Buggles?
o
Yep!
o
J
Java
has m
many
y prep
defined classes
o
And we will create many
classes of our own
throughout the semester
public class MagicWorld extends BuggleWorld
{
public void run()
{
Buggle harry = new Buggle();
Location apparateLocation = new Location(4,
Location(4 5);
harry.setPosition(apparateLocation);
}
}
OOP
2-19
OOP
2-20
A new Location object
Objects inside of objects
public class MagicWorld extends BuggleWorld
{
public void run()
{
…
Location apparateLocation = new Location(4, 5);
…
}
}
Location
Creates a new
apparateLocation x
4
instance of the
class Location named
5
apparateLocation
y
Location
Buggle
position
1
y
1
Direction
heading
EAST
color
brushDown
x
Color
true
*What
*
h about
b
contents of
f variable
bl brushDown
b h
inside
d of
faB
Buggle
l object?
b
Is this an object too?
OOP
2-21
o Expressions denote values (including objects)
3
// integer literal
true // boolean literal
4 + 5 // arithmetic expression
becky // variable reference
apparateLocation.x // instance variable reference
Color.yellow // class variable reference
new Buggle() // constructor method invocation
new Location(4, 5) // constructor method invocation
Buggle
(1, 1)
heading
EAST
color
brushDown
2-22
Expressions and Statements
A notational convenience
position
OOP
o Statements perform actions
red
Buggle becky = new Buggle(); // variable declaration and
//
assignment
becky.left(); // instance method invocation
becky.setColor(Color.yellow); // instance method invocation
Location apparateLocation = new Location(4, 5);
// variable declaration and assignment
harry.setPosition(apparateLocation); // inst. Meth. invocation
true
OOP
2-23
OOP
2-24
Nested Expressions
Object-oriented Terminology
Expressions can be nested arbitrarily as long as “grammatical rules”
are obeyed.
o
Object-oriented means we create and manipulate program
objects, which often represent things in the world (my car, you,
becky the buggle).
o
Objects are things that have state and can respond to messages.
When an object receives a message, it executes the
corresponding method ⎯ a named sequence of instructions that
describes some behavior of an object.
o
A class is a description of the shared characteristics of a group
of objects. A class defines the properties (instance variables)
that make up the state of each instance and the methods the
objects understand. E.g., a buggle’s color or forward().
o
An object created based on the class description is an instance of
the class.
o
A constructor method creates a new instance of a class.
b ck f
becky.forward((3*4)
d((3*4) – (2+3));
(2 3));
bernice.setPosition(becky.getPosition());
bernice.setHeading(becky.getHeading().opposite());
(
()
())
bernice.setColor(Color.red.darker().darker());
new Buggle().forward(5); // Careful! Can’t refer to unnamed object later!
OOP
2-25
OOP
2-26