Question

Need help with this java code supposed to be a military time clock, but I need...

Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic.

  1. public class Clock
  2. {
  3. private int hr; //store hours
  4. private int min; //store minutes
  5. private int sec; //store seconds
  6. public Clock ()
  7. {
  8. setTime (0, 0, 0);
  9. }
  10. public Clock (int hours, intminutes, int seconds)
  11. {
  12. setTime (hours, minutes, seconds);
  13. }
  14. public void setTime (int hours,int minutes, int seconds)
  15. {
  16. if (0 <= hours && hours < 24)
  17. hr = hours;
  18. else
  19. hr = 0;
  20. if (0 <= minutes && minutes < 60)
  21. min = minutes;
  22. else
  23. min = 0;
  24. if (0 <= seconds && seconds < 60)
  25. sec = seconds;
  26. else
  27. sec = 0;
  28. }
  29. //Method to return the hours
  30. public int getHours ( )
  31. {
  32. return hr;
  33. }
  34. //Method to return the minutes
  35. public int getMinutes ( )
  36. {
  37. return min;
  38. }
  39. //Method to return the seconds
  40. public int getSeconds ( )
  41. {
  42. return sec;
  43. }
  44. public void printTime ( )
  45. {
  46. if (hr < 10)
  47. System.out.print ("0");
  48. System.out.print (hr + ":");
  49. if (min < 10)
  50. System.out.print ("0");
  51. System.out.print (min + ":");
  52. if (sec < 10)
  53. System.out.print ("0");
  54. System.out.print (sec);
  55. }
  56. //The time is incremented by one second
  57. //If the before-increment time is 23:59:59, the time
  58. //is reset to 00:00:00
  59. public void incrementSeconds ( )
  60. {
  61. sec++;
  62. if (sec > 59)
  63. {
  64. sec = 0;
  65. incrementMinutes ( ); //increment minutes
  66. }
  67. }
  68. ///The time is incremented by one minute
  69. //If the before-increment time is 23:59:53, the time
  70. //is reset to 00:00:53
  71. public void incrementMinutes ( )
  72. {
  73. min++;
  74. if (min > 59)
  75. {
  76. min = 0;
  77. incrementHours ( ); //increment hours
  78. }
  79. }
  80. public void incrementHours ( )
  81. {
  82. hr++;
  83. if (hr > 23)
  84. hr = 0;
  85. }
  86. public boolean equals (ClockotherClock)
  87. {
  88. return (hr == otherClock.hr
  89. && min == otherClock.min
  90. && sec == otherClock.sec);
  91. }
  92. }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

public class Clock {

    private int hr; // store hours

    private int min; // store minutes

    private int sec; // store seconds

    public Clock() {

        setTime(0, 0, 0);

    }

    public Clock(int hours, int minutes, int seconds) {

        setTime(hours, minutes, seconds);

    }

    public void setTime(int hours, int minutes, int seconds) {

        if (0 <= hours && hours < 24)

            hr = hours;

        else

            hr = 0;

        if (0 <= minutes && minutes < 60)

            min = minutes;

        else

            min = 0;

        if (0 <= seconds && seconds < 60)

            sec = seconds;

        else

            sec = 0;

    }

    // Method to return the hours

    public int getHours() {

        return hr;

    }

    // Method to return the minutes

    public int getMinutes() {

        return min;

    }

    // Method to return the seconds

    public int getSeconds() {

        return sec;

    }

    public void printTime() {

        if (hr < 10)

            System.out.print("0");

        System.out.print(hr + ":");

        if (min < 10)

            System.out.print("0");

        System.out.print(min + ":");

        if (sec < 10)

            System.out.print("0");

        System.out.print(sec);

    }

    // The time is incremented by one second

    // If the before-increment time is 23:59:59, the time

    // is reset to 00:00:00

    public void incrementSeconds() {

        sec++;

        if (sec > 59) {

            sec = 0;

            incrementMinutes(); // increment minutes

        }

    }

    /// The time is incremented by one minute

    // If the before-increment time is 23:59:53, the time

    // is reset to 00:00:53

    public void incrementMinutes() {

        min++;

        if (min > 59) {

            min = 0;

            incrementHours(); // increment hours

        }

    }

    public void incrementHours() {

        hr++;

        if (hr > 23)

            hr = 0;

    }

    public boolean equals(Clock otherClock) {

        return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec);

    }

}

public class ClockDriver{

    public static void main(String[] args) {

        Clock clock = new Clock();

        clock.setTime(3, 15, 20);

        clock.printTime();

        System.out.println();

        clock.incrementHours();

        clock.incrementSeconds();

        clock.printTime();

        System.out.println();

        clock.incrementMinutes();

        clock.printTime();

        System.out.println();

        Clock otherClock = new Clock(4, 16, 21);

        System.out.println(clock.equals(otherClock));

        otherClock.incrementMinutes();

        System.out.println(clock.equals(otherClock));

    }

}

nagarajuanagaraju-Vostro-3550:26092019$ javac ClockDriver.java nagaraju@nagaraju-Vostro-3550 : 26092019$ java ClockDriver 03:

Add a comment
Know the answer?
Add Answer to:
Need help with this java code supposed to be a military time clock, but I need...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [Java] We have learned the class Clock, which was designed to implement the time of day...

    [Java] We have learned the class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following: Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors. Write a test...

  • Write in C++ please In Chapter 10, the class clockType was designed to implement the time...

    Write in C++ please In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • This is a Java programming assignment and I want help with the Time class. See below...

    This is a Java programming assignment and I want help with the Time class. See below for assignment details: For this question you must write a java class called Time and a client class called TimeClient. The partial Time class is given below. (For this assignment, you will have to submit 2 .java files: one for the Time class and the other one for the TimeClient class and 2 .class files associated with these .java files. So in total you...

  • Please help with Java programming! This code is to implement a Clock class. Use my beginning...

    Please help with Java programming! This code is to implement a Clock class. Use my beginning code below to test your implementation. public class Clock { // Declare your fields here /** * The constructor builds a Clock object and sets time to 00:00 * and the alarm to 00:00, also. * */ public Clock() { setHr(0); setMin(0); setAlarmHr(0); setAlarmMin(0); } /** * setHr() will validate and set the value of the hr field * for the clock. * *...

  • Can someone please help me with the following java assignment. So in this assignment you will...

    Can someone please help me with the following java assignment. So in this assignment you will begin with the starterProject I provide and then add to it.  I am also asking that before you begin you really look at the starterProject because this is the object-oriented basics. The starterProject is the exact same as the ClassClockExample.zip in Module 5.  I’ve also done this using a clock because it is something that you already know how it works.  Remember that OOD and OOP is...

  • need help with this JAVA lab, the starting code for the lab is below. directions: The...

    need help with this JAVA lab, the starting code for the lab is below. directions: The Clock class has fields to store the hours, minutes and meridian (a.m. or p.m.) of the clock. This class also has a method that compares two Clock instances and returns the one that is set to earlier in the day. The blahblahblah class has a method to get the hours, minutes and meridian from the user. Then a main method in that class creates...

  • HI C PROGRAMMING COULD YOU,, Rewrite the program to have it display the time on the...

    HI C PROGRAMMING COULD YOU,, Rewrite the program to have it display the time on the second line of the display, using the HH:MM:SS format. Use the 24-hour format for the hours, in other words, have the time go from 00:00:00 to 23:59:59. #include<LiquidCrystal.h> LiquidCrystal LcdDriver(11, 9, 5, 6, 7, 8); int minutes = 27; //These global integers keep the value of the clock int sec = 10; int hr = 10; const long interval = 1000; //This interval is...

  • D Question 6 4 pts Figure 1: clock Type |-hr: int -min: int -sec; int setTime...

    D Question 6 4 pts Figure 1: clock Type |-hr: int -min: int -sec; int setTime (int, int, int): void get Time (inte, int&, int&) const: void printTime() const: void increment seconds(): int +incrementMinutes(): int +increment Hours(): int -equalTime (const clockType.) const: bool Consider the UML class diagram shown in the accompanying figure. According to the UML class diagram, how many private members are in the class? none zero two three D Question 4 4 pts Consider the following class...

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT