Question: C++ The code below implements the Time ADT, without using a class. It produces the following output: 20:30:59 has passed 8:0:0 23:59:59 0:0:0 8:0:0 (1)

C++

The code below implements the Time ADT, without using a class. It produces the following output:

20:30:59 has passed 8:0:0 23:59:59 0:0:0 8:0:0

(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) List out all constructor(s), observer(s), transformer(s) in your new Time class.

#include "stdafx.h"

#include

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;

}

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

{

Write24(current); cout << " has passed "; Write24(eight); cout << endl;

}

Time t;

set(t, 23, 59, 59);

Write24(t); cout << endl;

tick(t);

// midnight (0:0:0)

Write24(t); cout << endl;

// advance to 8 AM

int i = 0;

while (i<8 * HOUR_IN_SECONDS)

{

tick(t);

i++;

}

if (Equal(t, eight))

{

Write24(t); cout << endl;

}

cin.ignore();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!