7. The code below implements the Time ADT, without using a class. It produces th
ID: 3548192 • Letter: 7
Question
7. The code below implements the Time ADT, without using a class. It produces the following output:
8:30:59PM has passed 8:0:0AM
11:59:59PM
0:0:0AM
8:0:0AM
(1) Create a class called Time for the ADT. Convert the code by using the Time class.
Your code must produce identical output as mine. Your code must replace my set function with a constructor. It must also have a default constructor that sets the time to 0.
2 points
(2) List out all constructor(s), observer(s), transformer(s) in your new Time class.
0.75 points
#include <iostream>
using namespace std;
typedef long Time; // time of day represented in seconds
const int DAY_IN_SECONDS=24*60*60;
const int HOUR_IN_SECONDS=60*60;
const int MINUTE_IN_SECONDS=60;
bool Equal(Time t1, Time t2){
return t1==t2;
}
// set hours, minutes, seconds to Time t
void set(Time & t, int hours, int minutes, int seconds){
t = hours*HOUR_IN_SECONDS+minutes*MINUTE_IN_SECONDS+seconds;
}
// output t in 24 hour format (hours:minutes:seconds)
void Write24(Time t){
int hour = t/HOUR_IN_SECONDS;
int minute = t%HOUR_IN_SECONDS/MINUTE_IN_SECONDS;
int second = t%MINUTE_IN_SECONDS;
cout<<hour<<":"<<minute<<":"<<second;
}
// output t in 12 hour format (hours:minutes:seconds[AM|PM])
void Write12(Time t){
int hour = t/HOUR_IN_SECONDS;
int minute = t%HOUR_IN_SECONDS/MINUTE_IN_SECONDS;
int second = t%MINUTE_IN_SECONDS;
if (hour>=12)
cout<<hour%12<<":"<<minute<<":"<<second<<"PM";
else
cout<<hour<<":"<<minute<<":"<<second<<"AM";
}
// increment t by 1 second
// reset t to 0 when reaching midnight
void tick(Time & t){
t=t+1;
if (t==DAY_IN_SECONDS)
t=0;
}
int main(){
Time eight;
Time current;
set(eight, 8, 0, 0);
set(current, 20, 30, 59);
if (!Equal(eight, current))
{
Write12(current); cout<<" has passed "; Write12(eight); cout<<endl;
}
Time t;
set(t,23,59,59);
Write12(t);cout<<endl;
tick(t);
// midnight (0:0:0)
Write12(t);cout<<endl;
// advance to 8 AM
int i=0;
while (i<8*HOUR_IN_SECONDS)
{
tick(t);
i++;
}
if (Equal(t, eight))
{
Write12(t);cout<<endl;
}
return 0;
}
Explanation / Answer
NO
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.