I tested the files "Time2.java" and "Time2Test.java".
They worked fine.
The following is the output I got:
//----------------------------------------------------
fuh- javac Time2Test.java
fuh- java Time2Test
Constructed with:
t1, all arguments defaulted
00:00:00
12:00:00 AM
t2: hour specified; minute and second defaulted
02:00:00
02:00:00 AM
t3: hour and minute specified; second defaulted
21:34:00
09:34:00 PM
t4: hour, minute and second specified
12:25:42
12:25:42 PM
t5: all invalid values specified
00:00:00
12:00:00 AM
t6: Time2 object t4 specified
12:25:42
12:25:42 PM
fuh-
//----------------------------------------------------
I have enclosed the files below so you can test run them yourself too.
I want you to think about this program this weekend and let
me know why it would work next Tuesday. :)
FC
//----------------------------------------------------------------------
// Time2.java
// Time2 class declaration
public class Time2 extends Object {
private int hour; // 0-23
private int minute; // 0-59
private int second; // 0-59
public Time2( ) { setTime( 0, 0, 0 ); }
//{ this(0, 0, 0); }
public Time2( int h ) { setTime( h, 0, 0 ); }
//{ this(h, 0, 0); }
public Time2( int h, int m ) { setTime( h, m, 0 ); }
//{ this(h, m, 0); }
public Time2( int h, int m, int s ) { setTime( h, m, s ); }
//{ this(h, m, s); }
public Time2( Time2 time ) {
setTime( time.hour, time.minute, time.second ); }
public void setTime( int h, int m, int s ) {
hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
second = ( ( s >= 0 && s < 60 ) ? s : 0 ); }
public int getHour( ) { return hour; }
public int getMinute( ) { return minute; }
public int getSecond( ) { return second; }
// Convert to universal time
public String toUniversalString( ) {
return String.format( "%02d:%02d:%02d",
getHour(), getMinute(), getSecond() );
}
// Convert to standard time
public String toString( ) {
return String.format( "%02d:%02d:%02d %s",
( ( getHour() == 0 || getHour() == 12 ) ? 12: getHour()%12 ),
getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
}
}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// Time2Test.java
// Using overloaded constructors
public class Time2Test
{
public static void main( String args[ ] )
{
Time2 t1 = new Time2( );
Time2 t2 = new Time2( 2 );
Time2 t3 = new Time2( 21, 34 );
Time2 t4 = new Time2( 12, 25, 42 );
Time2 t5 = new Time2( 27, 74, 99 );
Time2 t6 = new Time2( t4 );
System.out.println( "Constructed with: " );
System.out.println( "t1, all arguments defaulted" );
System.out.printf( " %s\n", t1.toUniversalString( ) );
System.out.printf( " %s\n", t1.toString( ) );
System.out.println(
"t2: hour specified; minute and second defaulted" );
System.out.printf( " %s\n", t2.toUniversalString( ) );
System.out.printf( " %s\n", t2.toString( ) );
System.out.println(
"t3: hour and minute specified; second defaulted" );
System.out.printf( " %s\n", t3.toUniversalString( ) );
System.out.printf( " %s\n", t3.toString( ) );
System.out.println( "t4: hour, minute and second specified" );
System.out.printf( " %s\n", t4.toUniversalString( ) );
System.out.printf( " %s\n", t4.toString( ) );
System.out.println( "t5: all invalid values specified" );
System.out.printf( " %s\n", t5.toUniversalString( ) );
System.out.printf( " %s\n", t5.toString( ) );
System.out.println( "t6: Time2 object t4 specified" );
System.out.printf( " %s\n", t6.toUniversalString( ) );
System.out.printf( " %s\n", t6.toString( ) );
}
}
//----------------------------------------------------------------------