Create a New Java Project called YourLastNameClock . Write a Clock class to repr
ID: 3890629 • Letter: C
Question
Create a New Java Project called YourLastNameClock.
Write a Clock class to represent the time of day in a program. This class should be defined as a separate file within your Java Project. Points will be deducted if the class is embedded as an inner class. This does not affect the name of your Java Project above.
Suppose that the time is represented as a set of three integers: one to represent the hours, one to represent the minutes, and another to represent the seconds.
hr: an integer to store the hours
min: an integer to store the minutes
sec: an integer to store the seconds
The class should have the following methods:
Constructor: default constructor (i.e. no-arg constructor) that initializes all fields to 0.
Consructor: accepts values specified by the user for each field as an argument.
setTime: sets the time to the time specified by the user. The method header is: public void setTime(int hours, int minutes, int seconds)
getHours: returns the hours.
getMinutes: returns the minutes.
getSeconds: returns the seconds.
printTime: prints the time in the form hh:mm:ss. The method header is: public void printTime()
incrementHours: increments the time by one hour.
incrementMinutes: increments the time by one minute.
incrementSeconds: increments the time by one second.
equals: compares two times to determine whether they are equal. The method header is: public boolean equals(Clock otherClock)
makeCopy: copies the time of one Clock object into another Clock object. See Chapter 8 content on deepcopy to assist with developing this method. The method header is: public void makeCopy(Clock otherClock)
getCopy: returns a copy of the time. A copy of the object's time is created and a reference to the copy is returned. The method header is: public Clock getCopy()
digitalClockGame: plays the Amazing Digital Clock Game. There is a belief that if at any time a person takes a look at a digital clock, with standard time, and if the digits of the hours, minutes, and seconds add up equally for the three parts, it is a good time, or actually a lucky time that will predict or is a forecast that whatever you were thinking/wishing at that instant, might come true.
For example, 11:02:20, 5:14:23, or 12:03:30 are considered as lucky times.
Besides that, there are three other special times that also carry specific meanings.
If all digits are the same, like 4:44:44 or 5:55:55, it predicts you might win a lottery.
If all digits are in ascending order, like 2:34:56 or 1:23:45, it predicts all dreams will come true.
If all digits are in descending order, like 6:54:32 or 5:43:21, it predicts a challenging road is ahead.
When this method is executed, it should prompt the user to enter a time in the format hh:mm:ss. Then the method will tell if the time is a lucky time or just a normal time. And then, if the time carries a specific meaning, as described above, the corresponding meaning should be displayed also.
-------
Write a menu-driven program that demonstrates the Clock class. Use the menu below.
Welcome to the Digital Clock Program! Please choose an option below.
1. Set the time.
2. Return the hours.
3. Return the minutes.
4. Return the seconds.
5. Print the time.
6. Increment the time by one hour.
7. Increment the time by one minute.
8. Increment the time by one second.
9. Compare two times for equality.
10. Play Amazing Digital Clock.
11. Quit.
Your program should prompt continuously after a selection is made. Do not end the program until Quit is selected.
Input Validation: Do not accept unreasonable time, negative values, or junk values, which must be rejected and asked for another one.
Be sure that clear instructions are given to the user.
Explanation / Answer
Here is the code for Clock.java:
import java.util.*;
class Clock
{
int hr; //hr: an integer to store the hours
int min; //min: an integer to store the minutes
int sec; //sec: an integer to store the seconds
//The class should have the following methods:
//Constructor: default constructor (i.e. no-arg constructor) that initializes all fields to 0.
public Clock()
{
hr = min = sec = 0;
}
//Constructor: accepts values specified by the user for each field as an argument.
public Clock(int h, int m, int s)
{
hr = h;
min = m;
sec = s;
}
//setTime: sets the time to the time specified by the user.
public void setTime(int hours, int minutes, int seconds)
{
hr = hours;
min = minutes;
sec = seconds;
}
//getHours: returns the hours.
public int getHours() { return hr; }
//getMinutes: returns the minutes.
public int getMinutes() { return min; }
//getSeconds: returns the seconds.
public int getSeconds() { return sec; }
//printTime: prints the time in the form hh:mm:ss.
public void printTime()
{
System.out.printf("%02d:%02d%02d ", hr, min, sec);
}
//incrementHours: increments the time by one hour.
public void incrementHours()
{
hr++;
if(hr == 24)
hr = 0;
}
//incrementMinutes: increments the time by one minute.
public void incrementMinutes()
{
min++;
if(min == 60)
{
min = 0;
incrementHours();
}
}
//incrementSeconds: increments the time by one second.
public void incrementSeconds()
{
sec++;
if(sec == 60)
{
sec = 0;
incrementMinutes();
}
}
//equals: compares two times to determine whether they are equal.
public boolean equals(Clock otherClock)
{
return hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec;
}
//makeCopy: copies the time of one Clock object into another Clock object.
//See Chapter 8 content on deepcopy to assist with developing this method.
public void makeCopy(Clock otherClock)
{
hr = otherClock.hr;
min = otherClock.min;
sec = otherClock.sec;
}
//getCopy: returns a copy of the time. A copy of the object's time is created and a
//reference to the copy is returned.
public Clock getCopy()
{
Clock copy = this;
return copy;
}
//digitalClockGame: plays the Amazing Digital Clock Game.
//There is a belief that if at any time a person takes a look at a digital clock,
//with standard time, and if the digits of the hours, minutes, and seconds add up equally
//for the three parts, it is a good time, or actually a lucky time that will predict or
//is a forecast that whatever you were thinking/wishing at that instant, might come true.
//For example, 11:02:20, 5:14:23, or 12:03:30 are considered as lucky times.
//Besides that, there are three other special times that also carry specific meanings.
//If all digits are the same, like 4:44:44 or 5:55:55, it predicts you might win a lottery.
//If all digits are in ascending order, like 2:34:56 or 1:23:45, it predicts all dreams will come true.
//If all digits are in descending order, like 6:54:32 or 5:43:21, it predicts a challenging road is ahead.
//When this method is executed, it should prompt the user to enter a time in the format hh:mm:ss.
//Then the method will tell if the time is a lucky time or just a normal time.
//And then, if the time carries a specific meaning, as described above, the corresponding meaning should be displayed also.
public void digitalClockGame()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the time in format (hh:mm:ss): ");
sc.useDelimiter(":");
int h = sc.nextInt();
int m = sc.nextInt();
int s = sc.nextInt();
int hSum = h/10 + h%10;
int mSum = m/10 + m%10;
int sSum = s/10 + s%10;
if(hSum == mSum && mSum == sSum)
System.out.println("It's a lucky time.");
else
System.out.println("It's a normal time.");
}
}
And the code for ClockDemo.java is:
import java.util.*;
class ClockDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int choice, h, m, s;
Clock myClock = new Clock();
while(true)
{
System.out.println("Welcome to the Digital Clock Program! Please choose an option below.");
System.out.println("1. Set the time.");
System.out.println("2. Return the hours.");
System.out.println("3. Return the minutes.");
System.out.println("4. Return the seconds.");
System.out.println("5. Print the time.");
System.out.println("6. Increment the time by one hour.");
System.out.println("7. Increment the time by one minute.");
System.out.println("8. Increment the time by one second.");
System.out.println("9. Compare two times for equality.");
System.out.println("10. Play Amazing Digital Clock.");
System.out.println("11. Quit.");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch(choice)
{
case 1: System.out.print("Enter the time in format (hh:mm:ss): ");
sc.useDelimiter(":");
h = sc.nextInt();
m = sc.nextInt();
s = sc.nextInt();
myClock.setTime(h, m, s);
break;
case 2: System.out.println("Hours: " + myClock.getHours()); break;
case 3: System.out.println("Minutes: " + myClock.getMinutes()); break;
case 4: System.out.println("Seconds: " + myClock.getSeconds()); break;
case 5: System.out.print("The time is: ");
myClock.printTime(); break;
case 6: myClock.incrementHours(); break;
case 7: myClock.incrementMinutes(); break;
case 8: myClock.incrementSeconds(); break;
case 9: System.out.print("Enter the time in format (hh:mm:ss): ");
sc.useDelimiter(":");
h = sc.nextInt();
m = sc.nextInt();
s = sc.nextInt();
if(myClock.equals(new Clock(h, m, s)))
System.out.println("The clocks tick the same.");
else
System.out.println("The clocks tick different.");
break;
case 10: myClock.digitalClockGame(); break;
case 11: return;
default: System.out.println("Invalid choice. Try again.");
}
}
}
}