DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2004

    Question Add up time with a class


    I have to make a class to add up time.

    the methods and the variables i have to use are.

    Time class

    private int hour
    private int minute
    private int second

    the methods of this class are

    time(int h,int m, int s)
    void show()
    time addUp(time t)
    void expand(time t)
    void setHour(int h)

    This class has to be called on from TestTime class where you make two objects of the time type, add these up and show them on screen

    This is something i have to do for school but can't get out off


  2. #2
    Join Date
    Feb 2004
    You'll have to be more specific than that. What have you done so far, and which bits are you finding hard? No one will do it for you, but we will help you.

  3. #3
    Join Date
    Dec 2004
    class Time {

    private int hour;
    private int minute;
    private int second;

    Time(int u,int m,int s){
    hour = u;
    minute = m;
    second = s;

    public int addup(tijd t){

    return ??;
    public static void Show(){

    System.out.println("The result is " + ??????);


    In the time class i don't now how to add up time. and use setTime method

    code to add up time :

    result=((second+second)%60) +(((second+second)/60)+((hour+hour)%60))+

    meaning you add up the seconds and whats over 60 seconds (1 minute) is seconds , same for minutes. I know this isn't the right code but it's the idea of it that i know, but how do i put this in a method (addup).

    Class TestTimeƧ

    public static main(String []args){

    Time t1 = new Time(10.20,12);
    time t2 = new Time( 20.30,14);


    here i have to call on the methods in Time to add them up and show the result.

    can i call on the show method with Time.show()

  4. #4
    Join Date
    Feb 2004
    Well first up, your addUp method needs to return a Time object, not an int. You need to change its signature.

    For the method, declare three int variables and name them newHour, newMinute and newSecond. Then use the formula you have to calculate the new values for these. eg
     newSeconds = (this.seconds + t.getSeconds) % 60;
    You then need to use these variables to create a new Time object, and return it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.