I AM TRYING TO MODIFY MY PROGRAM BELOW TO IMPLEMENT THE TIME AS A NUMBER OF SECO
ID: 3776887 • Letter: I
Question
I AM TRYING TO MODIFY MY PROGRAM BELOW TO IMPLEMENT THE TIME AS A NUMBER OF SECONDS SINCE MIDNIGHT AND SHOW THAT THERE IS NO VISIBLE CHANGE IN FUNCTIONALITY TO THE CLIENTS OF THE CLASS RATHER THAN THE THREE INTEGER VALUES HOUR, MINUTE, AND SECOND WHILE CLIENTS USE THE SAME PUBLIC METHODS AND GET THE SAME RESULTS. (USING THE VIRTUES OF IMPLEMENTATION HIDING). #ifndef TIME_H #define TIME_H 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 validation0 void setSecond(int); //set the second (after validation) //get functions unsigned int getHour() const; //return hour unsigned int getMinute() const; //return minute unsigned int getSecond () const; //return second void printUniversal() const; //output time in universal-time format void printStandard() const; //output time in standard-time format private: unsigned int hour; //0-23 unsigned int minute; //0-59 unsigned int second; //0-59 };//End of class #endif // CLASS_TIME_WEO_H //Member-function definitions for class Time #include #include #include #include "time.h" using namespace std; //Time constructor initializes each data member Time::Time( int hour, int minute, int second) { setTime(hour, minute, second); //validate and set time }//End constructor //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 }//End of function set_class_Time_WEO //set hour value void Time::setHour(int h) { if(h >= 0 && h < 24) hour = h; else throw invalid_argument("hour must be 0-23"); }//End function setHour //set Minute value void Time::setMinute(int m) { if(m >= 0 && m < 60) minute = m; else throw invalid_argument("minute must be 0-59"); }//End function setMInute //set Second value void Time::setSecond(int s) { if(s >= 0 && s < 60 ) second = s; else throw invalid_argument("second must be 0-59"); }//End of Function //return hour value unsigned int Time::getHour()const { return hour; }//End of Function //Return minute value unsigned int Time::getMinute()const { return minute; }//End Function //Return Second value unsigned int Time::getSecond()const { return second; }//End Function //Print Time in universal-time format (HH:MM;SS) void Time::printUniversal()const { cout << setfill('0') << setw(2) << getHour() <<":"<< setw(2) << getMinute() << ":" << setw(2) << getSecond(); }//End Function //Print Time in standard-time format (HH:MM:SS AM or PM) void Time::printStandard()const { cout << ((getHour() == 0 || getHour() == 12) ?12 : getHour() %12) << ":" << setfill('0') << setw(2) << getMinute() << ":" << setw(2) << getSecond() << (hour < 12 ? "AM" : "PM"); }//End function
Explanation / Answer
Added methods to convert time to totally seconds.
#ifndef TIME_H
#define TIME_H
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 validation0
void setSecond(int); //set the second (after validation)
//get functions unsigned
int getHour() const; //return hour unsigned
int getMinute() const; //return minute
unsigned int getSecond () const; //return second
void printUniversal() const; //output time in universal-time format
void printStandard() const; //output time in standard-time format
int convertToSeconds() const; // converting to seconds
private:
unsigned int hour; //0-23 unsigned
int minute; //0-59 unsigned
int second; //0-59 };//End of class
#endif
// CLASS_TIME_WEO_H
//Member-function definitions for class Time
#include
#include
#include
#include "time.h"
using namespace std;
//Time constructor initializes each data member
Time::Time( int hour, int minute, int second) { setTime(hour, minute, second);
//validate and set time }//End constructor
//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 }
//End of function set_class_Time_WEO
//set hour value
void Time::setHour(int h) { if(h >= 0 && h < 24) hour = h; else throw invalid_argument("hour must be 0-23"); }//End function setHour
//set Minute value
void Time::setMinute(int m) { if(m >= 0 && m < 60) minute = m; else throw invalid_argument("minute must be 0-59"); }//End function setMInute
//set Second value
void Time::setSecond(int s) { if(s >= 0 && s < 60 ) second = s; else throw invalid_argument("second must be 0-59"); }//End of Function
//return hour value unsigned
int Time::getHour()const { return hour; }//End of Function
//Return minute value unsigned
int Time::getMinute()const { return minute; }//End Function
//Return Second value unsigned
int Time::getSecond()const { return second; }//End Function
//Print Time in universal-time format (HH:MM;SS)
void Time::printUniversal()const {
cout << setfill('0') << setw(2) << getHour() <<":"<< setw(2) << getMinute() << ":" << setw(2) << getSecond();
}//End Function
//Print Time in standard-time format (HH:MM:SS AM or PM)
void Time::printStandard()const {
cout << ((getHour() == 0 || getHour() == 12) ?12 : getHour() %12) << ":" << setfill('0') << setw(2) << getMinute() << ":" << setw(2) << getSecond() << (hour < 12 ? "AM" : "PM"); }//End function
}
int Time::convertToSeconds()const {
return (hours*60*60) + (minutes*60) + seconds;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.