Question

For this project you will be writing up a simple Clock program to keep track of...

For this project you will be writing up a simple Clock program to keep track of time.

SimpleClock.java - contains your implementation of the SimpleClock class. You will need to provide the code for a constructor as well as the mutator methods set and tick and the accessor method toString. Look at the comments for each method to see how they should be implemented - the trickiest method is probably tick, which requires that you deal with the changing of minutes, hours and AM/PM in order to get it to work correctly.

/**
* SimpleClock.java
*
* A class that implements a simple clock.
*
* @author ENTER YOUR NAME HERE
* @version ENTER THE DATE HERE
*
*/
public class SimpleClock {

/* -------- Private member variables --------------------- */
private int hours;
private int minutes;
private int seconds;
private boolean morning;

/* -------- Constructor --------------------------------- */
/**
* The constructor should set the intial value of the clock to 12:00:00AM.
*/
public SimpleClock() {
// TODO - complete this constructor

}

/* --------- Instance methods ------------------------- */

/**
* Sets the time showing on the clock.
*
* @param hh
* - the hours to display
* @param mm
* - the minutes to display
* @param ss
* - the seconds to display
* @param morning
* - true for AM, false for PM
*/
public void set(int hh, int mm, int ss, boolean morning) {
// TODO - complete this procedure

}

/**
* Advances the clock by 1 second. Make sure when implementing this method
* that the seconds "roll over" correctly - 11:59:59AM should become
* 12:00:00PM for example.
*/
public void tick() {
// TODO - complete this procedure

}

/**
* Returns a string containing the current time formatted as a digital clock
* format. For example, midnight should return the string "12:00:00AM" while
* one in the morning would return the string "1:00:00AM" and one in the
* afternoon the string "1:00:00PM".
*
* @return - the current time formatted in AM/PM format
*/
@Override
public String toString() {
// TODO - complete this function

// TODO - the following line is only here to allow this program to
// compile. Replace it and remove this comment when you complete
// this method.
return "NOT IMPLEMENTED";
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Also included a Test class for testing. Ignore if you don’t need it.

// SimpleClock.java

public class SimpleClock {

      /* -------- Private member variables --------------------- */

      private int hours;

      private int minutes;

      private int seconds;

      private boolean morning;

      /* -------- Constructor --------------------------------- */

      /**

      * The constructor should set the intial value of the clock to 12:00:00AM.

      */

      public SimpleClock() {

            // using default values

            hours = 12;

            minutes = 0;

            seconds = 0;

            morning = true;

      }

      /* --------- Instance methods ------------------------- */

      /**

      * Sets the time showing on the clock.

      *

      * @param hh

      *            - the hours to display

      * @param mm

      *            - the minutes to display

      * @param ss

      *            - the seconds to display

      * @param morning

      *            - true for AM, false for PM

      */

      public void set(int hh, int mm, int ss, boolean morning) {

            // validating all fields before assigning

            if (hh < 1 || hh > 12) {

                  System.out.println("Invalid hour!");

            } else if (mm < 0 || mm > 59) {

                  System.out.println("Invalid minutes");

            } else if (ss < 0 || ss > 59) {

                  System.out.println("Invalid seconds");

            } else {

                  // all values are valid

                  hours = hh;

                  minutes = mm;

                  seconds = ss;

                  this.morning = morning;

            }

      }

      /**

      * Advances the clock by 1 second. Make sure when implementing this method

      * that the seconds "roll over" correctly - 11:59:59AM should become

      * 12:00:00PM for example.

      */

      public void tick() {

            // incrementing seconds

            seconds++;

            // checking if seconds go out of range

            if (seconds > 59) {

                  // resetting seconds, increasing minutes

                  seconds = 0;

                  minutes++;

            }

            // checking if minutes go out of range

            if (minutes > 59) {

                  // resetting minutes, incrementing hours

                  minutes = 0;

                  hours++;

                  // if hours is 12, switching the value of morning..ie if it is true,

                  // change to false, and vice versa

                  if (hours == 12) {

                        morning = !morning;

                  }

            }

            // if hours go out of range, resetting to 1

            if (hours > 12) {

                  hours = 1;

            }

      }

      /**

      * Returns a string containing the current time formatted as a digital clock

      * format. For example, midnight should return the string "12:00:00AM" while

      * one in the morning would return the string "1:00:00AM" and one in the

      * afternoon the string "1:00:00PM".

      *

      * @return - the current time formatted in AM/PM format

      */

      @Override

      public String toString() {

            // %02d specifies an integer value with max width of 2 spaces and padded

            // by 0. %2s specifies a string value of width 2

            // (morning ? "AM" : "PM") returns "AM" if morning is true, else "PM"

            return String.format("%02d:%02d:%02d%2s", hours, minutes, seconds,

                        (morning ? "AM" : "PM"));

      }

}

// Test.java

public class Test {

      public static void main(String[] args) {

            // creating a clock, testing set, toString and tick methods

            SimpleClock clock = new SimpleClock();

            System.out.println(clock);

            clock.set(11, 59, 59, true);

            System.out.println(clock);

            clock.tick();

            System.out.println(clock);

      }

}

/*OUTPUT*/

12:00:00AM

11:59:59AM

12:00:00PM

Add a comment
Know the answer?
Add Answer to:
For this project you will be writing up a simple Clock program to keep track of...
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
  • 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. * *...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • 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. 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, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • Task 3: Main Program Create a main program class that: Creates three or more Nurse instances...

    Task 3: Main Program Create a main program class that: Creates three or more Nurse instances assigned to different shifts. Creates three or more Doctor instances. Creates three or more Patient instances with pre-determined names and manually assigned physicians chosen from the pool of Doctor instances previously created. Generates another 20 Patient instances using randomly generated names and randomly assigns them physicians chosen from the pool of Doctor instances previously created. Prints the toString() values for all employees. Prints the...

  • As you can see this was a solution to a problem if someone could go through...

    As you can see this was a solution to a problem if someone could go through line by line and explain to me especially the timeof day file that would help me alot.I am having trouble understanding the logic used with the first class of code time of day. A mutable encapsulation of the time during a day. public class TimeofDay private static final int SECONDS PER MINUTE = 60 private static final int MINUTES PER HOUR = 60 private...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at...

    Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() or, if you are not using Java 8, new java.util.Date().toString() and extract the time from that string.) Also provide a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutesmethods. (2) Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show...

  • Writing 3 Java Classes for Student registration /** * A class which maintains basic information about...

    Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course {    /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Define a toString method in Post and override it in EventPost. EventPost Class: /** * This...

    Define a toString method in Post and override it in EventPost. EventPost Class: /** * This class stores information about a post in a social network news feed. * The main part of the post consists of events. * Other data, such as author and type of event, are also stored. * * @author Matthieu Bourbeau * @version (1.0) March 12, 2020 */ public class EventPost extends Post { // instance variables - replace the example below with your own...

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