Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please initialized the private variables in the order they were defined in follo

ID: 3677428 • Letter: P

Question

Please initialized the private variables in the order they were defined in following code. And make sure that the program compiles and does't have any errors!

By using:

Time::Time()
: is_24_hr(false), t_ms(0)
{}

--------------------------------------------------------------------

/* Time.cpp */

#include <iostream>

#include <typeinfo>

#include <string>

#include "Time.h"

#include <sstream>

using namespace std;

//////////////////////Conversion methods///////////////////////////////////////

long hrs_to_ms(int i)

{

if (i < 0)

cerr << "Method hrs_to_ms: non-positive value given. ";

return i*60*60*1000; //hrs-->min-->sec-->ms

}

long min_to_ms(int i)

{

if (i < 0)

cerr << "Method min_to_ms: non-positive value given. ";

return i*60*1000;

}

long sec_to_ms(int i)

{

if (i < 0)

cerr << "Method sec_to_ms: non-positive value given. ";

return i*1000;

}

int time_in_ms(long t)

{

return t % 1000;

}

int time_in_sec(long t)

{

return (t/1000)%60;

}

int time_in_min(long t)

{

return (t/1000/60)%60;

}

int time_in_24hrs(long t)

{

return (t/1000/60/60)%24;

}

int time_in_12hrs(long t)

{

return (t/1000/60/60)%12;

}

////////////////////////End Conversion Methods//////////////

//Default Constructor: Time is at 0 ms, not 24 hr format

Time::Time()

: t_ms(0), is_24_hr(false)

{}

//Overloaded Constructor: milliseconds only

Time::Time(long time)

: t_ms(time), is_24_hr(false)

{

if (time < 0)

cerr << "Time Constructor: non-positive value given for time.";

}

//Overloaded Constructor: Hours, minutes, seconds, ms format

Time::Time(int hours, int minutes, int seconds, int milli )

{

//Error checking: positive values

if (hours < 0 || minutes < 0 || seconds < 0 || milli < 0)

cerr <<"Time Constructor: non-positive value given ";

if (hours > 23)

cerr << "Time Constructor: invalid hour value ";

if (minutes >= 60)

cerr << "Time Constructor: invalid minute value ";

if (seconds > 60)

cerr << "Time Constructor: invalid seconds value ";

if (milli > 999)

cerr << "Time Constructor: invalid milliseconds value ";

long h_conv = hrs_to_ms(hours);

long m_conv = min_to_ms(minutes);

long s_conv = sec_to_ms(seconds);

t_ms = h_conv + m_conv + s_conv + milli;

is_24_hr = false;

}

//Destructor

Time::~Time(void)

{}

////////////////////////////////Methods////////////////////////////////////////////

long Time::asLong() const

{

return t_ms;

}

string Time::toString() const

{

int hrs, min, sec, ms;

ostringstream s;

/*Hour assignment: check for 24 hour flag, adjust hour assignment an AM/PM output */

if (is_24_hr)

{

hrs = time_in_24hrs(t_ms);

min = time_in_min(t_ms);

sec = time_in_sec(t_ms);

ms = time_in_ms(t_ms);

s << hrs << ":" << min << ":" << sec << ":" << ms;

}

/* Output as 12-hour time, add AM/PM to output string */

else

{

/* Check for midnight state */

if (time_in_12hrs(t_ms) == 0)

hrs = 12;

else

hrs = time_in_12hrs(t_ms);

min = time_in_min(t_ms);

sec = time_in_sec(t_ms);

ms = time_in_ms(t_ms);

/* AM/PM check */

/* Check the time using time_in_24hrs, not time_in_12hrs, which is always < 12 */

if (time_in_24hrs(t_ms) < 12)

s << hrs << ":" << min << ":" << sec << ":" << ms << " AM";

else

s << hrs << ":" << min << ":" << sec << ":" << ms << " PM";

}

return s.str();

}

void Time::set24Hour(bool value)

{

/* DEBUG */

//cout << "set24Hour: value is " << value;

is_24_hr = value;

}

bool Time::is24Hour() const

{

return is_24_hr;

}

-------------------------------------------------------------------

/* Time.h */

#ifndef TIME_H_
#define TIME_H_
#include <iostream>
#include <string>

/**
* Time class
*
* The Time class contains time as hours:minutes:seconds:milliseconds (AM/PM).
*/
class Time
{
public:
/**
* Constructor with zero values
*/
Time();
/**
* Constructors with arguments
*/
Time(long time);
Time(int hours, int minutes, int seconds, int milli);
/**
* Deconstructor
*/
virtual ~Time();
/**
* Return time as a long value representing time in milliseconds
*/
long asLong() const;
/**
* Display as a string in the format hours:minutes:seconds:milliseconds.
* For example 1:45:30:56 PM
*
* The time is displayed as 24 hours if the 24 hour flag is set true.
*/
std::string toString() const;
/**
* Enable/disable 24 hour time display
*/
void set24Hour(bool value);
/**
* Return true if 24 hour time display is enabled
*/
bool is24Hour() const;
private:
bool is_24_hr;
long t_ms;
};
#endif /* TIME_H_ */

--------------------------------------------------

/* Main.cpp */

#include <iostream>
#include <string>
#include "Time.h"
#include <sstream>

using namespace std;

int main()
{

/* Default constructor test */
cout << "Object test: ";
Time test;
if (test.is24Hour())
cout << "test is 24 hour formatted";
else
cout << "test is not 24 hour formatted";
cout << endl <<test.toString() << " ";

/* Overloaded test: asLong() test */
cout << "Object raw: ";
Time raw(14,02,50,600);
raw.set24Hour(true);
if (raw.is24Hour())
cout << "raw is 24 hour formatted ";
else
cout << "raw is not 24 hour formatted ";
cout << raw.toString() << " Time in ms: " << raw.asLong() << endl;

/* Overloaded test: 24 hour */
cout << "Object now: ";
Time now(13, 20, 04, 600);
now.set24Hour(true);
if (now.is24Hour())
cout << "now is 24 hour formatted";
else
cout << "now is not 24 hour formatted";
cout << endl <<now.toString() << " ";

/* Overloaded test: 12 hour */
cout << "Object t_money: ";
Time t_money (15, 55, 43, 900);
if (t_money.is24Hour())
cout << "t_money is 24 hour formatted";
else
cout << "t_money is not 24 hour formatted";
cout << endl <<t_money.toString() << " ";

/* Overloaded test: max time, min time [ALL WORKING, MIDNIGHT CASE HANDLED]*/
cout << "Object min: ";
Time min (0,0,0,0);
cout << min.toString() << " ";
min.set24Hour(true);
cout << min.toString() << " ";

cout << "Object max: ";
Time max (23, 59, 59, 999);
cout << max.toString() << " ";
max.set24Hour(true);
cout << max.toString() << " ";

/* Overloaded test: bad input */
cout << "Object bad1: ";
Time bad1 (-30, 80, 109, 1010); //Negative, out of bound values
cout << bad1.toString() << " ";

cout << "Object bad2: ";
Time bad2 ('T', 'a', 6, 509); //non-integer values
cout << bad2.toString() << " ";

cout << "Object bad3: ";
Time bad3 (14, 43, 50, 1010); //Out of bound only
cout << bad3.toString() << " ";

cout << "Object bad4: ";
Time bad4 (15, 45, 20, 50);
cout << bad4.toString() << " ";

}

Explanation / Answer

I have made the required changes in Time.cpp

Time.cpp

#include <iostream>
#include <typeinfo>
#include <string>
#include "Time.h"
#include <sstream>
using namespace std;
//////////////////////Conversion methods///////////////////////////////////////
long hrs_to_ms(int i)
{
if (i < 0)
cerr << "Method hrs_to_ms: non-positive value given. ";
return i*60*60*1000; //hrs-->min-->sec-->ms
}
long min_to_ms(int i)
{
if (i < 0)
cerr << "Method min_to_ms: non-positive value given. ";
return i*60*1000;
}
long sec_to_ms(int i)
{
if (i < 0)
cerr << "Method sec_to_ms: non-positive value given. ";
return i*1000;
}
int time_in_ms(long t)
{
return t % 1000;
}
int time_in_sec(long t)
{
return (t/1000)%60;
}
int time_in_min(long t)
{
return (t/1000/60)%60;
}
int time_in_24hrs(long t)
{
return (t/1000/60/60)%24;
}
int time_in_12hrs(long t)
{
return (t/1000/60/60)%12;
}
////////////////////////End Conversion Methods//////////////
//Default Constructor: Time is at 0 ms, not 24 hr format
Time::Time()
: is_24_hr(false),t_ms(0)
{}
//Overloaded Constructor: milliseconds only
Time::Time(long time)
: is_24_hr(false),t_ms(time)
{
if (time < 0)
cerr << "Time Constructor: non-positive value given for time.";
}
//Overloaded Constructor: Hours, minutes, seconds, ms format
Time::Time(int hours, int minutes, int seconds, int milli )
{
//Error checking: positive values
if (hours < 0 || minutes < 0 || seconds < 0 || milli < 0)
cerr <<"Time Constructor: non-positive value given ";
if (hours > 23)
cerr << "Time Constructor: invalid hour value ";
if (minutes >= 60)
cerr << "Time Constructor: invalid minute value ";
if (seconds > 60)
cerr << "Time Constructor: invalid seconds value ";
if (milli > 999)
cerr << "Time Constructor: invalid milliseconds value ";
long h_conv = hrs_to_ms(hours);
long m_conv = min_to_ms(minutes);
long s_conv = sec_to_ms(seconds);
is_24_hr = false;
t_ms = h_conv + m_conv + s_conv + milli;
}
//Destructor
Time::~Time(void)
{}
////////////////////////////////Methods////////////////////////////////////////////
long Time::asLong() const
{
return t_ms;
}
string Time::toString() const
{
int hrs=0, min=0, sec=0, ms=0;
ostringstream s;
/*Hour assignment: check for 24 hour flag, adjust hour assignment an AM/PM output */
if (is_24_hr)
{
hrs = time_in_24hrs(t_ms);
min = time_in_min(t_ms);
sec = time_in_sec(t_ms);
ms = time_in_ms(t_ms);
s << hrs << ":" << min << ":" << sec << ":" << ms;
}
/* Output as 12-hour time, add AM/PM to output string */
else
{
/* Check for midnight state */
if (time_in_12hrs(t_ms) == 0)
hrs = 12;
else
hrs = time_in_12hrs(t_ms);
min = time_in_min(t_ms);
sec = time_in_sec(t_ms);
ms = time_in_ms(t_ms);
/* AM/PM check */
/* Check the time using time_in_24hrs, not time_in_12hrs, which is always < 12 */
if (time_in_24hrs(t_ms) < 12)
s << hrs << ":" << min << ":" << sec << ":" << ms << " AM";
else
s << hrs << ":" << min << ":" << sec << ":" << ms << " PM";
}
return s.str();
}
void Time::set24Hour(bool value)
{
/* DEBUG */
//cout << "set24Hour: value is " << value;
is_24_hr = value;
}
bool Time::is24Hour() const
{
return is_24_hr;
}

output:

Object test:

test is not 24 hour formatted

12:0:0:0 AM

Object raw:

raw is 24 hour formatted

14:2:50:600

Time in ms: 50570600

Object now:

now is 24 hour formatted

13:20:4:600

Object t_money:

t_money is not 24 hour formatted

3:55:43:900 PM

Object min:

12:0:0:0 AM

0:0:0:0

Object max:

11:59:59:999 PM

23:59:59:999

Object bad1:

Time Constructor: non-positive value given

Time Constructor: invalid minute value

Time Constructor: invalid seconds value

Time Constructor: invalid milliseconds value

Method hrs_to_ms: non-positive value given.

-4:-38:-9:-990 AM

Object bad2:

Time Constructor: invalid hour value

Time Constructor: invalid minute value

1:37:6:509 PM

Object bad3:

Time Constructor: invalid milliseconds value

2:43:51:10 PM

Object bad4:

3:45:20:50 PM