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 17 of 17

Thread: [ Build An Alarm ]

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy [ Build An Alarm ]

    Implement a Clock class that has a tick method tick() and variables: hr, min, and sec. Build a test class that has:

    C1_int as an initial time (e.g., 10:30:00)  Clock c1_int = new Clock();
    C2_alarm as a target time. (e.g.,12:45:00)  Clock c2_alarm = new Clock();


    Write a code in the test class, in which you can set the target time, and let the program stop whenever the initial time equal the initial time.

    Hints:
    1. As in regular clock, the Tick method increases one second every time you call the method.
    2. The minutes increases 1 minute every 60 seconds.
    3. The hours increase 1 hour every 60 minutes.


    ---------------------------


    can anyone help me to write this program?
    it's an assignment due next week
    help a new member of the community for good first impression <3

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [ Build An Alarm ]

    yes, we can help answer any specific java programming questions you have.

    What do you have so far? Be sure to wrap all posted code in code tags.
    If there are any error messages, copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Rauda (September 13th, 2017)

  4. #3
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [ Build An Alarm ]

    Thank you for the reply
    I didn't start the program yet
    didn't know how to start

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [ Build An Alarm ]

    Look at the instructions for the program.
    What is the name of the class?
    Create a class with that name.

    What is the name of any methods?
    define the methods that are required.

    What does the instructions say about what a method should do?
    Think about what steps the code needs to take to do that.

    Write a few lines at a time, compile it and fix the errors before going on.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [ Build An Alarm ]

    ok so, that's what I've done so far..
    you got the idea right?


    public class Clock {
    private int hr;
    private int min;
    private int sec;
     
    public int getHour() { return hr;}
     
    public void setHour(int h){
    if(h>=0 && hr<=23) 
    hr=h;
    }
     
    public void getMin(){return min;}
     
    public void setMin(int m){
    if (m>=0 && min<=60)
    min=m;
    }
     
    private void tick(){
    sec++
    if(sec==60){
    sec=0;
    min++;
    if(min==60){
    min=0;
    hr++;
    if(hr==24)
    hr=0;
    }

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [ Build An Alarm ]

    Ok, that looks like a good start.
    Does it compile?

    The posted code has lost all its indentations. Logically nested statements should be indented to show the logic:
       if(a == b) {
           // do something
       }
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [ Build An Alarm ]

    i didn't understand
    do it for me

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [ Build An Alarm ]

    do it for me
    Your IDE or editor will do it. Check the IDE's documentation for code formatting.

    If no IDE, then use the editor by adding spaces before the nested lines.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [ Build An Alarm ]

     
     
    public class Clock {
     
        private int hr;
        private int min;
        private int sec;
     
        public int getHour() {
            return hr;
        }
     
        public void setHour(int h) {
            if (h >= 0 && hr <= 23) {
                hr = h;
            }
        }
     
        public int getMin() {
            return min;
        }
     
        public void setMin(int m) {
            if (m >= 0 && min <= 60) {
                min = m;
            }
        }
     
     
    private void tick() {
            sec++;
            if (sec == 60) {
                sec = 0;
                min++;
                if (min == 60) {
                    min = 0;
                    hr++;
                    if (hr == 24) {
                        hr = 0;
                    }
                }
            }
    }
    }

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [ Build An Alarm ]

    Much better.

    Does it compile without errors?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [ Build An Alarm ]

    yeah
    i need to do them in package I guess and class something like that
    a classTester for the main class clock
    the question explains it all

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [ Build An Alarm ]

    Ok. A package isn't necessary for the code to be compiled or tested. The code for testing the class can be in a main method in the Clock class.
    When the code works, changes can be made if your instructor requires them.

    Do you have any specific questions on the next part you are working on?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [ Build An Alarm ]

    yeah i want the whole code

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [ Build An Alarm ]

    If you have any specific questions about the code you are writing, please post them.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [ Build An Alarm ]

    Quote Originally Posted by Rauda View Post
     
     
    public class Clock {
     
        private int hr;
        private int min;
        private int sec;
     
        public int getHour() {
            return hr;
        }
     
        public void setHour(int h) {
            if (h >= 0 && hr <= 23) {
                hr = h;
            }
        }
     
        public int getMin() {
            return min;
        }
     
        public void setMin(int m) {
            if (m >= 0 && min <= 60) {
                min = m;
            }
        }
     
     
    private void tick() {
            sec++;
            if (sec == 60) {
                sec = 0;
                min++;
                if (min == 60) {
                    min = 0;
                    hr++;
                    if (hr == 24) {
                        hr = 0;
                    }
                }
            }
    }
    }


    HI WHY ITS BE WRONG WITH ME I DO THE SAME STEP

    HOW CAN I DECLARE IT TO THE MAIN METHOD

  17. #16
    Junior Member
    Join Date
    Sep 2017
    Posts
    10
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: [ Build An Alarm ]

    create a main class to test your clock

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: [ Build An Alarm ]

    HI WHY ITS BE WRONG WITH ME I DO THE SAME STEP
    @noura If you have a question, please start your own thread. Be sure to wrap any posted code with code tags.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. alarm clock gui
    By hhm136 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2014, 01:03 PM
  2. Difference between Maven Build and Eclipse Build
    By Beginner2java in forum Java IDEs
    Replies: 2
    Last Post: October 14th, 2013, 10:13 AM
  3. Difference between Maven Build and Eclipse Build
    By Beginner2java in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 14th, 2013, 10:13 AM
  4. Difference between Maven Build and Eclipse Build
    By Beginner2java in forum Java IDEs
    Replies: 1
    Last Post: October 14th, 2013, 10:09 AM
  5. Alarm Clock (How to figure out how much time left till alarm will sound)
    By MrMario in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2013, 12:58 AM