Question: We will be extending the Clock class from the lab in a number of ways. You should approach each of these extensions in a step

We will be extending the Clock class from the lab in a number of ways. You should approach each of these extensions in a step by step manner. Complete and test each extension before you move to the next one. Do not try to do them all at once. For each of these extensions, modify the Clock same clock class file but write a new TestClock program to test it (so the first extension should have a TestClock01 program with it, the second a TestClock02, etc.). You will be submitting all of your test programs as well as the final version of the Clock class.

  1. Fix the broken setTime method. As written, setTime allows a user to set nonsense times like -32:100:8888 AM. Fix it so that you may only set the time to a valid value for a time. Change setTime to return a boolean value, and return a false value if there is an attempt to set the time to an nonsensical value. Test this with TestClock01.
  2. Write a second constructor method for the Clock class. It should allow you to pass in the hour, minute, second and AM or PM value. Like setTime it should not let the user set a nonsensical time. HINT: You can call a classs methods from inside of its constructors! Test this with TestClock02.
  3. Write a method called militaryString() that operates like toString(), except that it returns the current time in military time format instead of in standard time format. So 3:30PM in military time would be 1530 and 3:30AM in military time would be 0330. Your String must include the leading 0 for morning times. Test this with TestClock03.
  4. Write a method called militaryInt() that returns the current time as an integer in military time format (obviously the leading 0 here will need to be dropped). Test this with TestClock04.
  5. Extend your Clock class to store the date. DO NOT change how tick() operates yet (see below). Instead, your code should be changed so that the day, month and year are stored internally. The Clocks constructors should set the date to January 1st, 2000 by default. You should provide a third constructor for the Clock that is like the one you wrote in step 1 above, except that it should allow you to pass in the day, month and year and set the time to 1:00AM by default. You should alter toString() so that it provides the date and time in YYYY-MM-DD HH:MM:SS AM format. You should provide mutator objects to set the month, the day and the year, but do not worry about setting them to valid years yet. Test this with TestClock05.
  6. Change tick() so that a tick can change the date. This is trickier than you might think. At midnight you need to increment the day, but if the day reaches the end of a month, you need to increment the month and set the day back to 1. And of course if the month is December you need to reset the month and increment the year. And different months have different number of days. Break this check out into a helper method that keeps track of how many days each month has, rather than putting this logic right in the tick() method. Test this with TestClock06. (For this assignment you are not required to worry about leap years, time zones, daylight savings time, or any of the other things that make these operations complicated - but keep in mind that the complexities of this are why in real-world code you should always used date libraries if possible rather than writing your own date logic!)

Clock.Java

/** * A simple class used to demonstrate the implemenation of Java classes. * * @author YOUR NAME HERE * @version DATE HERE */

public class Clock { private int hour; private int minute; private int second; private boolean am;

/** * Simple no-argument constructor */ public Clock() { this.hour = 1; this.minute = 0; this.second = 0; this.am = true; }

/** * Sets the hour, minute, second of the clock and whether it is morning or * evening * * @param hour * - set the hour to this value * @param minute * - set the minute to this value * @param second * - set the second to this value * @param am * - true when the time is a morning time, false otherwise */ public void setTime(int hour, int minute, int second, boolean am) { this.hour = hour; this.minute = minute; this.second = second; this.am = am; }

/** * * @return the current hour */ public int getHour() { return this.hour; }

/** * * @return the current minute value */ public int getMinute() { return this.minute; }

/** * * @return the current second value */ public int getSecond() { return this.second; }

/** * @return true if the clock is showing an AM time, false otherwise */ public boolean getAM() { return this.am; }

/** * Advances the time stored in the clock by 1 second */ public void tick() { // TODO Add code so that, when called, the clock is // incremented by 1 second. Remember to do the right // thing when seconds == 60, when minutes == 60, and when // hours == 12 }

/** * * @return the current time as a String in hh:mm:ssAM format */ @Override public String toString() { String result = ""; // TODO Add code so that, when called, this returns a // String formatted as time ("1:00:00AM" for example) result = "This method is not working"; return result; }

}

TestClock.java

/** * A simple program to test the clock object. Modify this to test your changes. * * @author YOUR NAME HERE * @version DATE HERE * */ public class TestClock {

public static void main(String[] args) { Clock myClock = new Clock();

int i = 0; while (i < 120) { System.out.println("The current time is: " + myClock.toString()); myClock.tick(); i = i + 1; } }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!