Instructions Provided on canvas for you is a file called "Time.h", which contains the Time class. This class allows the

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Instructions Provided on canvas for you is a file called "Time.h", which contains the Time class. This class allows the

Post by answerhappygod »

Instructions Provided On Canvas For You Is A File Called Time H Which Contains The Time Class This Class Allows The 1
Instructions Provided On Canvas For You Is A File Called Time H Which Contains The Time Class This Class Allows The 1 (32.74 KiB) Viewed 34 times
Instructions Provided On Canvas For You Is A File Called Time H Which Contains The Time Class This Class Allows The 2
Instructions Provided On Canvas For You Is A File Called Time H Which Contains The Time Class This Class Allows The 2 (44.37 KiB) Viewed 34 times
Starter File: Time.h
#include <string>
#include <sstream>
#include <iomanip>
using std::string;
using std::to_string;
using std::stringstream;
using std::setw;
using std::setfill;
class Time
{
protected:
int hour;
int min;
int sec;
public:
//Allows the user to specify thestarting hour, minute, and second.
//Note that these parameters are givingdefault arguments via
//the equal sign, which means that if avalue is not set, it will
//default to 0.
Time(int h = 0, int m = 0, int s =0)
{
setTime(h, m,s);
}
//Sets a new time ONLY if the timerepresents an actual time.
bool setTime(int h, int m, int s)
{
if ((h < 0)|| (m < 0) || (m > 59) || (s < 0) || (s > 59))
{
returnfalse;
}
else
{
hour= h;
min= m;
sec= s;
returntrue;
}
}
bool setTime(int s)
{
if (s <0)
returnfalse;
else
{
//Convertthe time in seconds to H:M:S
hour= s / 3600;
min= (s - hour * 3600) / 60;
sec= (s - hour * 3600 - min * 60);
returntrue;
}
}
//Getters for each attribute.
int getHour() { return hour; }
int getMin() { return min; }
int getSec() { return sec; }
string toString()
{
//Similar tocout, but used for formatting a
//string.
stringstreamss;
//Set the fillfor empty space to be "0"
ss <<setfill('0');
//Append thehour to ss, formatted to two digits.
ss <<setw(2) << hour;
//Append acolon.
ss <<":";
//Do the samefor min and sec.
ss <<setw(2) << min;
ss <<":";
ss <<setw(2) << sec;
//Generaate thestring containing the time from
//the stream andreturn it.
returnss.str();
}
};
Instructions Provided on canvas for you is a file called "Time.h", which contains the Time class. This class allows the user of the class to create and use Time objects to represent the time of day, which is expressed in the form of hours, minutes, and seconds. It has the following operations already in place: Time(constructors)- Gives the user of the class the option to initialize the Time object with a starting time. getHour/Min/Sec- a set of getter methods that returns the hour, minute, and second component. setTime(H, M, S) - Allows the time to be set my passing in the hour(H), minute(M), and second(S). Note that it rejects invalid times (i.e, minutes and seconds must be between 1 and 59 and hour must be at least 1). setTime(seconds) - Overloads the setTime method to allow the time to be set based on seconds. For example: if the seconds is 8274, then the time that is set is 02:17:54. Note that it rejects any amount below zero. What you are going to do next is derive the Time class to create a new class called Countdown, which uses functionality already defined in the Time class to create a countdown object. This object will have two constructors: • One that accepts the starting time in raw seconds. • One that accepts the starting time in Hour, Minute, and Second format. The time class will create two attributes: an integer that stores the number of seconds left, and a Boolean that indicates the countdown is ready. When one of the constructors is called, it will set the
start time attribute, performing any conversion calculations needed as well as the H, M, and S components from the Time class, as long as the time given is valid. If valid, it will set the ready flag to true. If the time passed in is not valid, it will set the ready flag to false. To set the time, you will create an overloaded method called setStartTime. This method will accept either the raw seconds OR the time in H:M:S format and reinitialize the countdown. The following is a breakdown of the methods: setStartTime(int num_seconds) - Performs a validation to ensure that num_seconds is a valid amount of time. If so, it sets start_time to num_seconds and calls setTime to set the H:M:S. Then sets the ready flag to true. If the time is invalid, only set the ready flag to false. setStartTime(inth, int m, int s) - Performs a validation to ensure that the time passed in is a valid amount of time. If so, converts H:M:S to raw seconds and stores the conversion in start_time. Ten it uses setTime to store the time in H:M:S. Sets the ready flag to true if valid, and false otherwise. It is advised you create these two methods first, and then simply call them in the constructor to save time. Once you have completed that, you will then implement the begin_countdown function. The algorithm for the countdown is as follows: while start_time > 0: Sleep: subtract 1 from time_left Update the time using setTime Output the new time using toString Sleep for 1 second To sleep, you may use the following code: Preprocessor Directives: #include <chrono> #include <thread> std::this_thread::sleep_for(std::chrono::milliseconds(1000)); The countdown function should, when the countdown is complete, set the ready flag to false to indicate the start_time needs to be set again. Additionally, the countdown should output an error message if no valid start time has been set (i.e., if the ready flag is still false).
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply