1 // Fig. 8.1: Time1.java
2 // Time1 class declaration maintains the time in 24-hour format.
3 import java.text.DecimalFormat;
4
5 public class Time1 extends Object {
6 private int hour; // 0 - 23
7 private int minute; // 0 - 59
8 private int second; // 0 - 59
9
10 // Time1 constructor initializes each instance variable to zero;
11 // ensures that each Time1 object starts in a consistent state
12 public Time1()
13 {
14 setTime( 0, 0, 0 );
15 }
16
17 // set a new time value using universal time; perform
18 // validity checks on the data; set invalid values to zero
19 public void setTime( int h, int m, int s )
20 {
21 hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
22 minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
23 second = ( ( s >= 0 && s < 60 ) ? s : 0 );
24 }
25
26 // convert to String in universal-time format
27 public String toUniversalString()
28 {
29 DecimalFormat twoDigits = new DecimalFormat( "00" );
30
31 return twoDigits.format( hour ) + ":" +
32 twoDigits.format( minute ) + ":" + twoDigits.format( second );
33 }
34
35 // convert to String in standard-time format
36 public String toStandardString()
37 {
38 DecimalFormat twoDigits = new DecimalFormat( "00" );
39
40 return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" +
41 twoDigits.format( minute ) + ":" + twoDigits.format( second ) +
42 ( hour < 12 ? " AM" : " PM" );
43 }
44
45 } // end class Time1
1 // Fig. 8.2: TimeTest1.java
2 // Class TimeTest1 to exercise class Time1.
3 import javax.swing.JOptionPane;
4
5 public class TimeTest1 {
6
7 public static void main( String args[] )
8 {
9 Time1 time = new Time1(); // calls Time1 constructor
10
11 // append String version of time to String output
12 String output = "The initial universal time is: " +
13 time.toUniversalString() + "\nThe initial standard time is: " +
14 time.toStandardString();
15
16 // change time and append updated time to output
17 time.setTime( 13, 27, 6 );
18 output += "\n\nUniversal time after setTime is: " +
19 time.toUniversalString() +
20 "\nStandard time after setTime is: " + time.toStandardString();
21
22 // set time with invalid values; append updated time to output
23 time.setTime( 99, 99, 99 );
24 output += "\n\nAfter attempting invalid settings: " +
25 "\nUniversal time: " + time.toUniversalString() +
26 "\nStandard time: " + time.toStandardString();
27
28 JOptionPane.showMessageDialog( null, output,
29 "Testing Class Time1", JOptionPane.INFORMATION_MESSAGE );
30
31 System.exit( 0 );
32
33 } // end main
34
35 } // end class TimeTest1
1 // Fig. 8.3: TimeTest2.java
2 // Errors resulting from attempts to access private members of Time1.
3 public class TimeTest2 {
4
5 public static void main( String args[] )
6 {
7 Time1 time = new Time1();
8
9 time.hour = 7; // error: hour is a private instance variable
10 time.minute = 15; // error: minute is a private instance variable
11 time.second = 30; // error: second is a private instance variable
12 }
13
14 } // end class TimeTest2
1 // Fig. 8.4: ThisTest.java
2 // Using the this reference to refer to instance variables and methods.
3 import javax.swing.*;
4 import java.text.DecimalFormat;
5
6 public class ThisTest {
7
8 public static void main( String args[] )
9 {
10 SimpleTime time = new SimpleTime( 12, 30, 19 );
11
12 JOptionPane.showMessageDialog( null, time.buildString(),
13 "Demonstrating the \"this\" Reference",
14 JOptionPane.INFORMATION_MESSAGE );
15
16 System.exit( 0 );
17 }
18
19 } // end class ThisTest
20
21 // class SimpleTime demonstrates the "this" reference
22 class SimpleTime {
23 private int hour;
24 private int minute;
25 private int second;
26
27 // constructor uses parameter names identical to instance variable
28 // names; "this" reference required to distinguish between names
29 public SimpleTime( int hour, int minute, int second )
30 {
31 this.hour = hour; // set "this" object's hour
32 this.minute = minute; // set "this" object's minute
33 this.second = second; // set "this" object's second
34 }
35
36 // use explicit and implicit "this" to call toStandardString
37 public String buildString()
38 {
39 return "this.toStandardString(): " + this.toStandardString() +
40 "\ntoStandardString(): " + toStandardString();
41 }
42
43 // return String representation of SimpleTime
44 public String toStandardString()
45 {
46 DecimalFormat twoDigits = new DecimalFormat( "00" );
47
48 // "this" is not required here, because method does not
49 // have local variables with same names as instance variables
50 return twoDigits.format( this.hour ) + ":" +
51 twoDigits.format( this.minute ) + ":" +
52 twoDigits.format( this.second );
53 }
54
55 } // end class SimpleTime
1 // Fig. 8.5: Time2.java
2 // Time2 class declaration with overloaded constructors.
3 import java.text.DecimalFormat;
4
5 public class Time2 {
6 private int hour; // 0 - 23
7 private int minute; // 0 - 59
8 private int second; // 0 - 59
9
10 // Time2 constructor initializes each instance variable to zero;
11 // ensures that Time object starts in a consistent state
12 public Time2()
13 {
14 this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
15 }
16
17 // Time2 constructor: hour supplied, minute and second defaulted to 0
18 public Time2( int h )
19 {
20 this( h, 0, 0 ); // invoke Time2 constructor with three arguments
21 }
22
23 // Time2 constructor: hour and minute supplied, second defaulted to 0
24 public Time2( int h, int m )
25 {
26 this( h, m, 0 ); // invoke Time2 constructor with three arguments
27 }
28
29 // Time2 constructor: hour, minute and second supplied
30 public Time2( int h, int m, int s )
31 {
32 setTime( h, m, s ); // invoke setTime to validate time
33 }
34
35 // Time2 constructor: another Time2 object supplied
36 public Time2( Time2 time )
37 {
38 // invoke Time2 constructor with three arguments
39 this( time.hour, time.minute, time.second );
40 }
41
42 // set a new time value using universal time; perform
43 // validity checks on data; set invalid values to zero
44 public void setTime( int h, int m, int s )
45 {
46 hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
47 minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
48 second = ( ( s >= 0 && s < 60 ) ? s : 0 );
49 }
50
51 // convert to String in universal-time format
52 public String toUniversalString()
53 {
54 DecimalFormat twoDigits = new DecimalFormat( "00" );
55
56 return twoDigits.format( hour ) + ":" +
57 twoDigits.format( minute ) + ":" + twoDigits.format( second );
58 }
59
60 // convert to String in standard-time format
61 public String toStandardString()
62 {
63 DecimalFormat twoDigits = new DecimalFormat( "00" );
64
65 return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" +
66 twoDigits.format( minute ) + ":" + twoDigits.format( second ) +
67 ( hour < 12 ? " AM" : " PM" );
68 }
69
70 } // end class Time2
1 // Fig. 8.6: TimeTest3.java
2 // Overloaded constructors used to initialize Time2 objects.
3 import javax.swing.*;
4
5 public class TimeTest3 {
6
7 public static void main( String args[] )
8 {
9 Time2 t1 = new Time2(); // 00:00:00
10 Time2 t2 = new Time2( 2 ); // 02:00:00
11 Time2 t3 = new Time2( 21, 34 ); // 21:34:00
12 Time2 t4 = new Time2( 12, 25, 42 ); // 12:25:42
13 Time2 t5 = new Time2( 27, 74, 99 ); // 00:00:00
14 Time2 t6 = new Time2( t4 ); // 12:25:42
15
16 String output = "Constructed with: " +
17 "\nt1: all arguments defaulted" +
18 "\n " + t1.toUniversalString() +
19 "\n " + t1.toStandardString();
20
21 output += "\nt2: hour specified; minute and second defaulted" +
22 "\n " + t2.toUniversalString() +
23 "\n " + t2.toStandardString();
24
25 output += "\nt3: hour and minute specified; second defaulted" +
26 "\n " + t3.toUniversalString() +
27 "\n " + t3.toStandardString();
28
29 output += "\nt4: hour, minute and second specified" +
30 "\n " + t4.toUniversalString() +
31 "\n " + t4.toStandardString();
32
33 output += "\nt5: all invalid values specified" +
34 "\n " + t5.toUniversalString() +
35 "\n " + t5.toStandardString();
36
37 output += "\nt6: Time2 object t4 specified" +
38 "\n " + t6.toUniversalString() +
39 "\n " + t6.toStandardString();
40
41 JOptionPane.showMessageDialog( null, output,
42 "Overloaded Constructors", JOptionPane.INFORMATION_MESSAGE );
43
44 System.exit( 0 );
45
46 } // end main
47
48 } // end class TimeTest3
Exercise #2
Modify the Time3 class of Fig. 8.6 to include the tick method that increments the time stored in a Time3 object by one second. Also provide method incrementMinute to increment the minute and method incrementHour to increment the hour. The Time3 object should always remain in a consistent state. Write a driver program that tests the tick method, the incrementMinute method and the incrementHour method to ensure that they work correctly. Be sure to test the following cases:
1. Incrementing into the next minute.
2. Incrementing into the next hour.
3. Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
0 Comments