In: Computer Science
In C++, Implement the following class that represents a clock.
Clock
- hour: int
- minute: int
- meridiem: string
+ Clock()
+ Clock(hr: int, min: int, mer: string)
+ setTime(hr: int, min: int, mer: string): void
+ setHour(hr: int): void + setMinute(min: int): void
+ setMeridiem(mer: string): void
+ getHour(): int
+ getMinute(): int
+ getMeridiem(): string
+ void tick()
+ string asString()
+ string asStandard()
Implementation Details:
• The default constructor should set the clock to midnight (12:00 am)
• Hour must be in the range 1 to 12
• Minute must be in the range 0 to 59
• Meridiem must be the string “am” or the string “pm”
• The constructor that accepts a time as parameters and all of the setters (mutators) must perform error checking. If an error is detected, print an appropriate error message and stop the program. The exit() function can be used to stop the program.
• tick() increments the minute value and handles any rollover. For example, a clock currently set to 11:59 am would become 12:00 pm after executing tick() .
• asString() returns the current time in a format suitable for printing to the screen (i.e. 1:05 pm). Note the leading zero for values of minutes less than 10.
• asStandard() returns the current time in 24-hour clock format(i.e. 13:05). Both the hour and minute values should be 2 digit numbers (use a leading zero for values less than 10).
Here are the first few lines of code to get started:
#include <iostream>
#include <string>
#include <sstream>
class Clock {
};
Clock::Clock()
{
setTime(12, 0, "am");
}
And here is the main() that was given to test this:
int main() {
Clock c;
cout << "After default constructor: " << endl;
cout << c.asString() << endl;
cout << c.asStandard() << endl;
c.tick();
c.tick();
cout << "After 2 ticks: " << endl;
cout << c.asString() << endl;
cout << c.asStandard() << endl;
for (int i = 0; i < 185; i = i + 1)
c.tick();
cout << "After 185 more ticks: " << endl;
cout << c.asString() << endl;
cout << c.asStandard() << endl;
cout << endl << endl;
// Continue testing constructors and tick()
// Continue testing getters and setters....
return 0;
}
Example Execution
After default constructor:
12:00am 00:00
After 2 ticks:
12:02am
00:02
After 185 more ticks:
3:07am
03:07
After parameter constructor:
11:59am
11:59
After 2 ticks:
12:01pm
12:01
After 185 more ticks:
3:06pm
15:06
#include <iostream> #include <string> #include <sstream> #include <cstdlib> #include <iomanip> using namespace std; class Clock { private: int hour, minute; string meridiem; public: Clock(); Clock(int, int, string); void setTime(int h, int m, string mer); void setHour(int); void setMinute(int); void setMeridiem(string); int getHour(); int getMinute(); string getMeridiem(); void tick(); string asString(); string asStandard(); }; Clock::Clock() { setTime(12, 0, "am"); } Clock::Clock(int h, int m, string s) { setTime(h, m, s); } void Clock::setTime(int h, int m, string s) { if(h <= 0 || h > 12) { cout << "invalid Hours" << endl; exit(1); } if(m < 0 || m > 59) { cout << "invalid Minutes" << endl; exit(1); } if(s != "am" && s != "pm") { cout << "invalid meridiem" << endl; exit(1); } this->hour = h; this->minute = m; this->meridiem = s; } void Clock::setHour(int h) { setTime(h, minute, meridiem); } void Clock::setMinute(int x) { setTime(hour, x, meridiem); } void Clock::setMeridiem(string x) { setTime(hour, minute, x); } int Clock::getHour() { return hour; } int Clock::getMinute() { return minute; } string Clock::getMeridiem() { return meridiem; } string Clock::asString() { stringstream ss; ss << setfill('0') << setw(2) << hour << ":"; ss << setfill('0') << setw(2) << minute << meridiem; return ss.str(); } string Clock::asStandard() { int h = hour; if(h == 12) { h = 0; } if(meridiem == "pm") { h += 12; } stringstream ss; ss << setfill('0') << setw(2) << h << ":"; ss << setfill('0') << setw(2) << minute; return ss.str(); } void Clock::tick() { minute += 1; if(minute == 60) { minute = 0; // increment hour if(hour == 12) { hour = 1; } else { hour++; } if(hour == 12) { meridiem = (meridiem == "am" ? "pm" : "am"); } } } int main() { Clock c; cout << "After default constructor: " << endl; cout << c.asString() << endl; cout << c.asStandard() << endl; c.tick(); c.tick(); cout << "After 2 ticks: " << endl; cout << c.asString() << endl; cout << c.asStandard() << endl; for (int i = 0; i < 185; i = i + 1) c.tick(); cout << "After 185 more ticks: " << endl; cout << c.asString() << endl; cout << c.asStandard() << endl; cout << endl << endl; // Continue testing constructors and tick() Clock c1(11, 59, "am"); cout << "After parameter constructor: " << endl; cout << c1.asString() << endl; cout << c1.asStandard() << endl; c1.tick(); c1.tick(); cout << "After 2 ticks: " << endl; cout << c1.asString() << endl; cout << c1.asStandard() << endl; for (int i = 0; i < 185; i = i + 1) c1.tick(); cout << "After 185 more ticks: " << endl; cout << c1.asString() << endl; cout << c1.asStandard() << endl; cout << endl << endl; return 0; }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.