Chapter 10 Strings and Characters 583 thirteen subscripts (Web site developers)

Chapter 10 Strings and Characters 583 thirteen subscripts of the faces array), and the calculation count/13always results in a value from 0 to 3 (the four subscripts in the suits array). When the deck array is initialized, it contains the cards with faces ace through king in order for each suit. When the user clicks the Deal card button, method actionPerformed at lines 48 62 invokes method dealCard (defined at lines 118 126) to get the next card in the array. If the deck is not empty, a Card object reference is returned; otherwise, null is returned. If the reference is not null, lines 53 54 display the Cardin the JTextField displayField and display the card number in the JLabelstatusLabel. If the reference returned by dealCard was null, the String NOMORECARDSTODEAL is displayed in the JTextField and the String Shuffle cards to continue is displayed in the JLabel. When the user clicks the Shuffle cards button, its actionPerformed method at lines 77 82 invokes method shuffle(defined on lines 102 115) to shuffle the cards. The method loops through all 52 cards (array subscripts 0 to 51). For each card, a number between 0 and 51 is picked randomly. Next, the current Card object and the randomly selected Card object are swapped in the array. A total of only 52 swaps are made in a single pass of the entire array, and the array of Card objects is shuffled! When the shuffling is complete, the String DECKISSHUFFLED is displayed in the JTextField. Notice the use of method setEnabled at lines 114 and 123 to activate and deactivate the dealButton. Method setEnabledcan be used on many GUI components. When it is called with a false argument, the GUI component for which it is called is disabled so the user cannot interact with it. To reactivate the button, method setEnabled is called with a true argument. 10.22 (Optional Case Study) Thinking About Objects: Event Handling Objects do not ordinarily perform their operations spontaneously. Rather, a specific operation is normally invoked when a sending object (a client object) sends a message to a receiving object (a server object) requesting that the receiving object perform that specific operation. In earlier sections, we mentioned that objects interact by sending and receiving messages. We began to model the behavior of our elevator system by using statechart and activity diagrams in Section 5.11 and collaboration diagrams in Section 7.10. In this section, we discuss how the objects of the elevator system interact. Events In Fig. 7.24, we presented an example of a person pressing a button by sending a press- Button message to the button specifically, the Person object called method press- Button of the Button object. This message describes an action that is currently happening; in other words, the Person presses a Button. In general, the message name structure is a verb preceding a noun e.g., the name of the pressButton message consists of the verb press followed by the noun button. An event is a message that notifies an object of an action that has already happened. For example, in this section, we modify our simulation so the Elevator sends an elevatorArrived event to the Elevator s Door when the Elevator arrives at a Floor. In Section 7.10, the Elevator opens this Door directly by sending an openDoor message. Listening for an elevatorArrived event allows the Door to determine the actions Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply