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.

1408 Eleva<strong>to</strong>r Model (on CD) Appendix H<br />

Eleva<strong>to</strong>r has departed and invokes method eleva<strong>to</strong>rArrived (lines 137–140)<br />

when the Eleva<strong>to</strong>r has arrived. Method eleva<strong>to</strong>rArrived calls method open-<br />

Door—the Door opens when the Eleva<strong>to</strong>r has arrived. Method eleva<strong>to</strong>rDeparted<br />

does not perform any action. At a first glance, you may wonder why this method<br />

does not call method closeDoor. The reason is that a Door should close before the<br />

Eleva<strong>to</strong>r has departed so the passenger will not be injured—in our model, the Eleva<strong>to</strong>r<br />

calls method closeDoor before calling eleva<strong>to</strong>rDeparted.<br />

H.5 Class But<strong>to</strong>n<br />

But<strong>to</strong>ns (Fig. H.8) are important <strong>to</strong> the model as well, because they signal the Eleva<strong>to</strong>r<br />

<strong>to</strong> move between Floors. Figure 15.12 indicates that class But<strong>to</strong>n contains boolean<br />

attribute pressed (line 14) <strong>to</strong> represent the state of the But<strong>to</strong>n (pressed or reset).<br />

Figure 15.12 indicates also that class But<strong>to</strong>n contains methods pressBut<strong>to</strong>n (lines<br />

23–29) and resetBut<strong>to</strong>n (lines 32–38).<br />

1 // But<strong>to</strong>n.java<br />

2 // Sends But<strong>to</strong>nEvents <strong>to</strong> But<strong>to</strong>nListeners when accessed<br />

3 package com.deitel.jhtp4.eleva<strong>to</strong>r.model;<br />

4<br />

5 // Deitel packages<br />

6 import com.deitel.jhtp4.eleva<strong>to</strong>r.event.*;<br />

7<br />

8 public class But<strong>to</strong>n implements Eleva<strong>to</strong>rMoveListener {<br />

9<br />

10 // But<strong>to</strong>nListener<br />

11 private But<strong>to</strong>nListener but<strong>to</strong>nListener = null;<br />

12<br />

13 // represent whether But<strong>to</strong>n is pressed<br />

14 private boolean pressed = false;<br />

15<br />

16 // set listener<br />

17 public void setBut<strong>to</strong>nListener( But<strong>to</strong>nListener listener )<br />

18 {<br />

19 but<strong>to</strong>nListener = listener;<br />

20 }<br />

21<br />

22 // press But<strong>to</strong>n and send But<strong>to</strong>nEvent<br />

23 public void pressBut<strong>to</strong>n( Location location )<br />

24 {<br />

25 pressed = true;<br />

26<br />

27 but<strong>to</strong>nListener.but<strong>to</strong>nPressed(<br />

28 new But<strong>to</strong>nEvent( this, location ) );<br />

29 }<br />

30<br />

31 // reset But<strong>to</strong>n and send But<strong>to</strong>nEvent<br />

32 public void resetBut<strong>to</strong>n( Location location )<br />

33 {<br />

34 pressed = false;<br />

Fig. Fig. Fig. H.8 H.8 H.8 Class But<strong>to</strong>n, which represents a But<strong>to</strong>n in the model, informs listeners<br />

when a But<strong>to</strong>n has been pressed or reset (part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!