






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: LAB:Comp Program & Prob Solv; Subject: Computer Science; University: Wellesley College; Term: Fall 2007;
Typology: Study notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!
Department of Computer Science Wellesley College
Friday, Sep. 7, 2007
OOP 2-
I don’t think that word means what you think it means
argument
parameter
class
variable
object
double
static
method float
instance
OOP 2-
becky in BuggleWorld
becky the Buggle
BuggleWorld
OOP 2-
Four properties of Buggles
o position: Where becky sits, specified by an (x, y) coordinate. o heading: The compass direction becky is facing. o color: becky and her paint brush’s color. o brushDown: Is becky ready to paint?
*Collectively these four properties define the state of a Buggle.
OOP 2-
A class is described by
instance variables that describe the properties of each class instance; and
instance methods that are the messages to which an instance of the class can respond.
becky
betty
bernice
OOP 2-
Methods with arguments (aren’t we sassy?)
o Some methods require additional information when they are invoked. o The additional information passed to the method is called an
becky.setColor(Color.yellow); becky.forward(3);
OOP 2-
Contracts
OOP 2-
Creating New Buggles
becky
betty
bernice
Buggle becky = new Buggle(); becky.brushUp(); becky.forward(7);
Buggle bernice = new Buggle(); bernice.setColor(Color.magenta); bernice.forward(5);
Buggle betty = new Buggle(); betty.setColor(Color.blue);
OOP 2-
An Example of a Class and Method public class BreakfastWorld extends BuggleWorld { public void run () { Buggle becky = new Buggle(); // becky goes outside becky.forward(2); becky.left(); becky.forward(); becky.right(); becky.forward(); becky.right(); becky.forward(); becky.left(); // walks to the bagel becky.forward(2); // and chows down becky.pickUpBagel(); } // run()
... } // class BreakfastWorld
constructing a new Buggle object
comments
r u n m e t h o d
OOP 2-
Creating becky the Buggle
assignment statement
(^1) variable declaration 2 constructor method invocation
OOP 2-
Behind the curtain
new invokes constructor method
Variable Declaration
becky (^) assignment connects the two
position
heading
color
brushDown true
Location
Buggle
x
y
1
1
Direction
Color
OOP 2-
Are there classes other than Buggles?
o Yep!
o Java has many pre- defined classes
o And we will create many classes of our own throughout the semester
OOP 2-
Objects inside of objects
position
heading
color
brushDown true
Location
Buggle
x
y
1
1
Direction
Color
*What about contents of variablebrushDown inside of a Buggle object? Is this an object too?
OOP 2-
A notational convenience
position
heading
color
brushDown true
Buggle
EAST
red
(1,1)
OOP 2-
Expressions and Statements
becky Color.yellow 3 new Buggle() apparateLocation new Location(4,5)
becky.left(); becky.setColor(Color.yellow); Buggle bernice = new Buggle(); bernice.forward(5); Location apparateLocation = new Location(4,5); harry.setPosition(apparateLocation);
OOP 2-
Object-oriented Terminology
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.