Question: Your program must include the following as comments at the beginning of the program: name assignment information program documentation In this assignment you will modify
Your program must include the following as comments at the beginning of the program:
name
assignment information
program documentation
In this assignment you will modify an existing class (the Time class from chapter 9 in the textbook). You will make changes to the class declaration (stored in a header file) and also the class implementation (stored in a cpp source file).
For a client of the class, the public interface will not change! A program using the Time class to create objects SHOULD work exactly the same as before the changes.
Here is a link to a ZIP folder with the existing specification and implementation of the Time class: Chapter_09_Time_class
Note: the header file and the cpp source file are from the 10th edition book code examples [ fig09_05_07 ]. I have provided a modified driver program.
Here are the changes you will need to make:
Delete the fields hour, minute, and second and replace them with a single field of type int named seconds. This field will be used to store the time internally as the number of seconds since midnight.
Example 1: If the user created a Time object with this statement: Time currentTime(6 , 25, 3); the value stored in the object's field (number of seconds since midnight) would be 23103 seconds.
Example 2: if Time object newTime was currently storing the value 55925 in it's seconds field and one of the print methods for the object was invoked, the time would be displayed as 15:32:05 or 3:32:05 PM
Your member functions will need to do calculations to convert from total seconds to hours, minutes, and seconds. Conversely from hours, minutes, and seconds to total seconds. Hint: use combinations of integer division and integer remainder (modulus) to convert hours, minutes, seconds to total seconds. Note further that an hour = 3600 seconds and a minute = 60 seconds.
When you have completed the assignment, create a ZIP file that includes the following:
Modified Time class declaration (Time.h)
Modified Time class implementation (Time.cpp)
The driver program Fig09_07 [you will not need to make any changes to the driver program]
****************************************************************************************************************************************
// Fig. 9.7: fig09_07.cpp // Constructor with default arguments. #include
// displays a Time in 24-hour and 12-hour formats void displayTime(const string& message, const Time& time) { cout << message << " Universal time: " << time.toUniversalString() << " Standard time: " << time.toStandardString() << " "; }
int main() { Time t1; // all arguments defaulted Time t2{2}; // hour specified; minute and second defaulted Time t3{21, 34}; // hour and minute specified; second defaulted Time t4{12, 25, 42}; // hour, minute and second specified
cout << "Constructed with: "; displayTime("t1: all arguments defaulted", t1); displayTime("t2: hours=2, minutes and seconds defaulted", t2); displayTime("t3: hours=21, minutes=34, seconds defaulted", t3); displayTime("t4: hours=12, minutes=25, seconds=42", t4); t1.setHour(13); t1.setMinute(25); t1.setSecond(47); displayTime("t1: after hours -> 13, minutes -> 25, seconds -> 47", t1); t2.setMinute(59); t2.setSecond(15); displayTime("t2: hours=2, after minutes -> 59, seconds -> 15", t2); t3.setSecond(56); displayTime("t3: hours=21, minutes=34, after seconds -> 56", t3);
// attempt to initialize t5 with invalid values try { Time t5{27, 74, 99}; // all bad values specified } catch (invalid_argument& e) { cerr << "Exception while initializing t5: " << e.what() << endl; } }
********************************************************************************************************************************************
// Fig. 9.6: Time.cpp // Member-function definitions for class Time. #include
// Time constructor initializes each data member Time::Time(int hour, int minute, int second) { setTime(hour, minute, second); // validate and set time }
// set new Time value using universal time void Time::setTime(int h, int m, int s) { setHour(h); // set private field hour setMinute(m); // set private field minute setSecond(s); // set private field second }
// set hour value void Time::setHour(int h) { if (h >= 0 && h < 24) { hour = h; } else { throw invalid_argument("hour must be 0-23"); } }
// set minute value void Time::setMinute(int m) { if (m >= 0 && m < 60) { minute = m; } else { throw invalid_argument("minute must be 0-59"); } }
// set second value void Time::setSecond(int s) { if (s >= 0 && s < 60) { second = s; } else { throw invalid_argument("second must be 0-59"); } }
// return hour value unsigned int Time::getHour() const { return hour; }
// return minute value unsigned Time::getMinute() const { return minute; }
// return second value unsigned Time::getSecond() const { return second; }
// return Time as a string in universal-time format (HH:MM:SS) string Time::toUniversalString() const { ostringstream output; output << setfill('0') << setw(2) << getHour() << ":" << setw(2) << getMinute() << ":" << setw(2) << getSecond(); return output.str(); }
// return Time as string in standard-time format (HH:MM:SS AM or PM) string Time::toStandardString() const { ostringstream output; output << ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12) << ":" << setfill('0') << setw(2) << getMinute() << ":" << setw(2) << getSecond() << (hour < 12 ? " AM" : " PM"); return output.str(); }
*****************************************************************************************************************************************
// Fig. 9.5: Time.h // Time class containing a constructor with default arguments. // Member functions defined in Time.cpp. #include
// prevent multiple inclusions of header #ifndef TIME_H #define TIME_H
// Time class definition class Time { public: explicit Time(int = 0, int = 0, int = 0); // default constructor
// set functions void setTime(int, int, int); // set hour, minute, second void setHour(int); // set hour (after validation) void setMinute(int); // set minute (after validation) void setSecond(int); // set second (after validation)
// get functions unsigned int getHour() const; // return hour unsigned int getMinute() const; // return minute unsigned int getSecond() const; // return second
std::string toUniversalString() const; // 24-hour time format string std::string toStandardString() const; // 12-hour time format string private: unsigned int hour{0}; // 0 - 23 (24-hour clock format) unsigned int minute{0}; // 0 - 59 unsigned int second{0}; // 0 - 59 };
#endif
********************************************************************************************************************************************************************
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
