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

Please fix the following code. (When implementing time math make sure to correct

ID: 3675019 • Letter: P

Question

Please fix the following code.

(When implementing time math make sure to correctly manage wrap around. As an example what is 23 hours plus 4 hours? and also consider using the toLong() member function for your math. It is a lot easier to add to long values and check for time underflow or overflow. )

mian.cpp

#include <iostream>

#include <string>

#include "Time.h"

#include <sstream>

#include <limits>

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() << " ";

   /* Operator override test */

   /* Output */

   cout << "Override test: << ";

   Time over1{15, 45, 39, 120};

   cout << "toString() output: " << over1.toString() << " ";

   cout << "Override output: " << over1 << " ";

   /* Less than: Tested <, >, and == */

   cout << "Override test: < ";

   Time x{5, 45, 39, 120};

   Time y{15, 45, 39, 120};

   cout << "x == " << x.asLong() << ", y == "

           << y.asLong() << " ";

   if (x < y)

       cout << "x is < y ";

   else

       cout << "x is not < y ";

   /* Greater than: Tested >, <, and == */

   cout << "Override test: > ";

   Time a{2,0,0,0};

   Time b{3,0,0,0};

   cout << "a == " << a.asLong() << ", b == "

           << b.asLong() << " ";

   if (b > a)

       cout << "b is > a ";

   else

       cout << "b is not > a ";

   /* Equals: both equality and inequality work */

   cout << "Equality test: == ";

   Time c{2,30,30,50};

   Time d{2,30,30,50};

   Time e{5, 6, 7, 8};

   Time f{10, 10, 10, 10};

   cout << "c == " << c.asLong() << ", d == " << d.asLong()

           << ", e == " << e.asLong() << ", f == "

           << f.asLong() << " ";

   if (c==d)

       cout << "c==d ";

   else

       cout << "c!=d ";

   if (e==f)

       cout << "e==f ";

   else

       cout << "e!=f ";

   /* Plus: */

   cout << "Addition test: + ";

   Time g{10,0,0,0};

   Time h{0,45,30,0};

   Time i{};

   cout << "g == " << g << ", h == " << h << " ";

   i = g + h;

   g = {12,0,0,0};

   h = {13,0,0,0};

   i = g + h;

   cout << "g + h == " << i << " ";

   cout << "g == " << g << ", h == " << h << " ";

   cout << "g + h == " << i << " ";

   cout << endl;

   /* Minus: */

   cout << "Subtraction test: - ";

   Time j {20,30,0,0}; /* 5:00:00 AM */

   Time k {5,0,0,0}; /* 8:30:00 PM */

   Time l{};

   l = j-k;

   cout << "j == " << j << ", k == " << k << endl;

   cout << "j == " << j.asLong() << "ms, k == " << k.asLong() << "ms"

           << ", j - k == " << l.asLong() << "ms ";

   /* Expected output: 3:30:00 PM */

   cout << "j - k == " << l << " ";

   j = {5,10,0,0}; //5:10:00 AM

   k = {20,20,0,0}; //8:20:00 PM

   l = j - k;

   /* Expected output 8:50:00 AM

   *

   */

   cout << "j == " << j << ", k == " << k << endl;

   cout << "j == " << j.asLong() << "ms, k == " << k.asLong() << "ms"

           << ", j - k == " << l.asLong() << "ms ";

   cout << "j - k == " << l << " ";

}

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

/* Time.cpp */

#include <iostream>

#include <typeinfo>

#include <limits>

#include <string>

#include "Time.h"

#include <sstream>

#include <cmath>

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; //min-->sec-->ms

}

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 ";

   /* Check for overflow values*/

   if (time > numeric_limits<long>::max())

       cerr << "Time Constructor: time value overflow ";

}

//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;

   /* Check for overflow values */

   if (t_ms > numeric_limits<long>::max())

       cerr << "Time Constructor: time value overflow ";

   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;

}

//////////////////////////* Override Operators *//////////////////////////////////////////////

/* Output: << acts as a toString() call */

ostream& operator <<(ostream& s, const Time& t)

{

   s << t.toString(); /*Assign the time output of the passed Time object

                       to the output stream*/

   return s;

}

/* Less than: Compare lhs time to rhs time using asLong() method */

bool operator <(const Time& lhs, const Time& rhs)

{

   bool result;

   if (lhs.asLong() < rhs.asLong() )

       result = true;

   else

       result = false;

   return result;

}

/* Greater than: return !(a<b), unless a == b, which should return False */

bool operator >(const Time& lhs, const Time& rhs)

{

   bool result;

   if (lhs.asLong() == rhs.asLong())

       result = false;

   else

       result = !(lhs < rhs);

   return result;

}

/* Equal to: Compare lhs.asLong() to rhs.asLong() for equality */

bool operator ==(const Time& lhs, const Time& rhs)

{

   return (lhs.asLong() == rhs.asLong());

}

/* Plus operator: this + arg.asLong() */

Time Time::operator +(const Time& t) const

{

   Time result {};

   result = this->asLong() + t.asLong();

   return result;

}

/* Minus Operator: this - arg.asLong() */

Time Time::operator -(const Time& t) const

{

   Time result{};

   int h,m,s,ms, this_long, t_long;

   /*Constant values: avoid magic constants! */

   const int hrs_in_day = 24;

   const int min_in_hr = 60;

   const int sec_in_min = 60;

   const int ms_in_sec = 1000;

   /* Store the long times of the operands */

   this_long = this->asLong();

   t_long = t.asLong();

   /* Positive result: Assign time using conversion methods */

   if (this_long - t_long >=0 )

   {

       h = time_in_24hrs(this_long) - time_in_24hrs(t_long);

       m = time_in_min(this_long) - time_in_min(t_long);

       s = time_in_sec(this_long) - time_in_sec(t_long);

       ms = time_in_ms(this_long) - time_in_ms(t_long);

   }

   /* Negative result: Check for borrow case, assign hrs, min, sec, ms individually. */

   else

   {

       h = (hrs_in_day + (time_in_24hrs(this_long) - time_in_24hrs(t_long) ))%24;

       //"Borrow" from hours if there are negative minutes

       if (time_in_min(this_long) - time_in_min(t_long) < 0)

           h--;

       m = (min_in_hr +( time_in_min(this_long) - time_in_min(t_long) ))%60;

       //Repeat borrow for minutes and seconds

       if (time_in_sec(this_long) - time_in_sec(t_long) < 0)

           m--;

       s = (sec_in_min + ( time_in_sec(this_long) - time_in_sec(t_long) ))%60;

       if (time_in_ms(this_long) - time_in_ms(t_long) < 0)

           s--;

       ms = (ms_in_sec + ( time_in_ms(this_long) - time_in_ms(t_long) ))%1000;

   }

   result = {h,m,s,ms};

   return result;

}

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

/* Time.h */

#ifndef TIME_H_

#define TIME_H_

#include <iostream>

#include <sstream>

#include <stdexcept>

#include <string>

using namespace std;

/**

* 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 millisecondsF

   */

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.

   */

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;

/**

   * Output the time to an output stream

   */

friend std::ostream& operator <<(std::ostream&, const Time&); // Output a Time to an output stream

/**

   * Define ordering relationships

   */

friend bool operator <(const Time& lhs, const Time& rhs);

friend bool operator >(const Time& lhs, const Time& rhs);

friend bool operator ==(const Time& lhs, const Time& rhs);

/**

   * Define addition and subtraction

   */

Time operator +(const Time&) const;

Time operator -(const Time&) const;

private:

   /**

   * Private members go here

   */

long t_ms; //Time variable, in milliseconds

bool is_24_hr; //24 hour flag

/* Define helper methods */

friend long hrs_to_ms(int i);

friend long min_to_ms(int i);

friend long sec_to_ms(int i);

friend int time_in_ms(long t);

friend int time_in_sec(long t);

friend int time_in_min(long t);

friend int time_in_24hrs(long t);

friend int time_in_12hrs(long t);

};

#endif /* TIME_H_ */

Explanation / Answer

Main.cpp


#include <iostream>
#include <string>
#include "Time.h"
#include <sstream>
#include <limits>
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() << " ";
/* Operator override test */
/* Output */
cout << "Override test: << ";
Time over1{15, 45, 39, 120};
cout << "toString() output: " << over1.toString() << " ";
cout << "Override output: " << over1 << " ";
/* Less than: Tested <, >, and == */
cout << "Override test: < ";
Time x{5, 45, 39, 120};
Time y{15, 45, 39, 120};
cout << "x == " << x.asLong() << ", y == "
<< y.asLong() << " ";
if (x < y)
cout << "x is < y ";
else
cout << "x is not < y ";
/* Greater than: Tested >, <, and == */
cout << "Override test: > ";
Time a{2,0,0,0};
Time b{3,0,0,0};
cout << "a == " << a.asLong() << ", b == "
<< b.asLong() << " ";
if (b > a)
cout << "b is > a ";
else
cout << "b is not > a ";
/* Equals: both equality and inequality work */
cout << "Equality test: == ";
Time c{2,30,30,50};
Time d{2,30,30,50};
Time e{5, 6, 7, 8};
Time f{10, 10, 10, 10};
cout << "c == " << c.asLong() << ", d == " << d.asLong()
<< ", e == " << e.asLong() << ", f == "
<< f.asLong() << " ";
if (c==d)
cout << "c==d ";
else
cout << "c!=d ";
if (e==f)
cout << "e==f ";
else
cout << "e!=f ";
/* Plus: */
cout << "Addition test: + ";
Time g{10,0,0,0};
Time h{15,45,30,0};
Time i{};
cout << "g == " << g << ", h == " << h << " ";
i = g + h;
cout << "g + h == " << i << " ";
g = {12,0,0,0};
h = {13,0,0,0};
cout << "g == " << g << ", h == " << h << " ";
i = g + h;
cout << "g + h == " << i << " ";
cout << "g == " << g << ", h == " << h << " ";
cout << "g + h == " << i << " ";
Time ms1 {21*60*60*1000};
Time ms2 {14*60*60*1000};
Time ms3{};
cout << "ms1 == " << ms1.asLong() << " or " << ms1 << ", ms2 == "
<< ms2.asLong() << " or " << ms2 << " ";
ms3 = ms1+ms2;
cout << "ms1 + ms2 == " << ms3.asLong() << " or " << ms3 << " ";
cout << endl;
/* Minus: */
cout << "Subtraction test: - ";
Time j {20,30,0,0}; /* 5:00:00 AM */
Time k {5,0,0,0}; /* 8:30:00 PM */
Time l{};
l = j-k;
cout << "j == " << j << ", k == " << k << endl;
cout << "j == " << j.asLong() << "ms, k == " << k.asLong() << "ms"
<< ", j - k == " << l.asLong() << "ms or " << l << " ";
cout << "Subtraction Test 2: millisecond ops ";
j = {140500};
k = {200690};
cout << "j == " << j.asLong() << " or " << j << ", k == " << k.asLong() << " or " << k << " ";
l = j-k;
cout << "j-k == " << l.asLong() << ", or " << l << " ";
cout << "Subtraction Test 3: Time code subtraction: ";
j = {5,10,0,0}; //5:10:00 AM
k = {20,20,0,0}; //8:20:00 PM
l = j - k;
/* Expected output 8:50:00 AM
*
*/
cout << "j == " << j << ", k == " << k << endl;
cout << "j == " << j.asLong() << "ms, k == " << k.asLong() << "ms"
<< ", j - k == " << l.asLong() << "ms ";
cout << "j - k == " << l << " ";
/* Assignment Operator */
/* Test standard copy */
cout << "Assignment Test: ";
cout << "Copy Constructor Test: Time and_for_what(christmas_morning) ";
Time christmas_morning {6,0,0,0};
Time and_for_what(christmas_morning);
cout << "christmas_morning == " << christmas_morning << ", and_for_what == "
<< and_for_what << " ";
christmas_morning.set24Hour(true);
cout << christmas_morning << " on a Christmas morning, and for what?! ";
cout << "Standard Copy Test: noon = lunch ";
Time lunch {12,0,0,0};
Time noon = lunch;
cout << "lunch == " << lunch << ", noon == " << noon;
cout << " ";
/* Test 24-hour boolean copy*/
cout << "24 Hour State Copy Test: evening = dinner in 24hr time ";
Time dinner = {18,30,0,0}; //6:30 PM
dinner.set24Hour(true); //Dinner is now 18:30:0:0
Time evening = dinner;
cout << "dinner == " << dinner << ", evening == " << evening << " ";
/* Test Copy chaining */
cout << "Copy chaining test: all values should be the same as the farthest time on the right. ";
Time Another, Brick, In, The = {0};
Time Wall {13,44,25,650}; //Time is 1:44:25:650
cout << "Wall == " << Wall << " ";
cout << "Another = Brick = In = The = Wall ";
Another = Brick = In = The = Wall; // All Times should now be 1:44:25:650
cout << "Another == " << Another << ", Brick == " << Brick
<< ", In == " << In << ", The == " << The << ", Wall == " << Wall << " ";
}

* **Time.cpp * * *
#include "Time.h"
Time::Time()
{   
hours=0;   
minutes=0;   
seconds=0;   
milli=0;
time=0;
}
Time::Time(int hr, int min, int sec, int mil)
{   
hours = hr;   
minutes = min;   
seconds = sec;   
milli = mil;   
time = 0;
}
Time::Time(long tm)
{   
time = tm;
} Time::~Time()
{
}