Chapter 4
|
|
|
Specification of a simple class |
This chapter discusses
|
|
|
|
How to write the specifications
for a class. |
|
The precise description of features
common to all instances of the class. |
|
Features of the server seen by the
client. |
|
Distinct from the implementation. |
|
The client-server relationship between
two objects. |
|
Several example class specifications. |
Client and Server
|
|
|
|
A client queries and commands a server. |
|
Queries ascertain values of properties. |
|
Commands change its state. |
|
A client uses a server. |
Client and Server (cont.)
Specification and
implementation
|
|
|
specification: an object’s features, as
seen by its clients. |
|
implementation: the “internals” that
make up the features. |
Specification and
implementation (cont.)
|
|
|
|
Specifications isolate you from the
details of the implementation. |
|
“I don’t care how you do it, just get
the job done”(to specifications). |
|
How the features are actually
implemented by the server, is of no concern to the client. |
|
Preserving the distinction between
specification and implementation is absolutely essential. |
Let’s start specifying
|
|
|
|
First, enumerate the object’s
responsibilities. Then determine its
properties and commands. |
|
Java syntax does not allow us to
separate a class specification from its implementation. |
|
|
A simple counter
|
|
|
|
A simple counter’s responsibilities: |
|
|
|
Know (one query): |
|
the value of the count |
|
Do (two commands): |
|
set the count to 0 |
|
increment the count by 1 |
A simple counter (cont.)
|
|
|
|
A simple counter |
|
Class:Counter |
|
Queries (Properties): |
|
count (A non-negative integer) |
|
Commands: |
|
reset (sets count value to 0) |
|
step_count (increments the
count by 1) |
|
|
|
|
Slide 10
Specification
documentation
|
|
|
Tools such as javadoc generate sets of
HTML documents containing specifications extracted from program source files. |
Slide 12
Invoking a method
|
|
|
method: a language construct that
defines and implements a query or command. |
|
In order to invoke a method, you must
have an instance of the class call the method. |
|
Syntax:instance.method() |
|
Example: c.count() |
Slide 14
Slide 15
Maze game example
|
|
|
An explorer (player) must navigate
successfully through the rooms without being killed by denizen (monsters). |
|
There can be several rooms and denizen,
but only one explorer. |
|
We need 3 classes: Explorer, Denizen,
and Room. |
Explorer responsibilities
|
|
|
Know: |
|
his name |
|
his location in the maze |
|
damage inflicted upon an opponent |
|
damage received from an opponent |
|
his stamina |
|
Do: |
|
set or change name |
|
change location |
|
fight a maze Denizen |
Slide 18
Invoking methods with
parameters
|
|
|
To be invoked properly, a method
sometimes requires information from the client. Information is passed in as parameters. |
|
When a client invokes a method, the
client must provide a value of the appropriate type for each parameter. |
|
Syntax:instance.method(p0,p1,p2…) |
|
The only way an object’s state can
change is by invoking one of the object’s methods. |
Parameters and arguments
|
|
|
If we wish to change an explorer’s
name, we must provide a new name. |
|
If we wish to change an explorer’s
location, we must provide either a new location or a movement from which the
location can be determined. |
|
The needed elements are referred to as
the parameters of the command. |
|
The actual values we provide are
referred to as arguments. |
|
|
Slide 21
Slide 22
Constructors
|
|
|
|
Can be used to set the initial values
of an objects properties. |
|
Examples: |
|
A counter’s initial value of 0. |
|
An explorer’s initial name, location,
strength, and stamina. |
|
|
Slide 24
Slide 25
Class Student
|
|
|
|
|
Consider a university registration
system. |
|
Students register for courses. |
|
Each student is represented by a
distinct object. |
|
All students are members of the Student
class. |
|
The university |
|
Assesses fees. |
|
Prints student schedules. |
|
Produces class rolls. |
|
Etc. |
|
|
|
|
Queries
|
|
|
public String name () |
|
public String address () |
|
public String ssn () |
|
public int creditHours () |
|
public int fees () |
|
public int feesPaid () |
|
public courses.CourseList schedule() |
Slide 28
Commands
|
|
|
public void changeName () |
|
public void changeAddress () |
|
public void changeSsn () |
|
public void addCourse () |
|
public void dropCourse () |
|
public void payFees () |
Contructors
|
|
|
public Student (String name, String
address, String ssn) |
We’ve covered
|
|
|
Specification of classes in Java. |
|
Client-Server relationships. |
|
Definitions of specification and implementation. |
|
Several examples of simple class
implementation. |
Glossary
Glossary (cont.)