Business web site - 404 Object-Based Programming Chapter 8 previously in method

404 Object-Based Programming Chapter 8 previously in method setTime for setting the hour, minuteor second. The addition of these methods caused us to redefine the body of method setTime to follow Software Engineering Observation 8.14 if a method of a class already provides all or part of the functionality required by another method of the class, call that method from the other method. Notice that setTime(lines 51 56) now calls methods setHour, setMinute and setSecond each of which performs part of setTime s task. The new get methods of the class are defined at lines 78 81, 84 87 and 90 93, respectively. Notice that each method simply returns the hour, minute or second value (a copy of each value is returned, because these are all primitive data type variables). The addition of these methods caused us to redefine the bodies of methods toUniversal- String (lines 96 103) and toString (lines 106 115) to follow Software Engineering Observation 8.14. In both cases, every use of instance variables hour, minute and second is replaced with a call to getHour, getMinute and getSecond. Due to the changes in class Time3 just described, we have minimized the changes that will have to occur in the class definition if the data representation is changed from hour, minute and second to another representation (such as total elapsed seconds in the day). Only the new set and get method bodies will have to change. This allows the programmer to change the implementation of the class without affecting the clients of the class (as long as all the public methods of the class are still called the same way). The TimeTest5 applet (Fig. 8.9) provides a graphical user interface that enables the user to exercise the methods of class Time3. The user can set the hour, minute or second value by typing a value in the appropriate JTextField and pressing the Enter key. The user can also click the Add1tosecond button to increment the time by one second. The JTextField and JButton events in this applet are all processed in method actionPerformed (lines 66 94). Notice that lines 34, 41, 48 and 59 all call addActionListener to indicate that the applet should start listening to JTextFields hourField, minuteField, secondField and JButton tickButton, respectively. Also, notice that all four calls use thisas the argument, indicating that the object of our applet class TimeTest5 has its actionPerformed method invoked for each user interaction with these four GUI components. This poses an interesting question how do we determine the GUI component with which the user interacted? In actionPerformed, notice the use of actionEvent.getSource() to determine which GUI component generated the event. For example, line 69 determines whether tickButton was clicked by the user. If so, the body of the if structure executes. Otherwise, the program tests the condition in the if structure at line 73, and so on. Every event has a source the GUI component with which the user interacted to signal the program to do a task. The ActionEvent parameter contains a reference to the source of the event. The condition in line 69 simply asks, Is the source of the event the tick- Button? This condition compares the references on either side of the == operator to determine whether they refer to the same object. In this case, if they both refer to the JButton, then the program knows that the user pressed the button. Remember that the source of the event calls actionPerformed in response to the user interaction. After each operation, the resulting time is displayed as a string in the status bar of the applet. The output windows in Fig. 8.9 illustrate the applet before and after the following operations: setting the hour to 23, setting the minute to 59, setting the second to 58 and incrementing the second twice with the Add1tosecond button. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply