Question
Description In this assignment, you'll write a Java class that contains variables to store time in hours, minutes, and seconds. Your class will provide an
Description
In this assignment, you'll write a Java class that contains variables to store time in hours, minutes, and seconds. Your class will provide an equals method to allow comparison of two objects to see if they contain identical values and a toString method that will print out a formatted time value with the AM or PM suffix depending on whether the time is before or after midday.
Write a Java class called SimpleTimeFormatter that represents a 24 hour clock (i.e. ranges from 00:00:00 to 23:59:59). It should have private integer variables for hour, minutes, and seconds, and should have the following public methods:
public void setTime(int hour, int minute, int second) - this method will set the object variables to the specified 24 hour time.. It must first test the values for a valid time being supplied by using the following rules:,
Hour must be in the range of 0 to 23.
Minutes and seconds must be in the range of 0 to 59.
If any of the parameters are outside these limits, it must set all three instance variables to zero (representing midnight). If the parameters represent a valid time, then it sets the instance variables to that time.
public int getHour() - this method returns the object's hour value. public int getMinutes() - this method returns the object's minutes value. public int getSeconds() - this method returns the object's seconds value.
public boolean equals(SimpleTimeFormatter otherTime) - this method compares the time represented by two SimpleTimeFormatter objects. It compares the time represented by this SimpleTimeFormatter object with the time
represented by the passed in object, otherTime, returning true if they represent the same time. To check for equality, this method must compare the hour, minutes, and seconds variables of this SimpleTimeFormatter object with hour, minutes, and seconds variables of the other SimpleTimeFormatter object, returning true if all three match, and false otherwise.
public String toString() - this method returns the object's time value formatted as a String of the form hh:mm:ss AM or PM. Hours, minutes and seconds must be two digits (ie. if less than 10, add a leading zero). Call the twoDigits method to convert the hours, minutes and seconds values to strings. The displayed hour is in the range 01 to 12.
For example:
Midnight is returned as 12:00:00 AM, noon as 12:00:00 PM, 9 AM as 09:00:00 AM, 3 PM as 03:00:00 PM.
private String twoDigits(int val) - this private method returns it's int parameter value formatted as a two digit String
For example: The value 5 returns the string "05 The value 20 returns the string "20".
It can use either the String.valueOf(int) method or Integer.toString(int) method to do the conversion. Call twoDigits() from the toString method in order to format the individual time fields as two digits where required. This method is made private so it cannot be called by methods in other classes.
Private int convertHour(int value) - this method returns an hour value based on a 12 hour clock. You will need to convert any values between 13 and 23, to a value between 1 and 12.
Note: the main() method for testing this class is supplied for you in the SimpleTimeFormatterDemo.java file attached to this assignment. Do NOT write your own main() method for this homework.
Sample output
Enter hour, minute, and second (-1 to exit) 10 20 30
Values read were: 10 20 30
10:20:30 AM
Enter hour, minute, and second (-1 to exit) 13 5 5
Values read were: 13 5 5 01:05:05 PM
Enter hour, minute, and second (-1 to exit) 25 10 20
Values read were: 25 10 20 12:00:00 AM
Enter hour, minute, and second (-1 to exit) -1
Test Data Use all the following test data, followed by some of your own examples.
10 20 30 111 13 1 1 000 23 59 59 12 0 0
6 30 0 24 30 15 12 60 15 12 30 60
This homework has two .java files you'll need to download.
Use the attached SimpleTimeFormatter.java source file as a starting template for writing your SimpleTimeFormatter class. It contains an outline of the class and methods needed, and you'll add the implementation code for each of the methods.
Use the attached SimpleTimeFormatterDemo.java source file to test your completed SimpleTimeFormatter.java class. It contains just the supplied main() method test code for testing your SimpleTimeFormatter class
Getting Started
The setTime() method first checks its parameters to see if they represent a valid time. If not, it sets all three of its instance variables to zero and returns. Otherwise, it sets the three instance variables to the time specified by the three parameter variables.
The toString() method must format the object's 24 hour time value into AM/PM time string. Once again, outline the logic needed to determine the hour and AM/PM value to be returned before you start writing Java code, something like:
if the hour value is 0 or 12, the formatted hour is 12 (either midnight or noon).
if the hour value is less than 12 the time is AM
else the time is PM
If the hour value is greater than 12, convert to a 12 hour clock value. For example, an hour value of 13 is 1:00 PM, 14 is 2:00 PM, etc. There are many ways to implement the logic for this function. You can use a switch statement or several if statements or even modulus arithmetic to test the value.
If the parameter value is less than 10 return "0" concatenated with the parameter value
else return the parameter value converted to string
using Integer.toString(val)
---------------------------------------------------------------------------------
/** * SimpleTimeFormatterDemo - tests the SimpleTimeFormatter class * * The SimpleTimeFormatter class must be complete before this class can be used. */ import java.util.Scanner; public class SimpleTimeFormatterDemo { public static void main(String[] args) { // Create a SimpleTimeFormatter object and set to sleep time. SimpleTimeFormatter sleep = new SimpleTimeFormatter(); sleep.setTime(23, 0, 0); System.out.println("Sleep at: " + sleep.toString()); // Create a SimpleTimeFormatter object and set to wakeup time. SimpleTimeFormatter wakeup = new SimpleTimeFormatter(); wakeup.setTime(6, 30, 0); System.out.println("Wakup at: " + wakeup.toString()); // Create a SimpleTimeFormatter object based on user inputs. int hour, minutes, seconds; do { Scanner scannerObject = new Scanner(System.in); System.out.println(); System.out.println("Enter hour minute and second (-1 to exit)"); hour = scannerObject.nextInt(); if (-1 == hour) { break; } minutes = scannerObject.nextInt(); seconds = scannerObject.nextInt(); System.out.println(); System.out.println("Values read were: " + hour + " " + minutes + " " + seconds); // Create a new SimpleTimeFormatter object SimpleTimeFormatter usertime = new SimpleTimeFormatter(); // Set the time to the time read from the keyboard usertime.setTime(hour, minutes, seconds); System.out.println(usertime.toString()); // Test if the time read matches the 'noon' SimpleTimeFormatter object's value. if (usertime.equals(sleep)) { System.out.println("Matches sleep time"); } // Test if the time read matches the 'wakeup' SimpleTimeFormatter objet's value if (usertime.equals(wakeup)) { System.out.println("It's time to wakeup!"); } } while (hour != -1); } }
-------------------------------------------------------------------------------------------
/** * SimpleTimeFormatter - a class which will format 3 input values * (hour, minutes, seconds) into a formatted time object of the form: * * HH:MM:SS AM * */ public class SimpleTimeFormatter { // Member variables holding the time (in 24 hour universal form). private int hour; // 0 - 23 private int minutes; // 0 - 59 private int seconds; // 0 - 59 /** * Sets the time values for this SimpleTimeFormatter object. If any of the * input parameter values are out of range, all of the variables for this * instance are set to zero. * * @param hour the hour (0 to 23). * @param minutes the minute (0 to 59). * @param seconds the second (0 to 59). */ public void setTime(int hour, int minutes, int seconds) { // TODO - add parameter checking code, and set the instance variables. // If any of the parameters are out of range, set all the instance variables to zero. } /** * Returns the hour value as an int. */ public int getHour() { return 0; // TODO - replace } /** * Returns the minute value as an int. */ public int getMinute() { return 0; // TODO - replace } /** * Returns the second value as an int. */ public int getSecond() { return 0; // TODO - replace } /** * Compares this SimpleTimeFormatter object against another SimpleTimeFormatter object. * The result is true if and only if the input argument is represents a * time that is the same time as represented by this object * * @param otherTime the object to be compared. * @returns true if both objects represent the same time value, false * otherwise. */ public boolean equals(SimpleTimeFormatter otherTime) { // TODO - add code to return 'true' if 'this' SimpleTimeFormatter matches // the time values in the 'otherTime' SimpleTimeFormatter object, else return false. return false; } /** * Returns the time of this SimpleTimeFormatter object in HH:MM:SS AM/PM form. * * Sample input: 9,15,0 * Sample output: 9:15:00 AM * * Sample input: 23,30,0 * Sample output: 11:30:00 PM * */ public String toString() { String result = ""; // TODO - add code to format the time in the instance variables // into HH:MM:SS AM/PM time (hour ranges from 1 to 12, AM or PM) // as a string and return it. Call the twoDigits() method to // convert each int value to String. return result; } /** * Returns the parameter value as a two digit String (with leading 0 if * needed) * * @return the parameter 'value' formatted as a two digit String (with * leading zero if needed). */ private String twoDigits(int value) { String result = ""; // TODO - Use the Integer.toString(int) method to convert // 'value' to String format, and add a leading "0" if // value is less than 10 in order to return a string with // two digits. return result; } /** * Returns a value for the hour value that is between 0 and 12. * Replace the "return 0" statement with an actual return value. */ private int convertHour(int value) { // TODO - implement logic to convert hour values between 13 - 23 // to 1 - 12. return 0; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started