Title: Class Specifications
1Class Specifications
2Class Specification
- What does the DrawingTool do exactly?
- Where is it positioned initially?
- How far does it move forward?
- How much does it rotate?
- How to answer the above questions?
- Ask someone else to tell you
- Guess
- Examine the source code
- Read the documentation
3Class Specification
- A way of defining semantics of a class
- complete and unambiguous definition of meaning
(at least when done formally) - Includes two notions
- Class invariant what is always true about the
class - Method specification a specification of behavior
for a method - A class specification will have
- One class invariant
- A method specification for each method
4Class Specification
- A method specification has three clauses
- precondition (optional)
- States the condition required for the method to
execute - modifies clause (optional)
- A list of variables that might be modified by
this method - postcondition
- States what is true at the conclusion of the
method
5Example Class Specification
6DrawingTool Class Specifications (continued)
Constructor Method public DrawingTool()
postcondition a new
DrawingTool object is created and placed in the
center of the Drawing Canvas
window. (Note that it is possible for one
DrawingTool to cover another.)
This object is set to drawing mode, so the arrow
is colored green. The arrow for
this object is pointing straight up. Update
Methods public void draw()
postcondition This object is set
to drawing mode, so the arrow is colored green.
A 0.5 sec. delay occurs following
this methods execution. public void
dontDraw() postcondition
This object is set to moving mode, so the
arrow is colored red. A 0.5 sec.
delay occurs following this methods execution.
public void turnClockwise()
postcondition This objects
direction of travel is rotated by 90 degrees
clockwise from its previous
(old) direction. A 0.5 sec.
delay occurs following this methods execution.
7Think of a class specification as a legal
contract for the class.
8Exercise 1
- Write a class invariant for TV
- Write a method specification for
- turnSetOn
- raiseVolume1db
9Exercise 2
- Given the Vehicle class diagram, construct a
class specification
10Given the class specifications for DrawingTool,
explain the behavior of the following program.
public class Director private DrawingTool
pen, pencil public Director() pen
new DrawingTool() pen.moveForward()
pen.moveForward() pencil new
DrawingTool() pencil.dontDraw()
pencil.moveForward() pencil.moveForward()
pen.turnClockwise()
pencil.turnClockWise() pencil.turnClockwise
() pencil.turnClockwise()
pencil.draw() pen.moveForward()
pencil.moveForward()