26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

294 Methods Chapter 6<br />

To create operations, we examine the verb phrases listed with each class. The phrase<br />

“moves <strong>to</strong> other floor” listed with class Eleva<strong>to</strong>r refers <strong>to</strong> the activity in which the eleva<strong>to</strong>r<br />

moves between floors. Should “moves” be an operation of class Eleva<strong>to</strong>r? The<br />

eleva<strong>to</strong>r decides <strong>to</strong> move in response <strong>to</strong> a but<strong>to</strong>n press. A but<strong>to</strong>n signals the eleva<strong>to</strong>r <strong>to</strong><br />

move, but a but<strong>to</strong>n does not actually move the eleva<strong>to</strong>r—therefore, “moves <strong>to</strong> other floor”<br />

does not correspond <strong>to</strong> an operation. (We include the operations for informing the eleva<strong>to</strong>r<br />

<strong>to</strong> move <strong>to</strong> the other floor later in the discussion, when we discuss the verb phrases associated<br />

with the but<strong>to</strong>ns.) The “arrives at a floor” phrase is also not an operation, because the<br />

eleva<strong>to</strong>r itself decides when <strong>to</strong> arrive on the floor after five seconds of travel.<br />

The “resets eleva<strong>to</strong>r but<strong>to</strong>n” phrase associated with class Eleva<strong>to</strong>r implies that the<br />

eleva<strong>to</strong>r informs the eleva<strong>to</strong>r but<strong>to</strong>n <strong>to</strong> reset. Therefore, class Eleva<strong>to</strong>rBut<strong>to</strong>n needs an<br />

operation <strong>to</strong> provide this service <strong>to</strong> the eleva<strong>to</strong>r. We place this operation (resetBut<strong>to</strong>n) in<br />

the bot<strong>to</strong>m compartment of class Eleva<strong>to</strong>rBut<strong>to</strong>n in our class diagram (Fig. 6.21).<br />

We represent the names of the operations as method names (by following the names<br />

with a pair of parentheses) and include the return type after the colon:<br />

resetBut<strong>to</strong>n() : void<br />

The parentheses can contain a comma-separated list of the parameters that the operation<br />

takes—in this case, none. For the moment, most of our operations have no parameters and a<br />

void return type; this might change as our design and implementation processes proceed.<br />

Eleva<strong>to</strong>rModel<br />

numberOfPeople : Integer=0<br />

addPerson( ) : void<br />

Person<br />

ID : Integer<br />

moving : Boolean = true<br />

doorOpened( ) : void<br />

Eleva<strong>to</strong>rShaft<br />

<br />

<br />

Floor<br />

floorNumber : Integer<br />

capacity : Integer = 1<br />

<br />

Eleva<strong>to</strong>r<br />

moving : Boolean = false<br />

Eleva<strong>to</strong>rBut<strong>to</strong>n<br />

summoned : Boolean = false pressed : Boolean = false<br />

currentFloor : Integer = 1<br />

destinationFloor : Integer = 2<br />

capacity : Integer = 1<br />

travelTime : Integer = 5<br />

resetBut<strong>to</strong>n( ) : void<br />

pressBut<strong>to</strong>n( ) : void<br />

ride( ) : void<br />

FloorBut<strong>to</strong>n<br />

requestEleva<strong>to</strong>r( ) : void<br />

enterEleva<strong>to</strong>r( ) : void<br />

exitEleva<strong>to</strong>r( ) : void<br />

pressed : Boolean = false<br />

resetBut<strong>to</strong>n( ) : void<br />

departEleva<strong>to</strong>r( ) : void pressBut<strong>to</strong>n( ) : void<br />

Fig. Fig. Fig. 6.21 6.21 Classes with attributes and operations.<br />

Eleva<strong>to</strong>rDoor<br />

open : Boolean = false<br />

openDoor( ) : void<br />

closeDoor( ) : void<br />

Light<br />

lightOn : Boolean = false<br />

turnOnLight( ) : void<br />

turnOffLight( ) : void<br />

Bell<br />

<br />

ringBell( ) : void<br />

FloorDoor<br />

open : Boolean = false<br />

openDoor( ) : void<br />

closeDoor( ) : void<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!