CS 120 Assignment #7Project Specifications1. Implement a class Time that keeps track of time by keeping the hour, minutes and seconds Note: The object will keep the time in military format, in a 24 hour clock. 2. Remember to validate all values! 3. Provide the following methods: · At least 2 constructors. · Provide accessor and mutators for all instance variables. · The toString() method will provide the time in military format. · Implement a method to provide the time in standard format using AM/PM. · Format the display to show two digits for minutes and seconds. · Provide a method timeOfDay() that returns the string “dawn”, “morning”, “afternoon”, “evening” or “night” depending on the time of the day. · Provide a method isMorning() that returns true if it is morning. 4. Provide a test program that prompts the user for time values, creates an object and uses all methods. 5. Provide documentation comments and your name in a comment. Due DateSubmit all zipped files through blackboard.com no later than November 29 by 11:59 pm. | ||
| | A7 >> View/Complete Assignment: A7 | |
| | Solutionimport java.text.DecimalFormat; // used for number formatting // This class maintains the time in 24-hour format
public class Time {
private int hour;
private int minute;
private int second; public Time() {
setTime( 0, 0, 0 );
} public Time( int h, int m, int s ) {
setTime( h, m, s );
} public void setTime( int h, int m, int s ) {
setHour( h );
setMinute( m );
setSecond( s );
} public void setHour( int h ) {
hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
} public void setMinute( int m ) {
minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
} public void setSecond( int s ) {
second = ( ( s >= 0 && s < 60 ) ? s : 0 );
} public String toMilitary()
{
DecimalFormat twoDigits = new DecimalFormat( "00" );return twoDigits.format( hour ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ); } // Convert to String in standard-time format
public String toString()
{
DecimalFormat twoDigits = new DecimalFormat( "00" );
String str = "";
str = ( hour == 12 || hour == 0 ) ? "12" : "" + (hour % 12);
str = str + ":" + twoDigits.format( minute ) + ":" +
twoDigits.format( second );
str = str + ( hour < 12 ? " AM" : " PM" );
return str;
}
public String timeOfDay(){
String str="";
switch ( hour ){
case 1: case 2: case 3: case 4: case 5:
str = "dawn";
break;
case 6: case 7: case 8: case 9: case 10: case 11:
str = "morning";
break;
case 12:
str = "noon";
break;
case 13: case 14: case 15: case 16: case 17:
str = "afternoon";
break;
case 18: case 19:
str = "evening";
break;
default:
str = "night";
}
return str;
}
public boolean isMorning(){
return hour > 6 && hour < 12;
}
public boolean isMidday(){
return hour == 12;
}
public boolean isEvening(){
return hour > 18 && hour < 20;
}
}import javax.swing.JOptionPane; public class TimeTest { public static void main( String [] args )
{
Time t;
String hour = JOptionPane.showInputDialog("Enter hour ");
String minute = JOptionPane.showInputDialog("Enter minutes ");
String second = JOptionPane.showInputDialog("Enter seconds ");
t = new Time(Integer.parseInt(hour), Integer.parseInt(minute), Integer.parseInt(second) );System.out.println( "Military time: " + t.toMilitary() ); System.out.println( "Standard time: " + t.toString() ); System.out.println( "Time of Day: " + t.timeOfDay() ); System.exit(0); } } |
I am student of CIS at - Massachusetts Bay College - Massbay, Wellesley, MA. I would like to share some of my classes, so who is interested in learn computation, feel free to copy, and exchange information. Também falo português e hablo un poquito del español. Nil, 12/11/2010
Total de visualizações de página
sábado, 11 de dezembro de 2010
Implement a class Time that keeps track of time by keeping the hour, minutes and seconds Note: The object will keep the time in military format, in a 24 hour clock.
Assinar:
Postar comentários (Atom)
Nenhum comentário:
Postar um comentário