Archive for May, 2007

406 Object-Based (Web site construction) Programming Chapter 8 54 displayField.setEditable( false

Thursday, May 3rd, 2007

406 Object-Based Programming Chapter 8 54 displayField.setEditable( false ); 55 container.add( displayField ); 56 57 // set up tickButton 58 tickButton = new JButton( “Add 1 to Second” ); 59 tickButton.addActionListener( this ); 60 container.add( tickButton ); 61 62 updateDisplay(); // update text in displayField 63 } 64 65 // handle button and text field events 66 public void actionPerformed( ActionEvent actionEvent ) 67 { 68 // process tickButton event 69 if ( actionEvent.getSource() == tickButton ) 70 tick(); 71 72 // process hourField event 73 else if ( actionEvent.getSource() == hourField ) { 74 time.setHour( 75 Integer.parseInt( actionEvent.getActionCommand() ) ); 76 hourField.setText( “” ); 77 } 78 79 // process minuteField event 80 else if ( actionEvent.getSource() == minuteField ) { 81 time.setMinute( 82 Integer.parseInt( actionEvent.getActionCommand() ) ); 83 minuteField.setText( “” ); 84 } 85 86 // process secondField event 87 else if ( actionEvent.getSource() == secondField ) { 88 time.setSecond( 89 Integer.parseInt( actionEvent.getActionCommand() ) ); 90 secondField.setText( “” ); 91 } 92 93 updateDisplay(); // update displayField and status bar 94 } 95 96 // update displayField and applet container’s status bar 97 public void updateDisplay() 98 { 99 displayField.setText( “Hour: ” + time.getHour() + 100 “; Minute: ” + time.getMinute() + 101 “; Second: ” + time.getSecond() ); 102 103 showStatus( “Standard time is: ” + time.toString() + 104 “; Universal time is: ” + time.toUniversalString() ); 105 } 106 Fig. 8.9 Using class Time3 s set and get methods (part 2 of 4). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

Chapter 8 Object-Based Programming 405 1 // Fig. (Php web hosting)

Wednesday, May 2nd, 2007

Chapter 8 Object-Based Programming 405 1 // Fig. 8.9: TimeTest5.java 2 // Demonstrating the Time3 class set and get methods. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 // Deitel packages 12 import com.deitel.jhtp4.ch08.Time3; 13 14 public class TimeTest5 extends JApplet 15 implements ActionListener { 16 17 private Time3 time; 18 private JLabel hourLabel, minuteLabel, secondLabel; 19 private JTextField hourField, minuteField, 20 secondField, displayField; 21 private JButton tickButton; 22 23 // Create Time3 object and set up GUI 24 public void init() 25 { 26 time = new Time3(); 27 28 Container container = getContentPane(); 29 container.setLayout( new FlowLayout() ); 30 31 // set up hourLabel and hourField 32 hourLabel = new JLabel( “Set Hour” ); 33 hourField = new JTextField( 10 ); 34 hourField.addActionListener( this ); 35 container.add( hourLabel ); 36 container.add( hourField ); 37 38 // set up minuteLabel and minuteField 39 minuteLabel = new JLabel( “Set minute” ); 40 minuteField = new JTextField( 10 ); 41 minuteField.addActionListener( this ); 42 container.add( minuteLabel ); 43 container.add( minuteField ); 44 45 // set up secondLabel and secondField 46 secondLabel = new JLabel( “Set Second” ); 47 secondField = new JTextField( 10 ); 48 secondField.addActionListener( this ); 49 container.add( secondLabel ); 50 container.add( secondField ); 51 52 // set up displayField 53 displayField = new JTextField( 30 ); Fig. 8.9 Using class Time3 s set and get methods (part 1 of 4). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services

Database web hosting - 404 Object-Based Programming Chapter 8 previously in method

Wednesday, May 2nd, 2007

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
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

Chapter 8 Object-Based Programming 403 (Post office web site) 70 // validate

Wednesday, May 2nd, 2007

Chapter 8 Object-Based Programming 403 70 // validate and set second 71 public void setSecond( int s ) 72 { 73 second = ( ( s >= 0 && s < 60 ) ? s : 0 ); 74 } 75 76 // Get Methods 77 // get hour value 78 public int getHour() 79 { 80 return hour; 81 } 82 83 // get minute value 84 public int getMinute() 85 { 86 return minute; 87 } 88 89 // get second value 90 public int getSecond() 91 { 92 return second; 93 } 94 95 // convert to String in universal-time format 96 public String toUniversalString() 97 { 98 DecimalFormat twoDigits = new DecimalFormat( "00" ); 99 100 return twoDigits.format( getHour() ) + ":" + 101 twoDigits.format( getMinute() ) + ":" + 102 twoDigits.format( getSecond() ); 103 } 104 105 // convert to String in standard-time format 106 public String toString() 107 { 108 DecimalFormat twoDigits = new DecimalFormat( "00" ); 109 110 return ( ( getHour() == 12 || getHour() == 0 ) ? 111 12: getHour() % 12 ) + ":" + 112 twoDigits.format( getMinute() ) + ":" + 113 twoDigits.format( getSecond() ) + 114 ( getHour() < 12 ? " AM" : " PM" ); 115 } 116 117 } // end class Time3 Fig. 8.8 Class Time3with set and get methods (part 3 of 3). The new set methods of the class are defined in Fig. 8.8 at lines 59 62, 65 68 and 71 74, respectively. Notice that each method performs the same conditional statement that was Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

402 Object-Based Programming Chapter 8 20 21 // (Web and email hosting)

Tuesday, May 1st, 2007

402 Object-Based Programming Chapter 8 20 21 // Time3 constructor: hour supplied, minute and second 22 // defaulted to 0 23 public Time3( int h ) 24 { 25 setTime( h, 0, 0 ); 26 } 27 28 // Time3 constructor: hour and minute supplied, second 29 // defaulted to 0 30 public Time3( int h, int m ) 31 { 32 setTime( h, m, 0 ); 33 } 34 35 // Time3 constructor: hour, minute and second supplied 36 public Time3( int h, int m, int s ) 37 { 38 setTime( h, m, s ); 39 } 40 41 // Time3 constructor: another Time3 object supplied 42 public Time3( Time3 time ) 43 { 44 setTime( time.getHour(), time.getMinute(), 45 time.getSecond() ); 46 } 47 48 // Set Methods 49 // Set a new time value using universal time. Perform 50 // validity checks on data. Set invalid values to zero. 51 public void setTime( int h, int m, int s ) 52 { 53 setHour( h ); // set the hour 54 setMinute( m ); // set the minute 55 setSecond( s ); // set the second 56 } 57 58 // validate and set hour 59 public void setHour( int h ) 60 { 61 hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); 62 } 63 64 // validate and set minute 65 public void setMinute( int m ) 66 { 67 minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); 68 } 69 Fig. 8.8 Class Time3with set and get methods (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

Web hosting providers - Chapter 8 Object-Based Programming 401 although set and

Tuesday, May 1st, 2007

Chapter 8 Object-Based Programming 401 although set and get methods could provide access to private data, the access is restricted by the programmer s implementation of the methods. The benefits of data integrity are not automatic simply because instance variables are made private the programmer must provide validity checking. Java provides the framework in which programmers can design better programs in a convenient manner. Software Engineering Observation 8.15 Methods that set the values of private data should verify that the intended new values are proper; if they are not, the set methods should place the private instance variables into an appropriate consistent state. A class s set methods can return values indicating that attempts were made to assign invalid data to objects of the class. This enables clients of the class to test the return values of set methods to determine whether the objects they are manipulating are valid and to take appropriate action if the objects are not valid. In Chapter 14, Exception Handling, we illustrate a more robust way in which clients of a class can be notified if an object is not valid. Good Programming Practice 8.5 Every method that modifies the private instance variables of an object should ensure that the data remains in a consistent state. The applet of Fig. 8.8 (class Time3) and Fig. 8.9 (class TimeTest5) enhances our Timeclass (now called Time3) to include get and set methods for the hour, minuteand secondprivate instance variables. The set methods strictly control the setting of the instance variables to valid values. Attempts to set any instance variable to an incorrect value cause the instance variable to be set to zero (thus leaving the instance variable in a consistent state). Each get method simply returns the appropriate instance variable s value. This applet introduces enhanced GUI event handling techniques as we move toward defining our first full-fledged windowed application. After discussing the code, we introduce how to set up an applet to use classes in programmer-defined packages. 1 // Fig. 8.8: Time3.java 2 // Time3 class definition with set and get methods 3 package com.deitel.jhtp4.ch08; 4 5 // Java core packages 6 import java.text.DecimalFormat; 7 8 public class Time3 extends Object { 9 private int hour; // 0 - 23 10 private int minute; // 0 - 59 11 private int second; // 0 - 59 12 13 // Time3 constructor initializes each instance variable 14 // to zero. Ensures that Time object starts in a 15 // consistent state. 16 public Time3() 17 { 18 setTime( 0, 0, 0 ); 19 } Fig. 8.8 Class Time3with set and get methods (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services

400 Object-Based Programming Chapter 8 Fig. 8.7Using overloaded (Web design company)

Tuesday, May 1st, 2007

400 Object-Based Programming Chapter 8 Fig. 8.7Using overloaded constructors to initialize objects of class Time2(part 3 Fig. of 3). 8.8 Using Set and Get Methods Private instance variables can be manipulated only by methods of the class. A typical manipulation might be the adjustment of a customer s bank balance (e.g., a privateinstance variable of a class BankAccount) by a method computeInterest. Classes often provide publicmethods to allow clients of the class to set (i.e., assign values to) or get (i.e., obtain the values of) private instance variables. These methods need not be called set and get, but they often are. As a naming example, a method that sets instance variable interestRate would typically be named setInterestRate and a method that gets the interestRate would typically be called getInterestRate. Get methods are also commonly called accessor methods or query methods. Set methods are also commonly called mutator methods (because they typically change a value). It would seem that providing set and get capabilities is essentially the same as making the instance variables public. This is another subtlety of Java that makes the language so desirable for software engineering. If an instance variable is public, the instance variable can be read or written at will by any method in the program. If an instance variable is private, a public get method certainly seems to allow other methods to read the data at will but the get method controls the formatting and display of the data. A public set method can and most likely will carefully scrutinize attempts to modify the instance variable s value. This ensures that the new value is appropriate for that data item. For example, an attempt to set the day of the month for a date to 37 would be rejected, an attempt to set a person s weight to a negative value would be rejected, and so on. So, Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

Chapter 8 Object-Based Programming (Bulletproof web design) 399 7 // Deitel

Tuesday, May 1st, 2007

Chapter 8 Object-Based Programming 399 7 // Deitel packages 8 import com.deitel.jhtp4.ch08.Time2; 9 10 public class TimeTest4 { 11 12 // test constructors of class Time2 13 public static void main( String args[] ) 14 { 15 Time2 t1, t2, t3, t4, t5, t6; 16 17 t1 = new Time2(); // 00:00:00 18 t2 = new Time2( 2 ); // 02:00:00 19 t3 = new Time2( 21, 34 ); // 21:34:00 20 t4 = new Time2( 12, 25, 42 ); // 12:25:42 21 t5 = new Time2( 27, 74, 99 ); // 00:00:00 22 t6 = new Time2( t4 ); // 12:25:42 23 24 String output = “Constructed with: ” + 25 “nt1: all arguments defaulted” + 26 “n ” + t1.toUniversalString() + 27 “n ” + t1.toString(); 28 29 output += “nt2: hour specified; minute and ” + 30 “second defaulted” + 31 “n ” + t2.toUniversalString() + 32 “n ” + t2.toString(); 33 34 output += “nt3: hour and minute specified; ” + 35 “second defaulted” + 36 “n ” + t3.toUniversalString() + 37 “n ” + t3.toString(); 38 39 output += “nt4: hour, minute, and second specified” + 40 “n ” + t4.toUniversalString() + 41 “n ” + t4.toString(); 42 43 output += “nt5: all invalid values specified” + 44 “n ” + t5.toUniversalString() + 45 “n ” + t5.toString(); 46 47 output += “nt6: Time2 object t4 specified” + 48 “n ” + t6.toUniversalString() + 49 “n ” + t6.toString(); 50 51 JOptionPane.showMessageDialog( null, output, 52 “Demonstrating Overloaded Constructors”, 53 JOptionPane.INFORMATION_MESSAGE ); 54 55 System.exit( 0 ); 56 } 57 58 } // end class TimeTest4 Fig. 8.7Using overloaded constructors to initialize objects of class Time2(part 2 Fig. of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services