Okay for class I have to write a program with the following instructions. I'm just going to copy and paste the instruction so I don't miss anything.
In the assignment you will define a class that represent a clock. Create a class named Clock that fulfills the following requirements:
• A class named SimpleClock
o A clock has three member variables
Hours: legal values are 0 to 23
Minutes: legal values are 0 to 59
Seconds: legal values are 0 to 59
o Define four constructors (see NOTE below)
One constructor with no arguments. Represents midnight, 00:00:00
One constructor with 1 argument for the hour.
One constructor with 2 arguments for the hour and minutes
One constructor with 3 arguments for the hour, minutes, and seconds
• Make sure all members are initialized in all constructors
o Define getters for all member variables
o Define setters for all member variables(see NOTE below)
o Define a method called isMorning()
Should return true if the time is less than 12:00:00 (noon),otherwise it should
return false
o Define a method called isAfternoon()
Should return true if the time between 12:00:00 and 17:59:59. Otherwise, it
should return false
o Define a method called isEvening()
Should return true if the time is 18:00:00 or later. Otherwise,it should return false
o Define a method named tick()
Returns nothing
Changes the time that the object represents by adding one second to the time.
Note the time may wrap. For example, if the current time is 13:59:58, calling tick() on that object would change the time to be 13:59:59.Calling tick() a second time would alter the minutes and the hour so that the final result would be 14:00:00
o Overload the << operator
Should output the time in a 12-hour format 00:00:00 AM/PM. For example, ten
minutes after midnight would be 12:10:00 AM
The AM and PM should be part of the string.
The printed hour should be in the range to 1 – 12.
For example,
• if the time is midnight, 00:00:00, you should output is as 12:00:00 AM.
• If the time is 05:30:12, you should output it as 5:30:12 AM
• If the time is 13:12:34, you should output it as 1:12:34 PM
Don’t change the member variables!Make local variables if you need them.
This is not a setter function.
NOTE: In both setters and constructors, the values passed in can be any integer,but you must keep the member variables in their valid ranges.
• If a negative value is passed in for a member variable, set that member variable variable to 0.For example,if a user of the class tried to set the time to -1:-99:55, the result would be 00:00:55
• If a value larger than the maximum legal value is passed in more a member variable,set that member variable to the largest legal value.
For example,if a user of the class tried to set the time to 3:123:132, the result would be 3:59:59
You should put the class prototype in Clock.h, and implement all the Clock member functions in Clock.cpp. In a third file, main.cpp. write the following code:
• A function named printTimeMessage
o Should accept one Clock object
o Should not return anything.
o Print the time with the message:The current time is. For example, if the clock represents the time 00:15:34, this calling the function would show
The current time is 12:15:34 AM
on the console.
• main.cpp should have your main() which does the following.
1. Prompt the user for hours, minutes, and seconds
2. Use that information to create a clock object.
3. Call the printTimeMessage with the object.
4. Add 2 hours, 30 minutes, and 15 seconds to the time and then print it again.
5. Call tick() 10 times. After each tick() call, print the new time.
6. Keep doing steps 1-5 until the user enters -99 for hours.
Sample:
Enter hours: 14
Enter minutes: 29
Enter seconds: 40
The current time is 2:29:40 PM
Back to the Future:
The current time is 4:59:55 PM
Time Flies:
The current time is 4:59:56 PM
The current time is 4:59:57 PM
The current time is 4:59:58 PM
The current time is 4:59:59 PM
The current time is 5:00:00 PM
The current time is 5:00:01 PM
The current time is 5:00:02 PM
The current time is 5:00:03 PM
The current time is 5:00:04 PM
The current time is 5:00:05 PM
Enter hours: -99
Later alligator!!
Things to do
• Name the program pass8 and remember the extension must be .cpp.
• Use the same instructions as in Assignment 1 to edit using gvim pass8.cpp
• Compile using g++ pass8.cpp Clock.cpp
------------------------------------------
I've tried on this and I have some code. I was told I could for simplifying purposes so I wrote it all in one file instead of doing it in the separate files. This is what I have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <iostream>
#include <string>
using namespace std;
class Clock {
private: int mHours;
int mMinutes;
int mSeconds;
public:
Clock();
Clock(int);
Clock(int, int);
Clock(int, int, int);
int getHours();
{return mHours;}
int getMinutes();
{return mMinutes;}
int getSeconds();
{return mSeconds;}
void setHours(int hourValue) {
if(hourValue > 0 && hourValue < 13) {
mHours = hourValue;
}else if(hourValue<0) {
mHours = 0;
}else if(hourValue>12) {
mHours = 12;
}
}
void setMinutes(int minuteValue) {
if(minuteValue > 0 && minuteValue < 60) {
mMinutes = minuteValue;
}else if(minuteValue < 0) {
mMinutes = 0;
}else if( minuteValue > 59) {
mMinutes = 59;
}
}
void setSecond(int secondValue) {
if (secondValue > 0 && secondValue < 60) {
mSeconds = secondValue;
}else if(secondValue < 0) {
mSeconds = 0;
}else if(secondValue > 59) {
mSeconds = 59;
}
}
bool isMorning();
bool isAfternoon();
bool isEvening();
void printTimeMessage(int, int, int);
void tick();
friend ostream &operator<<(ostream &, Clock &);
};
ostream &operator<<(ostream &out, Clock &time) {
if(time.mHours > 12)
time.mHours = time.mHours -12;
else
time.mHours =time.mHours;
out << time.mHours << ":" << time.mMinutes << ":"
<< time.mSeconds << endl;
return out;
}
Clock::Clock() {
setHours(12);
setMinutes(0);
setSeconds(0);
}
Clock::Clock(int hourValue)
{setHours(hourValue);}
Clock::Clock(int hourValue, int minuteValue)
{setHours(hourValue);
setMinutes(minuteValue);}
Clock::Clock(int hourValue, int minuteValue, int secondValue)
{setHours(hourValue);
setMinutes(minuteValue);
setSeconds(secondValue);}
bool Clock::isMorning() {
if (mHours < 12) {
return true;
}else{
return false;
}
}
bool Clock::isAfternoon() {
if(mHours>=12 && mHours < 18) {
return true;
}else{
return false;
}}
bool Clock::isEvening() {
if (mHours < 18) {
return false;
}else{
return true;
}}
void Clock::tick() {
setSeconds((getSeconds() +1) % 60);
if (getSeconds() ==0)
{
setMinutes((getMinutes() +1) %60);
if(getMinutes()==0)
{
setHours((getHours()%12) +1);
}}}
void Clock::printTimeMessage(int, int, int) {
Clock a(int);
cout<<"The current time is"<<a.getHours<<endl;
}
int main() {
int hour, minute, second;
cout<< "What is the hour?" << endl;
cin>>hour;
cout << "minute?"<< endl;
cin>>minute;
cout<<"second?"<<endl;
cin>>second;
printTimeMessage(hour, minute,second);
return 0;
}
|