© 2006 pearson addison-wesley. all rights reserved 2.2.1 drawinggizmo « constructor »...

6
© 2006 Pearson Addison-Wesley. All rights reserved 2.2.1 DrawingGizmo «constructor» DrawingGizmo() «update» void moveForward() void turnClockwise() void turnCounterclockwise() void dontDraw() void draw() void delay2Sec() w do you know? (or do you know?) . . . • where a DrawingGizmo object is positioned when instantiated? • how far a DrawingGizmo object moves for each moveForward? the precise angle of turn that results from a call to turnClockwise. (1) You could trust your instructor. You could guess, based on the names of the classes and methods. (3) You could ask the programmer who wrote DrawingGizmo. ) You could read the DrawingGizmo code (if you can find it). (5) You could read the __________________. OR Possible “answers” for such questions...

Upload: osborne-obrien

Post on 19-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: © 2006 Pearson Addison-Wesley. All rights reserved 2.2.1 DrawingGizmo « constructor » DrawingGizmo() « update » void moveForward() void turnClockwise()

© 2006 Pearson Addison-Wesley. All rights reserved 2.2.1

DrawingGizmo

«constructor» DrawingGizmo()

«update» void moveForward() void turnClockwise() void turnCounterclockwise() void dontDraw() void draw() void delay2Sec()

How do you know? (or do you know?) . . . • where a DrawingGizmo object is positioned when instantiated?

• how far a DrawingGizmo object

moves for each moveForward?

• the precise angle of turn that results from

a call to turnClockwise.

(1) You could trust your instructor.

(2) You could guess, based on the names of the classes and methods.

(3) You could ask the programmer who wrote DrawingGizmo.

(4) You could read the DrawingGizmo code (if you can find it).

(5) You could read the __________________.OR

Possible “answers” for such questions...

Page 2: © 2006 Pearson Addison-Wesley. All rights reserved 2.2.1 DrawingGizmo « constructor » DrawingGizmo() « update » void moveForward() void turnClockwise()

© 2006 Pearson Addison-Wesley. All rights reserved 2.2.2

A class specification defines the semantics (behavior) of a class by way of a class invariant to describe what is always true of the class’s objects.

states the conditions that are necessary for the method to properly execute.

specifications for each of the classes methods.

• a _________________ (optional),

• a _________________.

Each method specification consists of

states what is true when the method completes execution.

Page 3: © 2006 Pearson Addison-Wesley. All rights reserved 2.2.1 DrawingGizmo « constructor » DrawingGizmo() « update » void moveForward() void turnClockwise()

© 2006 Pearson Addison-Wesley. All rights reserved 2.2.3

DrawingGizmo Class Specifications

Invariant A DrawingGizmo object …

• appears as an arrow within a “Drawing Canvas” window. (This window is 260 pixels wide and 160 pixels high.)

• is either in drawing mode or moving mode.

Note that all bulleted assertions must be true. (Sometimes and is used in place of bullets.)

Page 4: © 2006 Pearson Addison-Wesley. All rights reserved 2.2.1 DrawingGizmo « constructor » DrawingGizmo() « update » void moveForward() void turnClockwise()

© 2006 Pearson Addison-Wesley. All rights reserved 2.2.4

DrawingTool Class Specifications (continued)

Constructor Method public DrawingGizmo() post: a new DrawingGizmo object is created and placed in the center of the Drawing Canvas window. (Note that one DrawingGizmo may cover another.) and This object is set to drawing mode, so the arrow is colored green. and The arrow for this object is pointing straight up. Update Methods public void draw() post: This object is set to drawing mode, so the arrow is colored green. public void dontDraw() post: This object is set to moving mode, so the arrow is colored red. public void turnClockwise() post: This object’s direction of travel is rotated by 30 degrees clockwise from its previous direction.

public void turnCounterclockwise() post: This object’s direction of travel is rotated by 30 degrees counterclockwise from its previous direction.

Page 5: © 2006 Pearson Addison-Wesley. All rights reserved 2.2.1 DrawingGizmo « constructor » DrawingGizmo() « update » void moveForward() void turnClockwise()

© 2006 Pearson Addison-Wesley. All rights reserved 2.2.5

DrawingGizmo Class Specifications (continued)

public void moveForward() pre: A minimum of 20 pixels remain between the arrow and the edge of Drawing Canvas in the direction of travel. post: This object’s is moved forward by twenty pixels from its previous location. and If this object is in drawing mode, then a line segment is drawn across the twenty pixel path just traversed. (In moving mode nothing is drawn.) public void delay2Sec() post: All drawing activity for the drawing canvas of this DrawingGizmo is suspended for 2 seconds, then resumed.

Think of a class specification as a “legal” for the class.

Page 6: © 2006 Pearson Addison-Wesley. All rights reserved 2.2.1 DrawingGizmo « constructor » DrawingGizmo() « update » void moveForward() void turnClockwise()

© 2006 Pearson Addison-Wesley. All rights reserved 2.2.6

public class Driver { private DrawingGizmo pen, pencil;

public Driver() { pen = new DrawingGizmo(); pen.moveForward(); pen.moveForward(); pencil = new DrawingGizmo(); pencil.dontDraw(); pencil.moveForward(); pencil.moveForward(); pen.turnClockwise(); pen.delay2Sec(); pencil.turnClockwise(); pencil.turnClockwise(); pencil.turnClockwise(); pencil.draw(); pen.moveForward(); pencil.moveForward(); }}

public class Driver { private DrawingGizmo pen, pencil;

public Driver() { pen = new DrawingGizmo(); pen.moveForward(); pen.moveForward(); pencil = new DrawingGizmo(); pencil.dontDraw(); pencil.moveForward(); pencil.moveForward(); pen.turnClockwise(); pen.delay2Sec(); pencil.turnClockwise(); pencil.turnClockwise(); pencil.turnClockwise(); pencil.draw(); pen.moveForward(); pencil.moveForward(); }}

Given the class specifications for DrawingGizmo, explain the behavior of the following program.