Question

Write in C++ please In Chapter 10, the class clockType was designed to implement the time...

Write in C++ please

In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write a test program to test your class.

Files:

clocktype.h

//clockType.h, the specification file for the class clockType
#ifndef H_ClockType
#define H_ClockType

class clockType
{
public:
void setTime(int hours, int minutes, int seconds);
//Function to set the time.
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds
// The function checks whether the values of
// hours, minutes, and seconds are valid. If a
// value is invalid, the default value 0 is
// assigned.

void getTime(int& hours, int& minutes, int& seconds) const;
//Function to return the time.
//Postcondition: hours = hr; minutes = min;
// seconds = sec

void printTime() const;
//Function to print the time.
//Postcondition: The time is printed in the form
// hh:mm:ss.

void incrementSeconds();
//Function to increment the time by one second.
//Postcondition: The time is incremented by one
// second.
// If the before-increment time is 23:59:59, the
// time is reset to 00:00:00.

void incrementMinutes();
//Function to increment the time by one minute.
//Postcondition: The time is incremented by one
// minute.
// If the before-increment time is 23:59:53,
// the time is reset to 00:00:53.

void incrementHours();
//Function to increment the time by one hour.
//Postcondition: The time is incremented by one
// hour.
// If the before-increment time is 23:45:53, the
// time is reset to 00:45:53.

bool equalTime(const clockType& otherClock) const;
//Function to compare the two times.
//Postcondition: Returns true if this time is
// equal to otherClock; otherwise,
// returns false.

clockType(int hours, int minutes, int seconds);
//constructor with parameters
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds
// The constructor checks whether the values of
// hours, minutes, and seconds are valid. If a
// value is invalid, the default value 0 is
// assigned.

clockType();
//default constructor with parameters
//The time is set to 00:00:00.
//Postcondition: hr = 0; min = 0; sec = 0

private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};

#endif

clockTypeImp.cpp

//Implementation File for the class clockType

#include
#include "clockType.h"
  
using namespace std;

void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

void clockType::getTime(int& hours, int& minutes,
int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}

void clockType::incrementHours()
{
hr++;
if (hr > 23)
hr = 0;
}

void clockType::incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours();
}
}

void clockType::incrementSeconds()
{
sec++;

if (sec > 59)
{
sec = 0;
incrementMinutes();
}
}

void clockType::printTime() const
{
if (hr < 10)
cout << "0";
cout << hr << ":";

if (min < 10)
cout << "0";
cout << min << ":";

if (sec < 10)
cout << "0";
cout << sec;
}

bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}

clockType::clockType(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

clockType::clockType() //default constructor
{
hr = 0;
min = 0;
sec = 0;
}

extClockType.h

#ifndef H_extClockType

#define H_extClockType

#include

#include "clockType.h"

using namespace std;

class extClockType: public clockType

{

public:

extClockType();

extClockType(int, int, int, string);

void setTime(int hours, int minutes, int seconds, string zone);

string printTimezone();

string getTimezone();

private:

string zone;

};

#endif

extClockTypeImp.cpp

#include

#include "clockType.h"

#include "extClockType.h"

using namespace std;

extClockType::extClockType(): clockType()

{

zone = "na";

}

extClockType::extClockType(int hours, int minutes, int seconds, string time_zone)

{

clockType::setTime(hours, minutes, seconds);

zone = time_zone;

}

void extClockType::setTime(int hours, int minutes, int seconds, string zone)

{

clockType::setTime(hours, minutes, seconds);

}

string extClockType::getTimezone()

{

return zone;

}

string extClockType::printTimezone()

{

clockType::printTime();

cout << " " << zone << endl;

return 0;

}

main.cpp

//Program that uses the class extClockType
#include

#include

#include

#include "clockType.h"

#include "extClockType.h"

using namespace std;

int main()
{
extClockType time1(5, 10, 34, "CST");
extClockType time2;

cout << "Time 1: ";
time1.printTime();
cout << endl;

time2.setTime(12, 45, 59, "PST");

cout << "Time 2: ";
time2.printTime();
cout << endl;

time2.incrementSeconds();

cout << "After incrementing time2 by one second, Time 2: ";
time2.printTime();
cout << endl;

return 0;
}//end main

This provides an output, but program is graded through a system and gives me this output:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from Cyl
[ RUN      ] Cyl.1
/root/sandboxf03e3a1a/nt-test-68af047a.cpp:26: Failure
Value of: "Time 1: 05:10:34 CST\nTime 2: 12:45:59 PST\nAfter incrementing time2 by one second, Time 2: 12:46:00 PST"
Expected: output
Which is: "Time 1: 05:10:34\nTime 2: 12:45:59\nAfter incrementing time2 by one second, Time 2: 12:46:00"
[  FAILED  ] Cyl.1 (1 ms)
[----------] 1 test from Cyl (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] Cyl.1

 1 FAILED TEST

Test Content:

using namespace std;

TEST(Cyl, 1) {
    extClockType time1(5, 10, 34, "CST");
    extClockType time2;  
    testing::internal::CaptureStdout();
    cout << "Time 1: ";
    time1.printTime();
    cout << endl;

    time2.setTime(12, 45, 59, "PST");

    cout << "Time 2: ";
    time2.printTime();
    cout << endl;

    time2.incrementSeconds();

    cout << "After incrementing time2 by one second, Time 2: ";
    time2.printTime();
    std::string output = testing::internal::GetCapturedStdout();

    ASSERT_EQ(output, "Time 1: 05:10:34 CST\nTime 2: 12:45:59 PST\nAfter incrementing time2 by one second, Time 2: 12:46:00 PST");
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.cpp clockType.h 56 if (sec > 59) { sec = 0; incrementMinutes(); clang version 7.0.0-3-ubuntuo. 18.04.1 (tags/RELEASE_700

#######################################
         clockType.h
#######################################
//clockType.h, the specification file for the class clockType
#ifndef H_ClockType
#define H_ClockType

class clockType
{
public:
void setTime(int hours, int minutes, int seconds);
//Function to set the time.
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds
// The function checks whether the values of
// hours, minutes, and seconds are valid. If a
// value is invalid, the default value 0 is
// assigned.

void getTime(int& hours, int& minutes, int& seconds) const;
//Function to return the time.
//Postcondition: hours = hr; minutes = min;
// seconds = sec

void printTime() const;
//Function to print the time.
//Postcondition: The time is printed in the form
// hh:mm:ss.

void incrementSeconds();
//Function to increment the time by one second.
//Postcondition: The time is incremented by one
// second.
// If the before-increment time is 23:59:59, the
// time is reset to 00:00:00.

void incrementMinutes();
//Function to increment the time by one minute.
//Postcondition: The time is incremented by one
// minute.
// If the before-increment time is 23:59:53,
// the time is reset to 00:00:53.

void incrementHours();
//Function to increment the time by one hour.
//Postcondition: The time is incremented by one
// hour.
// If the before-increment time is 23:45:53, the
// time is reset to 00:45:53.

bool equalTime(const clockType& otherClock) const;
//Function to compare the two times.
//Postcondition: Returns true if this time is
// equal to otherClock; otherwise,
// returns false.

clockType(int hours, int minutes, int seconds);
//constructor with parameters
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds
// The constructor checks whether the values of
// hours, minutes, and seconds are valid. If a
// value is invalid, the default value 0 is
// assigned.

clockType();
//default constructor with parameters
//The time is set to 00:00:00.
//Postcondition: hr = 0; min = 0; sec = 0

private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};

#endif



#######################################
    clockTypeImp.cpp
#######################################
//Implementation File for the class clockType

#include <iostream>
#include "clockType.h"
  
using namespace std;

void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

void clockType::getTime(int& hours, int& minutes,
int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}

void clockType::incrementHours()
{
hr++;
if (hr > 23)
hr = 0;
}

void clockType::incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours();
}
}

void clockType::incrementSeconds()
{
sec++;

if (sec > 59)
{
sec = 0;
incrementMinutes();
}
}

void clockType::printTime() const
{
if (hr < 10)
cout << "0";
cout << hr << ":";

if (min < 10)
cout << "0";
cout << min << ":";

if (sec < 10)
cout << "0";
cout << sec;
}

bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}

clockType::clockType(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

clockType::clockType() //default constructor
{
hr = 0;
min = 0;
sec = 0;
}



#######################################
      extClockType.h
#######################################
#ifndef H_extClockType

#define H_extClockType

#include<iostream>

#include "clockType.h"

using namespace std;

class extClockType: public clockType

{

public:

extClockType();

extClockType(int, int, int, string);

void setTime(int hours, int minutes, int seconds, string zone);

string printTimezone();

string getTimezone();

void printTime() const;

private:

string zone;

};

#endif



#######################################
 extClockTypeImp.cpp
#######################################
#include<iostream>

#include "clockType.h"

#include "extClockType.h"

using namespace std;

extClockType::extClockType(): clockType()

{

zone = "na";

}

extClockType::extClockType(int hours, int minutes, int seconds, string time_zone)

{

clockType::setTime(hours, minutes, seconds);

zone = time_zone;

}

void extClockType::setTime(int hours, int minutes, int seconds, string time_zone)

{

clockType::setTime(hours, minutes, seconds);
zone = time_zone;

}

string extClockType::getTimezone()

{

return zone;

}

string extClockType::printTimezone()

{

clockType::printTime();

cout << " " << zone << endl;

return 0;

}

void extClockType::printTime() const {
        
clockType::printTime();
cout << " " << zone;

}



#######################################
            main.cpp
#######################################
//Program that uses the class extClockType
#include<iostream>
#include "clockType.h"

#include "extClockType.h"

using namespace std;

int main()
{
extClockType time1(5, 10, 34, "CST");
extClockType time2;

cout << "Time 1: ";
time1.printTime();
cout << endl;

time2.setTime(12, 45, 59, "PST");

cout << "Time 2: ";
time2.printTime();
cout << endl;

time2.incrementSeconds();

cout << "After incrementing time2 by one second, Time 2: ";
time2.printTime();
cout << endl;

return 0;
}//end main



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

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Write in C++ please In Chapter 10, the class clockType was designed to implement the time...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [Java] We have learned the class Clock, which was designed to implement the time of day...

    [Java] We have learned the class Clock, which was designed to implement the time of day in a program. Certain application in addition to hours, minutes, and seconds might require you to store the time zone. Please do the following: Derive the class ExtClock from the class Clock by adding a data member to store the time zone. Add necessary methods and constructors to make the class functional. Also write the definitions of the methods and constructors. Write a test...

  • Need help with this java code supposed to be a military time clock, but I need...

    Need help with this java code supposed to be a military time clock, but I need help creating the driver to test and run the clock. I also need help making the clock dynamic. public class Clock { private int hr; //store hours private int min; //store minutes private int sec; //store seconds public Clock () { setTime (0, 0, 0); } public Clock (int hours, intminutes, int seconds) { setTime (hours, minutes, seconds); } public void setTime (int hours,int...

  • I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the...

    I need help displaying two zeroes in hours:minutes:seconds. In Java. I need some help for the time to display correctly. I want it to display double zeroes. 06:00:00 and 12:00:00. public class Clock { String name; static int uid=100; int id; int hr; int min; int sec; public Clock() {   this.name="Default";   this.id=uid;   this.hr=00; this.min=00; this.sec=00; uid++; } public Clock(String name, int hr, int min, int sec) { if (hr<=24 && min <=60 && sec <=60) {   this.hr = hr; this.min...

  • HI C PROGRAMMING COULD YOU,, Rewrite the program to have it display the time on the...

    HI C PROGRAMMING COULD YOU,, Rewrite the program to have it display the time on the second line of the display, using the HH:MM:SS format. Use the 24-hour format for the hours, in other words, have the time go from 00:00:00 to 23:59:59. #include<LiquidCrystal.h> LiquidCrystal LcdDriver(11, 9, 5, 6, 7, 8); int minutes = 27; //These global integers keep the value of the clock int sec = 10; int hr = 10; const long interval = 1000; //This interval is...

  • C++ Please! Given this definition of a Time class: class Time { private: int hour, min,...

    C++ Please! Given this definition of a Time class: class Time { private: int hour, min, sec; public: Time() { hour = 0; min = 0; sec = 0; } Time(int h, int m, int s) {      hour = h; min = m; sec = s; } int getHour() const {     return hour; } int getMin() const {     return minute; } int getSec() const {     return sec; } }; Derive a class MilTime from the Time...

  • D Question 6 4 pts Figure 1: clock Type |-hr: int -min: int -sec; int setTime...

    D Question 6 4 pts Figure 1: clock Type |-hr: int -min: int -sec; int setTime (int, int, int): void get Time (inte, int&, int&) const: void printTime() const: void increment seconds(): int +incrementMinutes(): int +increment Hours(): int -equalTime (const clockType.) const: bool Consider the UML class diagram shown in the accompanying figure. According to the UML class diagram, how many private members are in the class? none zero two three D Question 4 4 pts Consider the following class...

  • This is a Java programming assignment and I want help with the Time class. See below...

    This is a Java programming assignment and I want help with the Time class. See below for assignment details: For this question you must write a java class called Time and a client class called TimeClient. The partial Time class is given below. (For this assignment, you will have to submit 2 .java files: one for the Time class and the other one for the TimeClient class and 2 .class files associated with these .java files. So in total you...

  • I need help implementing class string functions, any help would be appreciated, also any comments throughout...

    I need help implementing class string functions, any help would be appreciated, also any comments throughout would also be extremely helpful. Time.cpp file - #include "Time.h" #include <new> #include <string> #include <iostream> // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for the hours, // an integer variable for the minutes, and a char variable for...

  • I have a program that needs comments added to it: #include <iostream> #include <stdio.h> #include <conio.h>...

    I have a program that needs comments added to it: #include <iostream> #include <stdio.h> #include <conio.h> #include <windows.h> using namespace std; int main() { int m, s,h; cout << "A COUNTDOWN TIMER " << endl; cout << "enter time in hours here" << endl; cin >> h; cout << "enter time in minutes here " << endl; cin >> m; cout << "enter time in seconds here" << endl; cin >> s; cout << "Press any key to start" <<...

  • Please help with Java programming! This code is to implement a Clock class. Use my beginning...

    Please help with Java programming! This code is to implement a Clock class. Use my beginning code below to test your implementation. public class Clock { // Declare your fields here /** * The constructor builds a Clock object and sets time to 00:00 * and the alarm to 00:00, also. * */ public Clock() { setHr(0); setMin(0); setAlarmHr(0); setAlarmMin(0); } /** * setHr() will validate and set the value of the hr field * for the clock. * *...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT