Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I think I have a good chunk of this figured out, but am running out of time and still have some coding to finish, any

I think I have a good chunk of this figured out, but am running out of time and still have some coding to finish, any help would be appreciated. This is for C++. The instructions for the lab where

Complete the following: - In clockPart2.cpp: complete the code for all the methods that are not completely written - In clockLabPart2Driver.cpp - In void visualTestForSetAheadAnBackMembererFunctions() write the test code for set hours ahead and back - In void visualTestForConstructors() write the test code for the argument constructor and the copy constructor - Following the pattern given for the 2 visual test functions above add the following 2 visual test functions to your code 1. write the test code for each visual test 2. call each function from the visualTests() function void visualTestForSyncAndAssignmentOperator(); void visualTestForDifference();

Right now I am stuck on what to write for this section: void Clock::sync(Clock& right) const

and what to write for this section: int Clock::differenceInClocks(const Clock& right) const

clockPart2.h

#ifndef CLOCK_H

#define CLOCK_H

#include

#include

using namespace::std;

class Clock {

public:

Clock(void);

Clock(int hr, int m, bool chimeHalfHour, bool chimeHour);

Clock(const Clock& right);

~Clock();

void sync(Clock& right) const; // parameter will have same time as invoking instance

void setToCurrentTime(void);

void setHoursAhead(int h); // Assume 1 <= h <= 23

void setHoursBack(int h); // Assume 1 <= h <= 23

void setMinutesAhead(int m); // Assume 1 <= m <= 59, leave hours the same

void setMinutesBack(int m); // Assume 1 <= m <= 59, leave hours the same

int differenceInClocks(const Clock& right) const;

// will return in minutes the absolute value of the time difference between the

// invoking instance and the parameter

// HINT: instead of comparing 1 hour 55 minutes to 2 hours 12 minutes

// convert both times to minutes past midnight and work with those numbers

// overloaded = operator, study Lecture 3 for an example of the = operator

const Clock& operator=(const Clock& right);

int getMinutes(void) const;

int getHours(void) const;

bool getChimeOnHalfHour(void) const;

bool getChimeOnHour(void) const;

void setMinutes(int m);

void setHours(int h);

void setChimeOnHalfHour(bool v);

void setChimeOnHour(bool v);

string chimeOnHalfHour(void) const;

string chimeOnHour(void) const;

void tick(void); // time goes up 1 minute

// need to consider the cases: 59 minutes and any hour but 23

// 59 minues and 23 hours

private:

int minutes; // will range between 0 and 59

int hours; // will range between 0 and 23

bool shouldChimeOnHalfHour;

bool shouldChimeOnHour;

};

#endif

*****************************************************

clockPart2.cpp

#include "clockPart2.h"

#include

void Clock::setToCurrentTime()

{

int currentT = time(0); // get time in number of seconds elapsed since January 1, 1970

// time is GMT - Greenwich Mean Time

currentT = currentT % (24 * 60 * 60); // convert to seconds past midnight

currentT = currentT / 60; // convert to minutes

int cHr, cMn;

cHr = currentT / 60; // get current hour

const int GMT_DIFFERENCE = -6; // for Central Standard Time compared to Greenwich Mean Time

cHr = (cHr + GMT_DIFFERENCE + 24) % 24;

cMn = currentT % 60; // get current minute

setHours(cHr);

setMinutes(cMn);

}

// Constructors

Clock::Clock(void)

{

setToCurrentTime(); // will set hours and minutes

setChimeOnHalfHour(false); // default value of false

setChimeOnHour(false);

}

Clock::Clock(int hr, int m, bool chimeHalfHour, bool chimeHour) // Argument Constructor

{

setMinutes(m);

setHours(hr);

setChimeOnHalfHour(false);

setChimeOnHour(false);

}

Clock::Clock(const Clock& right) // Copy Constructor

{

setMinutes(right.getMinutes());

setHours(right.getHours());

setChimeOnHalfHour(right.getChimeOnHalfHour());

setChimeOnHour(right.getChimeOnHour());

}

// Destructor

Clock::~Clock()

{

// no code, no work to be done

}

void Clock::sync(Clock& right) const // parameter will have same time as invoking instance

{ // sync the time, ie both will have the same values

// but do not change the should chime fields

// students write the code

}

void Clock::setHoursAhead(int h) // Assume 1 <= h <= 23

{

hours = hours + h;

if (hours > 23)

{

hours = hours % 23;

setHoursAhead(1);

}

}

void Clock::setHoursBack(int h) // Assume 1 <= h <= 23

{

hours = hours - h;

if (hours < 0)

{

hours = hours + 23;

setHoursBack(1);

}

}

void Clock::setMinutesAhead(int m) // Assume 1 <= m <= 59, leave hours the same

{

setMinutes((getMinutes() + m) % 60); // in case the result is >= 60

}

void Clock::setMinutesBack(int m) // Assume 1 <= m <= 59, leave hours the same

{

setMinutes((getMinutes() - m + 60) % 60); // in case the result is < 0

}

int Clock::differenceInClocks(const Clock& right) const

{

return 0; // Dummy Code in order to compile, students write the code

}

const Clock& Clock::operator=(const Clock& right)

{

hours = right.hours;

minutes = right.minutes;

return *this; // necessary for the code to compile

}

int Clock::getMinutes(void) const

{

return minutes;

}

int Clock::getHours(void) const

{

return hours;

}

bool Clock::getChimeOnHalfHour(void) const

{

return shouldChimeOnHalfHour;

}

void Clock::setMinutes(int m)

{

if (m >= 0 && m <= 59)

minutes = m;

else

minutes = 0;

}

void Clock::setHours(int h)

{

if (h >= 0 && h <= 23)

hours = h;

else

hours = 0;

}

void Clock::setChimeOnHalfHour(bool v)

{

shouldChimeOnHalfHour = v;

}

string Clock::chimeOnHalfHour(void) const

{

if (getChimeOnHalfHour() && getMinutes() == 30)

return "ding";

else

return "";

}

bool Clock::getChimeOnHour(void) const

{

return shouldChimeOnHour;

}

void Clock::setChimeOnHour(bool v)

{

shouldChimeOnHour = v;

}

void Clock::tick(void)

{

if (getMinutes() < 59)

setMinutes(getMinutes() + 1);

else

{

setMinutes(0);

if (getHours() < 23)

setHours(getHours() + 1);

setHours(0);

}

}

string Clock::chimeOnHour(void) const

{

if (this->getChimeOnHour() && this->getMinutes() == 0)

{

string answer = "";

int times;

int work = getHours();

if (work == 0) times = 12;

else if (work <= 12) times = work;

else times = work - 12;

for (int i = 1; i <= times; i++)

answer = answer + " dong";

return answer;

}

else

return "";

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions