pictureworld - wellesley cscs111.wellesley.edu/.../notes/lectures/lecture05pictureworld.pdf · 7...

8
1 CS111 Computer Programming Department of Computer Science Wellesley College PictureWorld A world of fruitful methods Tuesday, September 19, 2006 PictureWorld 5-2 Buggles and BuggleWorld Primitives Simple values: e.g, integers, booleans, … Black-box objects: e.g., buggles, points, colors Black-box methods: forward(), left(), … Means of Combination Arithmetic on numbers: 1 + 2 Instance variable reference: p.x Class variable reference: Color.red Void method invocation: becky.forward() Nonvoid method invocation: becky.getColor() Constructor method invocation: new Point(1,2) Sequences of statements: bob.forward(3); bob.left(); Means of abstraction Creating our own methods: rollover(), dogHouse() Creating our own classes: LetterBuggle

Upload: others

Post on 24-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PictureWorld - Wellesley CScs111.wellesley.edu/.../notes/lectures/Lecture05PictureWorld.pdf · 7 PictureWorld 5-13 Invocation Trees An invocation tree is an abbreviation of a tree

1

CS111 Computer Programming

Department of Computer ScienceWellesley College

PictureWorld

A world of fruitful methods

Tuesday, September 19, 2006

PictureWorld 5-2

Buggles and BuggleWorldPrimitives

Simple values: e.g, integers, booleans, …Black-box objects: e.g., buggles, points, colorsBlack-box methods: forward(), left(), …

Means of CombinationArithmetic on numbers: 1 + 2Instance variable reference: p.xClass variable reference: Color.redVoid method invocation: becky.forward()Nonvoid method invocation: becky.getColor()Constructor method invocation: new Point(1,2)Sequences of statements: bob.forward(3); bob.left();

Means of abstractionCreating our own methods: rollover(), dogHouse()Creating our own classes: LetterBuggle

Page 2: PictureWorld - Wellesley CScs111.wellesley.edu/.../notes/lectures/Lecture05PictureWorld.pdf · 7 PictureWorld 5-13 Invocation Trees An invocation tree is an abbreviation of a tree

2

PictureWorld 5-3

PictureWorld

o A world for studying (1) fruitful methodsand (2) notion that complex structurescan be built from simple primitives usingpowerful means of combination and meansof abstraction.

o Each picture exists within the unitsquare, and understands one method: howto stretch itself to fill a given rectanglein the applet.

o PictureWorld’s GUI is much simplier thanBuggleWorld’s.

(0.0,0.0)

(1.0,1.0)

x

y

PictureWorld 5-4

Some “Primitive” Pictures

bp(blue patch) mark

gw(green wedge)

leavesrp(red patch)

empty

Page 3: PictureWorld - Wellesley CScs111.wellesley.edu/.../notes/lectures/Lecture05PictureWorld.pdf · 7 PictureWorld 5-13 Invocation Trees An invocation tree is an abbreviation of a tree

3

PictureWorld 5-5

Adding Pictures to a PictureWorldpublic class SimplePictureWorld extends PictureWorld {

public void initializePictureChoices() {

// Create new “primitive” pictures // (they’re not really primitive, as we’ll see later) Picture bp = this.patch(Color.blue); // blue patch Picture rp = this.patch(Color.red); // red patch Picture mark = this.checkmark(Color.red, Color.blue); // red/blue check Picture gw = this.wedge(Color.green); // green wedge Picture leaves = this.twoLeaves(Color.green); // green leaves

// Add pictures to the menu this.addPictureChoice(“blue patch”, bp); this.addPictureChoice(“red patch”, rp); this.addPictureChoice(“mark”, mark); this.addPictureChoice(“green wedge”, gw); this.addPictureChoice(“leaves”, leaves);

// Notes: // 1. “this.” is optional; can omit it! // 2. Can always use Picture expression in place of name. // For example: // addPictureChoice(“mark”, checkMark(Color.red, Color.blue)) } ...}

PictureWorld 5-6

PictureWorld contract// Automatically invoked when a PictureWorld applet is createdpublic void initializePictureChoices();

// Adds name to the menu and associates it with picpublic void addPictureChoice(String name, Picture pic);

public Picture empty(); // Returns an empty picture

We’ll now explore many more methods in the contract …

Page 4: PictureWorld - Wellesley CScs111.wellesley.edu/.../notes/lectures/Lecture05PictureWorld.pdf · 7 PictureWorld 5-13 Invocation Trees An invocation tree is an abbreviation of a tree

4

PictureWorld 5-7

Rotating Picturespublic Picture clockwise90(Picture p) // Returns p rotated 90° clockwisepublic Picture clockwise180(Picture p) // Returns p rotated 180° clockwisepublic Picture clockwise270(Picture p) // Returns p rotated 270° clockwise

gw clockwise90(gw)�

clockwise180(gw)�

clockwise270(gw)�

PictureWorld 5-8

Flipping Picturespublic Picture flipHorizontally(Picture p) // Returns p flipped across horiz axispublic Picture flipVertically(Picture p) // Returns p flipped across vert axispublic Picture flipDiagonally(Picture p) // Returns p flipped acros diag axis

gw flipVertically(gw)�

flipHorizontally(gw)�

flipDiagonally(gw)�

Page 5: PictureWorld - Wellesley CScs111.wellesley.edu/.../notes/lectures/Lecture05PictureWorld.pdf · 7 PictureWorld 5-13 Invocation Trees An invocation tree is an abbreviation of a tree

5

PictureWorld 5-9

Putting one picture beside another

// Returns picture resulting from putting p1 beside p2public Picture beside(Picture p1, Picture p2)

// Returns picture resulting from putting p1 beside p2,// where p1 takes give fraction of rectangle.public Picture beside(Picture p1, Picture p2, double fraction)

beside(gw,rp)� beside(gw,rp,0.25)� beside(gw,rp,0.75)�

PictureWorld 5-10

Putting one picture above another

// Returns picture resulting from putting p1 above p2public Picture above(Picture p1, Picture p2)

// Returns picture resulting from putting p1 above p2,// where p1 takes give fraction of rectangle.public Picture above(Picture p1, Picture p2, double fraction)

above(gw,rp)� above(gw,rp,0.25)� above(gw,rp,0.75)�

Page 6: PictureWorld - Wellesley CScs111.wellesley.edu/.../notes/lectures/Lecture05PictureWorld.pdf · 7 PictureWorld 5-13 Invocation Trees An invocation tree is an abbreviation of a tree

6

PictureWorld 5-11

Putting one picture over another// Returns picture resulting from overlaying p1 on top of p2public Picture overlay(Picture p1, Picture p2)

overlay(mark,leaves)� overlay(leaves,mark)�

PictureWorld 5-12

Combining Four Picturespublic Picture fourPics(Picture p1, Picture p2, Picture p3, Picture p4) { return above(beside(p1, p2), beside(p3,p4));}

public Picture fourSame(Picture p) { return fourPics(p,p,p,p);}

fourPics(bp,gw,mark,rp)� fourSame(leaves)�

Page 7: PictureWorld - Wellesley CScs111.wellesley.edu/.../notes/lectures/Lecture05PictureWorld.pdf · 7 PictureWorld 5-13 Invocation Trees An invocation tree is an abbreviation of a tree

7

PictureWorld 5-13

Invocation TreesAn invocation tree is an abbreviation of a tree of JEM frames. It isespecially helpful for understanding computations involving fruitful methods.

SPW .fourSame( P1 ): P3SPW . fourPics( P1 , P1 ,

P1 , P1 )

SPW . beside( P1 , P1 ): P2a

SPW . beside( P1 , P1 ): P2b

SPW . above( P2a , P2b ): P3

ExecutionLand

ObjectLand

P1 P2a P2b P3Picture Picture Picture PictureSPW

SimplePictureWorld

PictureWorld 5-14

Repeated Tilingpublic Picture tiling (Picture p) { return fourSame(fourSame(fourSame(fourSame(p))));}

tiling(mark)� tiling(gw)

Page 8: PictureWorld - Wellesley CScs111.wellesley.edu/.../notes/lectures/Lecture05PictureWorld.pdf · 7 PictureWorld 5-13 Invocation Trees An invocation tree is an abbreviation of a tree

8

PictureWorld 5-15

Rotation Combinatorspublic Picture rotations (Picture p) { return fourPics(clockwise270(p), p, clockwise180(p), clockwise90(p));}

public Picture rotations2 (Picture p) { return fourPics(p, clockwise90(p), clockwise180(p), clockwise270(p));}

rotations(gw)� rotations2(gw)

PictureWorld 5-16

A Simple Recipe for Complexitypublic Picture wallpaper (Picture p) { return rotations(rotations(rotations(rotations(p)));}

public Picture design (Picture p) { return rotations2(rotations2(rotations2(rotations2(p)));}

wallpaper(gw)� design(gw)