Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: help with clock class

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with clock class

    i missed lecture today and we were supposed to complete this class and objects. im not really sure how to do this and would appreciate any help you all can provide.
    class Clock
    {
    // Declare fields of the class
    // 0 <= hours < 24, 0 <= minutes < 60, 0 <= seconds < 60
    private int hours, minutes, seconds;
    Clock(int hh, int mm, int ss) //constructor
    {
    }
    //increase time by sec seconds
    public void incrementSeconds(int sec)
    {
    }
    //increase time by min minutes
    public void incrementMinutes(int min)
    {
    }
    //increase time by hh hours,
    //if hours reach 24, simply wrap around to 0.
    public void incrementHours(int hh)
    {
    }
    public void addTime(Clock C) //add C into the clock
    {
    }
    //print time in hours:minutes:seconds am(or pm) format
    public void printTime()
    {
    }
    }
    class ClockDemo
    {
    public static void main(String [] args)
    {
    //write each statement for each operation below
    (create Clock object C1 with h:m:s = 0:0:0)
    (create second Clock object C2 with h:m:s=12:35:59)
    (add C2 into C1)
    (print C1)
    (print C2)
    (increase clock C1 by 1 seconds)
    (print C1)
    (increase clock C1 by 100 minutes)
    (print C1)
    (increase clock C1 by 10 hours)
    (print C1)
    (print C2)
    }
    }
    Last edited by helloworld922; February 25th, 2010 at 07:58 PM.


  2. #2
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: help with clock class

    Well the incrementing shouldn't be too bad.

    If you incremented by n seconds, then as long as n >= 60, increment by minutes instead and subtract 60 from n. When 0 <= n < 60, add that to the current number of seconds. If the result is >= 60, subtract 60 and increment minutes again.

    Ditto for minutes & hours.

    For hours, just subtract 24 from n until 0 <= n < 24, and then add that to hours. If the result >= 24, subtract 24.

    There are more efficient ways to do this involving integer division and modulus operators, but loops are easy and none of this is going to be expensive.

    The stuff in main() you should really have no trouble with.
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

Similar Threads

  1. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 10:18 AM