Join 150,427 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,078 people online right now. Registration is fast and FREE... Join Now!
i dont know what to do. here is what i have to do can someone help?
Write a class named Time that represents the time of day in hours and minutes. It should provide a constructor that can initialize the object to a specific time of day, and accessor methods getHour, getMinute as well as toString. Write four additional commands: addHour() and addMinute() to add a single hour and minute, respectively; and addHours(int n) and addMinutes(int n) to add the specified number of hours or minutes. Thoroughly test your class. Write the Time class assuming a 24-hour clock. toString should return strings such as "00:15" and "23:09".
Extra credit: Write the Time class assuming a 12-hour clock --that is, getHour will always return a number between 0 and 12. Add an additional accessor method, getPeriodDesignator. The last accessor method returns a value for "AM" if the time is between midnight and noon and "PM" if the time is between noon and 1 minute before midnight. Use an enumerated type. toString should return values such as "00:15AM," "12:00PM" (noon), and "11:59PM" (1 minute to midnight).
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.
If you are having trouble with your school work, check with your professor/teacher, or other classmates.
Don't get me wrong, we are here to help, but if you post your project & someone does it for you, then what will you learn? If you have a specific question, or are getting an error, please feel free to post it, & someone will do their best to help you.
public class Time { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59
// one formatting object to share in toString and toUniversalString private static DecimalFormat twoDigits = new DecimalFormat( "00" );
// Time constructor initializes each instance variable to zero; // ensures that Time object starts in a consistent state public Time() { this( 0, 0, 0 ); // invoke Time constructor with three arguments }
// Time constructor: hour supplied, minute and second defaulted to 0 public Time( int h ) { this( h, 0, 0 ); // invoke Time constructor with three arguments }
// Time constructor: hour and minute supplied, second defaulted to 0 public Time( int h, int m ) { this( h, m, 0 ); // invoke Time constructor with three arguments }
// Time constructor: hour, minute and second supplied public Time( int h, int m, int s ) { setTime( h, m, s ); }
// Time constructor: another Time3 object supplied public Time( Time time ) { // invoke Time constructor with three arguments this( time.getHour(), time.getMinute(), time.getSecond() ); }
// Set Methods // set a new time value using universal time; perform // validity checks on data; set invalid values to zero public void setTime( int h, int m, int s ) { setHour( h ); // set the hour setMinute( m ); // set the minute setSecond( s ); // set the second }
// validate and set hour public void setHour( int h ) { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); }
// validate and set minute public void setMinute( int m ) { minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); }
// validate and set second public void setSecond( int s ) { second = ( ( s >= 0 && s < 60 ) ? s : 0 ); }
// Get Methods // get hour value public int getHour() { return hour; }
// get minute value public int getMinute() { return minute; }
// get second value public int getSecond() { return second; }
// convert to String in universal-time format public String toUniversalString() { return twoDigits.format( getHour() ) + ":" + twoDigits.format( getMinute() ) + ":" + twoDigits.format( getSecond() ); }
i did what was stated but i am not sure if its done correct i want to know i the way i do the addHour() is the same as addMin() here is my update version of the program. also i want to kno how can i test my class ?
CODE
public class Time { private int hour; // 0 - 23 private int minute; // 0 - 59
// one formatting object to share in toString and toUniversalString private static DecimalFormat twoDigits = new DecimalFormat( "00" );
// Time constructor initializes each instance variable to zero; // ensures that Time object starts in a consistent state public Time() { this( 0, 0); // invoke Time constructor with two arguments }
// Time constructor: hour supplied, minute and second defaulted to 0 public Time( int h ) { this( h, 0); // invoke Time constructor with two arguments }
// Time constructor: hour and minute supplied public Time( int h, int m ) { this( h, m); // invoke Time constructor with two arguments }
// Time constructor: hour, minute and second supplied public Time( int h, int m) { setTime( h, m); }
// Time constructor: another Time3 object supplied public Time( Time time ) { // invoke Time constructor with two arguments this( time.getHour(), time.getMinute()); }
// Set Methods // set a new time value using universal time; perform // validity checks on data; set invalid values to zero public void setTime( int h, int m) { setHour( h ); // set the hour setMinute( m ); // set the minute }
// validate and set hour public void setHour( int h ) { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); }
// validate and set minute public void setMinute( int m ) { minute = (( m >= 0 && m < 60 ) ? m : 0 ); } public void addMin() { addMin(1); }
public void addMin(int n) { minute += n; // add minutes hour += (minute / 60); // if over 60 add to hours minute %= 60; // wrap at 60 hour %= 24; // wrap at 24 }
public void addHour(int n) { hour += n; // add hour minute += (hour / 60); // if over 60 add to hours minute %= 60; // wrap at 60 hour %= 24; // wrap at 24 }
// Get Methods // get hour value public int getHour() { return hour; }
// get minute value public int getMinute() { return minute; }
// convert to String in universal-time format public String toUniversalString() { return twoDigits.format( getHour() ) + ":" + twoDigits.format( getMinute() ) + ":" + }
Now to test your code you have to add the static void method main
java
static void main(String[] arg) { // create an object time Time time = new Time(); // display it System.out.println("Just created: " + time.toUnsiversalString() + " - " + time.toString()); // see what happend s if I hade 15 hours // that will test addHours and if my AM and PM works time.addHour(15); System.out.println("Time is now: " + time.toUnsiversalString() + " - " + time.toString()); // test setMinute time.setMinute(18); System.out.println("Time after setMinute: " + time.toUnsiversalString() + " - " + time.toString()); // test in a loop for the wrap of the minutes for(int i = 0; i < 5; i++) { time.addMinute(15); System.out.println("after addMinute(15): " + time.toUnsiversalString() + " - " + time.toString()); } // and so on........... // other test // other test }
I presume you compiled to class by doing at the prompt:
public class Time { private int hour; // 0 - 23 private int minute; // 0 - 59
// one formatting object to share in toString and toUniversalString private static DecimalFormat twoDigits = new DecimalFormat( "00" );
// Time constructor initializes each instance variable to zero; // ensures that Time object starts in a consistent state public Time() { this( 0, 0); // invoke Time constructor with two arguments }
// Time constructor: hour supplied, minute and second defaulted to 0 public Time( int h ) { this( h, 0); // invoke Time constructor with two arguments }
// Time constructor: hour and minute supplied public Time( int h, int m ) { this( h, m); // invoke Time constructor with two arguments }
// Time constructor: hour, minute and second supplied public Time( int h, int m) { setTime( h, m); }
// Time constructor: another Time3 object supplied public Time( Time time ) { // invoke Time constructor with two arguments this( time.getHour(), time.getMinute()); }
// Set Methods // set a new time value using universal time; perform // validity checks on data; set invalid values to zero public void setTime( int h, int m) { setHour( h ); // set the hour setMinute( m ); // set the minute }
// validate and set hour public void setHour( int h ) { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); }
// validate and set minute public void setMinute( int m ) { minute = (( m >= 0 && m < 60 ) ? m : 0 ); } public void addMin() { addMin(1); }
public void addMin(int n) { minute += n; // add minutes hour += (minute / 60); // if over 60 add to hours minute %= 60; // wrap at 60 hour %= 24; // wrap at 24 }
public static void main(String[] arg) { // create an object time Time time = new Time(); // display it System.out.println("Just created: " + time.toUnsiversalString() + " - " + time.toString()); // see what happends if I hade 15 hours // that will test addHours and if my AM and PM works time.addHour(15); System.out.println("Time is now: " + time.toUnsiversalString() + " - " + time.toString()); // test setMinute time.setMinute(18); System.out.println("Time after setMinute: " + time.toUnsiversalString() + " - " + time.toString()); // test in a loop for the wrap of the minutes for(int i = 0; i < 5; i++) { time.addMinute(15); System.out.println("after addMinute(15): " + time.toUnsiversalString() + " - " + time.toString()); } // and so on........... // other test // other test }