CS99 |
Fundamental Programming Concepts
Summer 2001 |
|
|
|
Lab 9: Solutions
class Time {
private int hour; /* 0-23 */
private int minute; /* 0-59 */
private int second; /* 0-59 */
/**
* Default constructor for class Time
* Initializes all the variables to zero.
*/
public Time() {
setTime( 0, 0, 0 );
}
/**
* Construct a Time object given a time in hours
* @param h the time in hours
*/
public Time( int h ) {
setTime( h, 0, 0 );
}
/**
* Construct a Time object given a time in hours and minutes
* @param h the hours
* @param m the minutes
*/
public Time( int h, int m ) {
setTime( h, m, 0 );
}
/**
* Construct a Time object given a time in hours, minutes, and seconds
* @param h the hours
* @param m the minutes
* @param s the seconds
*/
public Time( int h, int m, int s ) {
setTime( h, m, s );
}
// Set Methods
/**
* Set a new Time value using military time.
* @param h the hours
* @param m the minutes
* @param s the seconds
*/
public void setTime( int h, int m, int s ) {
setHour( h );
setMinute( m );
setSecond( s );
}
/**
* Set the hours. Perform validity checks on the data. Set invalid
values to zero.
* @param h the hours
*/
public void setHour( int h ) {
if ( h >= 0 && h < 24 )
hour = h;
else
hour = 0;
}
/**
* Set the minutes. Perform validity checks on the data. Set invalid
values to zero.
* @param m the minutes
*/
public void setMinute( int m ) {
if ( m >= 0 && m < 60 )
minute = m;
else
minute = 0;
}
/**
* Set the seconds. Perform validity checks on the data. Set invalid
values to zero.
* @param s the seconds
*/
public void setSecond( int s ) {
if ( s >= 0 && s < 60 )
second = s;
else
second = 0;
}
// Get methods
/**
* Return the hour.
*/
public int getHour() {
return hour;
}
/**
* Return the minute.
*/
public int getMinute() {
return minute;
}
/**
* Return the second.
*/
public int getSecond() {
return second;
}
/**
* Convert the time to a String in military-time format.
*/
public String toMilitaryString(){
String miliTime = "";
if ( hour < 10 )
miliTime += "0" + hour + ":";
else
miliTime += hour + ":";
if ( minute < 10 )
miliTime += "0" + minute + ":";
else
miliTime += minute + ":";
if ( second < 10 )
miliTime += "0" + second;
else
miliTime += second;
return miliTime;
}
/**
* Convert the time to a String in standard-time format.
*/
public String toString() {
String standardTime = "";
int sHour = 0;
if ( hour == 0 )
sHour = 12;
else
sHour = hour%12;
standardTime += sHour + ":";
if ( minute < 10 )
standardTime += "0" + minute + ":";
else
standardTime += minute + ":";
if ( second < 10 )
standardTime += "0" + second;
else
standardTime += second;
if ( hour > 12 )
standardTime += "PM";
else
standardTime += "AM";
return standardTime;
}
}
|
TimeTest.txt
Enter an hour, minute, and second: 18 30 22
Military Time: 18:30:22
Standard Time: 6:30:22PM
Would you like to continue (y/n)? y
Enter an hour, minute, and second: 5 14 59
Military Time: 05:14:59
Standard Time: 5:14:59AM
Would you like to continue (y/n)? n
ModifiedTime.java
class ModifiedTime {
/*CODE FOR REST OF CLASS SAME AS TIME.JAVA */
/* Increments the time by one hour. Maintains consistent time state. */
public void incrementHour() {
hour = ( hour + 1 )%24;
}
/**
* Increments the time stored by one minute. Maintains consistent time
* state
* by calling incrementHour() if change in minute moves to next hour.
*/
public void incrementMinute() {
minute = (minute + 1)%60;
if ( minute == 0 )
incrementHour();
}
/* Increments the time stored by one second. Maintains object in
consistent state */
public void tick() {
second = (second + 1)%60;
if ( second == 0 )
incrementMinute();
}
}
TimeIncrement.java
class TimeIncrement {
public static void main( String[] args ) {
TokenReader in = new TokenReader( System.in );
int hours, mins, secs;
ModifiedTime t = new ModifiedTime( 5, 6, 59 );
System.out.println("Military Time: " + t.toMilitaryString() );
System.out.println("Standard Time: " + t.toString() );
System.out.println("Incrementing into the next minute: ");
t.tick();
System.out.println("Military Time: " + t.toMilitaryString() );
System.out.println("Standard Time: " + t.toString() );
System.out.println("\n\nNew time.");
t = new ModifiedTime( 5, 59, 45 );
System.out.println("Military Time: " + t.toMilitaryString() );
System.out.println("Standard Time: " + t.toString() );
System.out.println("Incrementing into the next hour: ");
t.incrementMinute();
System.out.println("Military Time: " + t.toMilitaryString() );
System.out.println("Standard Time: " + t.toString() );
System.out.println("\n\nNew time.");
t = new ModifiedTime( 23, 59, 59 );
System.out.println("Military Time: " + t.toMilitaryString() );
System.out.println("Standard Time: " + t.toString() );
System.out.println("Incrementing into the next day: ");
t.tick();
System.out.println("Military Time: " + t.toMilitaryString() );
System.out.println("Standard Time: " + t.toString() );
}
}
/**
OUTPUT:
Military Time: 05:06:59
Standard Time: 5:06:59AM
Incrementing into the next minute:
Military Time: 05:07:00
Standard Time: 5:07:00AM
New time.
Military Time: 05:59:45
Standard Time: 5:59:45AM
Incrementing into the next hour:
Military Time: 06:00:45
Standard Time: 6:00:45AM
New time.
Military Time: 23:59:59
Standard Time: 11:59:59PM
Incrementing into the next day:
Military Time: 00:00:00
Standard Time: 12:00:00AM
*/