Question

In: Computer Science

In C++, Implement the following class that represents a clock. Clock - hour: int - minute:...

In C++, Implement the following class that represents a clock.

Clock

- hour: int

- minute: int

- meridiem: string

+ Clock()

+ Clock(hr: int, min: int, mer: string)

+ setTime(hr: int, min: int, mer: string): void

+ setHour(hr: int): void + setMinute(min: int): void

+ setMeridiem(mer: string): void

+ getHour(): int

+ getMinute(): int

+ getMeridiem(): string

+ void tick()

+ string asString()

+ string asStandard()

Implementation Details:

• The default constructor should set the clock to midnight (12:00 am)

• Hour must be in the range 1 to 12

• Minute must be in the range 0 to 59

• Meridiem must be the string “am” or the string “pm”

• The constructor that accepts a time as parameters and all of the setters (mutators) must perform error checking. If an error is detected, print an appropriate error message and stop the program. The exit() function can be used to stop the program.

• tick() increments the minute value and handles any rollover. For example, a clock currently set to 11:59 am would become 12:00 pm after executing tick() .

• asString() returns the current time in a format suitable for printing to the screen (i.e. 1:05 pm). Note the leading zero for values of minutes less than 10.

• asStandard() returns the current time in 24-hour clock format(i.e. 13:05). Both the hour and minute values should be 2 digit numbers (use a leading zero for values less than 10).

Here are the first few lines of code to get started:

#include <iostream>

#include <string>

#include <sstream>

class Clock {

};

Clock::Clock()

{

setTime(12, 0, "am");

}

And here is the main() that was given to test this:

int main() {

Clock c;

cout << "After default constructor: " << endl;

cout << c.asString() << endl;

cout << c.asStandard() << endl;

c.tick();

c.tick();

cout << "After 2 ticks: " << endl;

cout << c.asString() << endl;

cout << c.asStandard() << endl;

for (int i = 0; i < 185; i = i + 1)

c.tick();

cout << "After 185 more ticks: " << endl;

cout << c.asString() << endl;

cout << c.asStandard() << endl;

cout << endl << endl;

// Continue testing constructors and tick()

// Continue testing getters and setters....

return 0;

}

Example Execution

After default constructor:

12:00am 00:00

After 2 ticks:

12:02am

00:02

After 185 more ticks:

3:07am

03:07

After parameter constructor:

11:59am

11:59

After 2 ticks:

12:01pm

12:01

After 185 more ticks:

3:06pm

15:06

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iomanip>

using namespace std;


class Clock {
        private:
        int hour, minute;
        string meridiem;        

        public:
        Clock();
        Clock(int, int, string);
        void setTime(int h, int m, string mer);
        void setHour(int);
        void setMinute(int);
        void setMeridiem(string);
        int getHour();
        int getMinute();
        string getMeridiem();

        void tick();
        string asString();
        string asStandard();

};


Clock::Clock() {
        setTime(12, 0, "am");
}
Clock::Clock(int h, int m, string s) {
        setTime(h, m, s);
}

void Clock::setTime(int h, int m, string s) {

        if(h <= 0 || h > 12) {
                cout << "invalid Hours" << endl;
                exit(1);
        }
        if(m < 0 || m > 59) {
                cout << "invalid Minutes" << endl;
                exit(1);
        }
        if(s != "am" && s != "pm") {
                cout << "invalid meridiem" << endl;
                exit(1);
        }

        this->hour = h;
        this->minute = m;
        this->meridiem = s;
}
void Clock::setHour(int h) {
        setTime(h, minute, meridiem);
}
void Clock::setMinute(int x) {
        setTime(hour, x, meridiem);
}
void Clock::setMeridiem(string x) {
        setTime(hour, minute, x);
}

int Clock::getHour() {
        return hour;
}
int Clock::getMinute() {
        return minute;
}
string Clock::getMeridiem() {
        return meridiem;
}

string Clock::asString() {
        stringstream ss;
        ss << setfill('0') << setw(2) << hour << ":";
        ss << setfill('0') << setw(2) << minute << meridiem;
        return ss.str();
}
string Clock::asStandard() {
        int h = hour;
        if(h == 12) {
                h = 0;
        }
        if(meridiem == "pm") {
                h += 12;
        }
        stringstream ss;
        ss << setfill('0') << setw(2) << h << ":";
        ss << setfill('0') << setw(2) << minute;
        return ss.str();
}

void Clock::tick() {
        minute += 1;
        if(minute == 60) {
                minute = 0;
                // increment hour
                if(hour == 12) {
                        hour = 1;
                } else {
                        hour++;
                }

                if(hour == 12) {
                        meridiem = (meridiem == "am" ? "pm" : "am");
                }
        }
}

int main() {

        Clock c;

        cout << "After default constructor: " << endl;
        cout << c.asString() << endl;
        cout << c.asStandard() << endl;
        c.tick();
        c.tick();

        cout << "After 2 ticks: " << endl;
        cout << c.asString() << endl;
        cout << c.asStandard() << endl;

        for (int i = 0; i < 185; i = i + 1)
                c.tick();

        cout << "After 185 more ticks: " << endl;
        cout << c.asString() << endl;
        cout << c.asStandard() << endl;
        cout << endl << endl;


        // Continue testing constructors and tick()
        Clock c1(11, 59, "am");
        cout << "After parameter constructor: " << endl;
        cout << c1.asString() << endl;
        cout << c1.asStandard() << endl;
        c1.tick();
        c1.tick();

        cout << "After 2 ticks: " << endl;
        cout << c1.asString() << endl;
        cout << c1.asStandard() << endl;

        for (int i = 0; i < 185; i = i + 1)
                c1.tick();

        cout << "After 185 more ticks: " << endl;
        cout << c1.asString() << endl;
        cout << c1.asStandard() << endl;
        cout << endl << endl;


        return 0;

}
**************************************************

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.


Related Solutions

At 3:00 the hour hand and the minute hand of a clock point in directions that...
At 3:00 the hour hand and the minute hand of a clock point in directions that are 90° apart. What is the first time after 3:00 that the angle between the two hands has decreased to 26.0°?
A clock has an hour hand of length 2.4 cm and a minute hand of length...
A clock has an hour hand of length 2.4 cm and a minute hand of length 3.8 cm. (a) Calculate the position and velocity of the hour hand at noon. (b) Calculate the position and velocity of the minute hand at 12:15.
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
class nodeType                    // class used to implement a node { public:         int data;   &n
class nodeType                    // class used to implement a node { public:         int data;                        // data member of node         nodeType * next;        // pointer member of node }; int main() {         int x;         nodeType * head =________ ;                     // initialize head pointer         nodeType * tail = _______ ;                        // initialize tail pointer _______ * p;                                                 // create an auxiliary pointer to a node         for (x = 0; x < 10; ++x)         {                 p =   _________ nodeType; // create a node ___________ = x + 10;                                // store...
Given a clock on the planet 9 with hour, minute, and second hands, where day last 14 hours (hour hand circles twice for a day)
USE MATLABGiven a clock on the planet 9 with hour, minute, and second hands, where day last 14 hours (hour hand circles twice for a day)a) At what time (in hours), the first time after 0th hour, all 3 hands will overlap,b) At what times (in hours) they will overlap between 0th hour and 7th hour?
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
(a) Create a Card class that represents a playing card. It should have an int instance...
(a) Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Include the following methods: A constructor with two arguments for initializing the two instance variables. A copy constructor. A method equals — with one argument — which compares the calling object with another Card and returns true if and only if the corresponding ranks and suits are equal. Make sure your method will not generate...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT