Welcome to Dream.In.Code
Become a Java Expert!

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!




help?

2 Pages V  1 2 >  
Reply to this topicStart new topic

help?, dont know what to do?

webbywebb
1 Apr, 2008 - 07:24 PM
Post #1

D.I.C Head
**

Joined: 30 Jan, 2007
Posts: 73


My Contributions
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).
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Help?
1 Apr, 2008 - 07:30 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,166



Thanked: 78 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
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.

code.gif

If you are having trouble with your school work, check with your professor/teacher, or other classmates.
User is offlineProfile CardPM
+Quote Post

webbywebb
RE: Help?
1 Apr, 2008 - 07:34 PM
Post #3

D.I.C Head
**

Joined: 30 Jan, 2007
Posts: 73


My Contributions
ok thanx
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Help?
1 Apr, 2008 - 07:38 PM
Post #4

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,166



Thanked: 78 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

webbywebb
RE: Help?
1 Apr, 2008 - 07:38 PM
Post #5

D.I.C Head
**

Joined: 30 Jan, 2007
Posts: 73


My Contributions
this is what i have so far: i dont kno what to do

CODE
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() );
   }

   // convert to String in standard-time format
   public String toString()
   {
      return ( ( getHour() == 12 || getHour() == 0 ) ?
         12 : getHour() % 12 ) + ":" + twoDigits.format( getMinute() ) +
         ":" + twoDigits.format( getSecond() ) +
         ( getHour() < 12 ? " AM" : " PM" );
   }

} // end class Time


This post has been edited by webbywebb: 1 Apr, 2008 - 07:41 PM
User is offlineProfile CardPM
+Quote Post

webbywebb
RE: Help?
2 Apr, 2008 - 06:38 AM
Post #6

D.I.C Head
**

Joined: 30 Jan, 2007
Posts: 73


My Contributions
i am stuck :'(
User is offlineProfile CardPM
+Quote Post

pbl
RE: Help?
2 Apr, 2008 - 06:55 AM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(webbywebb @ 2 Apr, 2008 - 07:38 AM) *

i am stuck :'(


That's a very very good start.

The assignment says to return the time as HH:MM your returns HH:MM:SS but it is not a big deal.

the addMin() and addMin(int min) methods could look like this:

CODE

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
}


What are you stuck with ? The way to test it ?

User is online!Profile CardPM
+Quote Post

webbywebb
RE: Help?
2 Apr, 2008 - 07:54 AM
Post #8

D.I.C Head
**

Joined: 30 Jan, 2007
Posts: 73


My Contributions
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() ) + ":" +
    }

    // convert to String in standard-time format
    public String toString()
    {
    return ( ( getHour() == 12 || getHour() == 0 ) ?
    12 : getHour() % 12 ) + ":" + twoDigits.format( getMinute() ) +
    ":" +    ( getHour() < 12 ? " AM" : " PM" );
    }    

} // end class Time

User is offlineProfile CardPM
+Quote Post

pbl
RE: Help?
2 Apr, 2008 - 08:11 AM
Post #9

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
In addHour don't touch the minutes

CODE

public void addHour() {
   addHour(1);
}

public void addHour(int n) {
    hour += n; // add hour
    hour %= 24;
}


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:

javac Time.java

now to execute it do:

java Time

Have fun

I presume
User is online!Profile CardPM
+Quote Post

webbywebb
RE: Help?
2 Apr, 2008 - 08:43 AM
Post #10

D.I.C Head
**

Joined: 30 Jan, 2007
Posts: 73


My Contributions
do i add the static void method main in the class or do i save it as a Main.java cause i keep getting errors

CODE
import java.text.DecimalFormat;
import  java.awt.*;
import  javax.swing.*;

  
   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() {
    addHour(1);
    }

    public void addHour(int n) {
    hour += n; // add hour
    hour %= 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() ) + ":" +
    }

    // convert to String in standard-time format
    public String toString()
    {
    return ( ( getHour() == 12 || getHour() == 0 ) ?
    12 : getHour() % 12 ) + ":" + twoDigits.format( getMinute() ) +
    ":" +    ( getHour() < 12 ? " AM" : " PM" );
    }    

}// end class Time
    
      
      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
    }

User is offlineProfile CardPM
+Quote Post

pbl
RE: Help?
2 Apr, 2008 - 01:59 PM
Post #11

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(webbywebb @ 2 Apr, 2008 - 09:43 AM) *

do i add the static void method main in the class or do i save it as a Main.java cause i keep getting errors


in your class Time

what type of error do you have

User is online!Profile CardPM
+Quote Post

pbl
RE: Help?
2 Apr, 2008 - 03:34 PM
Post #12

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
The static class main goes before


} // end class
User is online!Profile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month