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

Please help with this question if you could. Thanks ____________________________

ID: 3549355 • Letter: P

Question

Please help with this question if you could. Thanks

_________________________________________________________________

Create a class Time24 that stores a time in 24 hour format.  The Time24 class must store the time using three data members: hours, minutes, seconds.  The Time24 class handles times with hours in the range of 0 to 23, minutes in the range 0 to 59, and seconds in the range 0 to 59.  Provide a constructor that initializes hours, minutes, and seconds. The constructor must provide default values of 0 for hours, minutes, and seconds. The constructor must validate the given hours, minutes, and seconds values.  If an invalid value is given, the value should be set to 0.

___________________________________________________________________________________

An algorithm to add s seconds to a 24 hour time follows:

seconds = seconds + s

minutes = minutes + seconds / 60

seconds = seconds % 60

hours = hours + minutes / 60

minutes = minutes % 60

___________________________________________________________________________

#ifndef TIME24_H

#define TIME24_H

#include <iostream>

using namespace std;

class Time24

{

public:

    explicit Time24(const int h=0, const int m=0, const int s=0);

    Time24 operator+(const Time24 &t) const;               // this + t

    Time24 operator+(const int s) const;                   // this + s

    // Global friend function

    friend Time24 operator+(const int s, const Time24& t); // s + t

    Time24& operator+=(const Time24 &t);                   // this += t

    Time24& operator+=(const int s);                       // this += s

    Time24& operator++();                                  // ++this

    bool operator==(const Time24 &t) const;                // this == t

    bool operator!=(const Time24 &t) const;                // this != t

    bool operator< (const Time24 &t) const;                // this <  t

    bool operator<=(const Time24 &t) const;                // this <= t

    bool operator> (const Time24 &t) const;                // this >  t

    bool operator>=(const Time24 &t) const;                // this >= t

    // Global friend functions supporting input/output.

    friend istream& operator>>(istream &in, Time24 &t);         // hh:mm:ss

    friend ostream& operator<<(ostream &out, const Time24 &t);  // hh:mm:ss

private:

   int hours;     // valid values: 0-23

    int minutes;   // valid values: 0-59

    int seconds;   // valid values: 0-59

};

#endif

Explanation / Answer

//Time24.h Header File
#ifndef TIME24_H
#define TIME24_H
#include <iostream>
using namespace std;
class Time24
{
public:
explicit Time24(const int h=0, const int m=0, const int s=0);
Time24 operator+(const Time24 &t) const; // this + t
Time24 operator+(const int s) const; // this + s
// Global friend function
friend Time24 operator+(const int s, const Time24& t); // s + t
Time24& operator+=(const Time24 &t); // this += t
Time24& operator+=(const int s); // this += s
Time24& operator++(); // ++this
Time24 operator++(int);
bool operator==(const Time24 &t) const; // this == t
bool operator!=(const Time24 &t) const; // this != t
bool operator< (const Time24 &t) const; // this < t
bool operator<=(const Time24 &t) const; // this <= t
bool operator> (const Time24 &t) const; // this > t
bool operator>=(const Time24 &t) const; // this >= t
// Global friend functions supporting input/output.
friend istream& operator>>(istream &in, Time24 &t); // hh:mm:ss
friend ostream& operator<<(ostream &out, const Time24 &t); // hh:mm:ss
private:
int hours; // valid values: 0-23
int minutes; // valid values: 0-59
int seconds; // valid values: 0-59
};
#endif

// Time24.cpp

Time24::Time24(const int h, const int m, const int s)
{
hours = (h>=0 && h<=23)?h:0;
minutes = (m>=0 && m<=59)?m:0;
seconds = (s>=0 && s<=59)?s:0;
}
Time24 Time24::operator+(const Time24 &t) const // this + t
{
Time24 local;
local.seconds = this->seconds + t.seconds;
local.minutes = this->minutes + t.minutes+ (local.seconds / 60);
local.seconds = local.seconds % 60;
local.hours = this->hours + t.hours + (local.minutes / 60);
local.minutes = local.minutes % 60;
return local;
}
Time24 Time24::operator+(const int s) const // this + s
{
Time24 local;
local.seconds = this->seconds + s;
local.minutes = this->minutes + (local.seconds / 60);
local.seconds = local.seconds % 60;
local.hours = this->hours + (local.minutes / 60);
local.minutes = local.minutes % 60;
return local;
}
// Global friend function
Time24 operator+(const int s, const Time24& t) // s + t
{
return t+s;
}
Time24& Time24::operator+=(const Time24 &t) // this += t
{
seconds = seconds + t.seconds;
minutes = minutes + t.minutes+ (seconds / 60);
seconds = seconds % 60;
hours = hours + t.hours + (minutes / 60);
minutes = minutes % 60;
return *this;
}
Time24& Time24::operator+=(const int s) // this += s
{
seconds = seconds + s;
minutes = minutes + seconds / 60;
seconds = seconds % 60;
hours = hours + minutes / 60;
minutes = minutes % 60;
return *this;
}
Time24& Time24::operator++() // ++this
{
++seconds;
minutes = minutes + seconds / 60;
seconds = seconds % 60;
hours = hours + minutes / 60;
minutes = minutes % 60;
return *this;
}
Time24 Time24::operator++(int k)
{
Time24 local(*this);
++(*this);
return local;
}
bool Time24::operator==(const Time24 &t) const // this == t
{
return (hours==t.hours && minutes == t.minutes && seconds == t.seconds);
}
bool Time24::operator!=(const Time24 &t) const // this != t
{
return (hours!=t.hours || minutes != t.minutes || seconds == t.seconds);
}
bool Time24::operator< (const Time24 &t) const // this < t
{
if(hours<t.hours) return true;
else if(hours==t.hours && minutes < t.minutes)
return true;
else if(hours==t.hours && minutes == t.minutes && seconds<t.seconds)
return true;
return false;
}
bool Time24::operator<=(const Time24 &t) const // this <= t
{
if(hours<=t.hours) return true;
else if(hours==t.hours && minutes <= t.minutes)
return true;
else if(hours==t.hours && minutes == t.minutes && seconds<=t.seconds)
return true;
return false;
}
bool Time24::operator> (const Time24 &t) const // this > t
{
return !(*this<t);
}
bool Time24::operator>=(const Time24 &t) const // this >= t
{
return !(*this<=t);
}
// Global friend functions supporting input/output.
istream& operator>>(istream &in, Time24 &t) // hh:mm:ss
{
in >> t.hours >> t.minutes >> t.seconds;
return in;
}
ostream& operator<<(ostream &out, const Time24 &t) // hh:mm:ss
{
out << t.hours << ":" << t.minutes << ":" << t.seconds << endl;
return out;
}

// main.cpp

int main()
{
Time24 T1(1,2,3);
Time24 T2;
cout <<"Enter hours, minutes and seconds of time";
cin >> T2;
cout << endl;
cout << (T1+T2) << endl;
cout << (T1+59) << endl;
return 0;
}