Design and implement a \"Time\" class (ADT-abstract data type) that represents a
ID: 3793995 • Letter: D
Question
Design and implement a "Time" class (ADT-abstract data type) that represents a specific time of day. You can a object's hour, minutes and seconds as integers (for example, 10:23:15. is simplest to store these data member values in 24-hour "Military" me (e.g., 0:00:00 23:59:59) This data must be in the private section of the Time class that implements the ADT. In all cases throughout your Time object implementation, you must ensure and validate that these data member values are within their legitimate ranges. Include two initialization operation(s) (e.g., constructors) in your class a "convert constructor" uses client-supplied "24-hour" values for the ADT's data (be sure to validate the time data. Illegal values should result in same initialization as the default constructor) Example: Tine myAppt 15, o, o 3 o'clock in the afternoon a "default constructor" automatically initializes the Time object with the current time on the computer e Example: Time rightNow This current-time initialization can be accomplished in your code by using the built-inExplanation / Answer
File #1 Main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <time.h>
#include "Prog1.h"
using namespace std;
int main()
{
time_t now;
struct tm tstruct;
time(&now);
tstruct = *localtime(&now);
int hour = tstruct.tm_hour;
int min = tstruct.tm_min;
int sec = tstruct.tm_sec;
cout << "Time is " << hour<<":"<< min<<":"<< sec << endl;
Time alarmTime(00, 50, 30);
string alarmString;
cout << alarmString << endl;
alarmString = alarmTime.get24HourFormat();
cout << alarmString << endl;
alarmString = alarmTime.get12HourAmPmFormat();
cout << alarmString << endl;
alarmTime.increamentSeconds(45);
cout << alarmTime.get24HourFormat() << endl;
cout<< alarmTime.get12HourAmPmFormat()<< endl;
alarmTime.increamentSeconds(200);
cout << alarmTime.get24HourFormat() << endl;
cout << alarmTime.get12HourAmPmFormat() << endl;
alarmTime.increamentMinutes(63);
cout << alarmTime.get24HourFormat() << endl;
cout << alarmTime.get12HourAmPmFormat() << endl;
}
File #2 Prog1.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include"Prog1.h"
using namespace std;
#define SECS_PER_MIN 60
#define SECS_PER_HOUR 3600
#define MINS_PER_HOUR 60
Time::Time()
{
}
Time::Time(int a, int b, int c): hour(a), min(b), sec(c)
{
}
string Time::get24HourFormat()
{
string time_hour = to_string(hour);
string time_min = to_string(min);
string time_sec = to_string(sec);
string time_24 = time_hour +":"+ time_min +":"+ time_sec;
//cout << "Value of string time is: " << time_24 << endl;
return time_24;
}
string Time::get12HourAmPmFormat()
{
string time_hour;
string AM_PM;
if (hour > 12)
{
time_hour = to_string(hour - 12); // 23 so 11PM
AM_PM = "PM";
}
else
{
time_hour = to_string(hour);
AM_PM = "AM";
}
string time_min = to_string(min);
string time_sec = to_string(sec);
string time_12 = time_hour + ":" + time_min + ":" + time_sec+ " "+AM_PM;
//cout << "Value of string time is: " << time_24 << endl;
return time_12;
}
void Time::increamentHours(int inc_h)
{
hour = hour + inc_h;
}
void Time::increamentMinutes(int inc_m)
{
// 200 mints
int getHour = 0;
int getMin = 0;
if (inc_m >= 60)
{
getHour = inc_m / MINS_PER_HOUR; // 3
getMin = inc_m % MINS_PER_HOUR; // 20
}
else
{
getMin = inc_m;
}
hour = hour + getHour;
min = min + getMin;
}
void Time::increamentSeconds(int inc_s)
{
// 20100 seconds
int getHour = inc_s / SECS_PER_HOUR; // 5
int getMin = inc_s / SECS_PER_MIN; // 335
int min_left = getMin % SECS_PER_MIN;
int secs_left = inc_s % SECS_PER_MIN;
hour = hour + getHour;
min = min + min_left;
sec = sec + secs_left;
if (sec >=60)
{
min = min + (sec / 60);
sec = (sec % 60);
}
if (min >=60)
{
hour = hour + (min / 60);
min = (hour % 60);
}
if (hour >= 24)
hour = hour - 24;
}
File#3 Prog1.h
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include <string>
using namespace std;
class Time
{
private:
int hour;
int min;
int sec;
public:
Time();
Time(int a, int b, int c);
string get24HourFormat();
string get12HourAmPmFormat();
void increamentHours(int);
void increamentMinutes(int);
void increamentSeconds(int);
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.