Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Object Oriented Programming with Buggles and Bagels - Slides | CS 111, Study notes of Computer Science

Material Type: Notes; Class: LAB:Comp Program & Prob Solv; Subject: Computer Science; University: Wellesley College; Term: Fall 2007;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-on7
koofers-user-on7 🇺🇸

2

(1)

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS111 Computer Programming
Department of Computer Science
Wellesley College
Object Oriented Programming
with Buggles and Bagels
Friday, Sep. 7, 2007
OOP 2-2
Unlearn What You Have Learned
I don’t think that word means what you think it means
argument
parameter
class
variable
object
double
static
float
method
instance
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Object Oriented Programming with Buggles and Bagels - Slides | CS 111 and more Study notes Computer Science in PDF only on Docsity!

CS111 Computer Programming

Department of Computer Science Wellesley College

Object Oriented Programming

with Buggles and Bagels

Friday, Sep. 7, 2007

OOP 2-

Unlearn What You Have Learned

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

argument

becky.setColor(Color.yellow); becky.forward(3);

OOP 2-

Contracts

o Every class has a contract that specifies the behavior

of its methods, i.e., how instances of the class respond

to messages.

o Any user of a class can expect that objects will behave

as described in the contract.

o Any implementer of the class must ensure that objects

fulfill the contract.

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

Buggle becky = new Buggle();

assignment statement

(^1) variable declaration 2 constructor method invocation

OOP 2-

Behind the curtain

Buggle becky = new Buggle();

new invokes constructor method

Variable Declaration

becky (^) assignment connects the two

position

heading

color

brushDown true

Location

Buggle

x

y

1

1

Direction

Color

EAST

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?

EAST

OOP 2-

A notational convenience

position

heading

color

brushDown true

Buggle

EAST

red

(1,1)

OOP 2-

Expressions and Statements

o Expressions denote values and objects

becky Color.yellow 3 new Buggle() apparateLocation new Location(4,5)

o Statements perform actions

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.