Question

3. write a c++ program: Design and implement a class called Clock that describes the time...

3.
write a c++ program:

Design and implement a class called Clock that describes the time of a clock:
a) Include in the class 3 constructors with one, two, and three parameters to set the hours, the minutes, and the seconds, respectively.
b) Include also a default constructor that sets the member variables of the class to 00:00:00.
c) Write a member function to increment the time by a given amount, and second member
function to reset the clock. The reset function set the time back to 00:00:00.
d) Include a friend function to display the time in both in AM/PM format as well as in military
format (i.e. in military format, 4:00pm is written as 16:00).
e) Write a driver (i.e. a main) that tests your class and uses all the functions you have created.
f) Separate the ADT from its implementation and write them in 2 separate files. The main
must be in a third file. Be sure all the methods of the ADT are documented (i.e. they have the comments and Precondition and Postcondition as shown in all the examples of your book). If you don’t remember how to separate your program in multiple files, watch again the video of Chapter 10 “Separate Interface and Implementation – Part one and Part Two”.
Call your main program ex13_3.cpp.
Cong
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// clock.h

#ifndef CLOCK_H_
#define CLOCK_H_

#include <iostream>
using namespace std;

class Clock
{
private: // private data members
   int hours;
   int minutes;
   int seconds;
public:
   // constructors
   Clock(int seconds);
   Clock(int minutes, int seconds);
   Clock(int hours, int minutes, int seconds);
   Clock(); //default constructor
   void incrementTime(int secondsAmount); // increment the time by given number of seconds
   void reset(); // reset the time back to 00:00:00
   friend ostream& operator<<(ostream &out, const Clock &clock); // output the time in AM/PM and military format
};


#endif

//end of clock.h

// clock.cpp

#include "clock.h"
#include <iomanip>

Clock ::Clock()
{
   hours=0;
   minutes=0;
   seconds=0;
}

Clock ::Clock(int seconds)
{
   this->seconds = seconds;
   minutes=0;
   hours=0;
   if(this->seconds > 59)
   {
       minutes = this->seconds/60;
       this->seconds = this->seconds%60;
   }

   if(minutes > 59)
   {
       hours = minutes/60;
       minutes = minutes%60;
   }

   if(hours > 23)
   {
       hours = hours%24;
   }
}

Clock ::Clock(int minutes, int seconds)
{
   this->seconds = seconds;
   this->minutes=minutes;
   hours=0;
   if(this->seconds > 59)
   {
       this->minutes += this->seconds/60;
       this->seconds = this->seconds%60;
   }

   if(this->minutes > 59)
   {
       hours = this->minutes/60;
       this->minutes = this->minutes%60;
   }

   if(hours > 23)
   {
       hours = hours%24;
   }
}

Clock::Clock(int hours, int minutes, int seconds)
{
   this->seconds = seconds;
   this->minutes = minutes;
   this->hours = hours;

   if(this->seconds > 59)
   {
       this->minutes += this->seconds/60;
       this->seconds = this->seconds%60;
   }

   if(this->minutes > 59)
   {
       this->hours += this->minutes/60;
       this->minutes = this->minutes%60;
   }

   if(this->hours > 23)
   {
       this->hours = this->hours%24;
   }
}

void Clock::incrementTime(int secondsAmount)
{
   this->seconds += secondsAmount;
   if(this->seconds > 59)
   {
       this->minutes += this->seconds/60;
       this->seconds = this->seconds%60;
   }

   if(this->minutes > 59)
   {
       this->hours += this->minutes/60;
       this->minutes = this->minutes%60;
   }

   if(this->hours > 23)
   {
       this->hours = this->hours%24;
   }
}

void Clock::reset()
{
   hours=0;
   minutes=0;
   seconds =0;
}

ostream& operator<<(ostream &out, const Clock &clock)
{
   out<<" Time in AM/PM format : ";
   if(clock.hours >=0 && clock.hours <=11)
   {
       out<<setfill('0') << setw(2)<<clock.hours<<":"<<setfill('0') << setw(2)<<clock.minutes<<":"<<setfill('0') << setw(2)<<clock.seconds<<" AM"<<endl;
   }else if(clock.hours == 12)
   {
       out<<setfill('0') << setw(2)<<clock.hours<<":"<<setfill('0') << setw(2)<<clock.minutes<<":"<<setfill('0') << setw(2)<<clock.seconds<<" PM"<<endl;
   }
   else
   {
       int hour = clock.hours-12;
       out<<setfill('0') << setw(2)<<hour<<":"<<setfill('0') << setw(2)<<clock.minutes<<":"<<setfill('0') << setw(2)<<clock.seconds<<" PM"<<endl;
   }

   out<<" Time in military format : "<<setfill('0') << setw(2)<<clock.hours<<":"<<setfill('0') << setw(2)<<clock.minutes<<":"<<setfill('0') << setw(2)<<clock.seconds<<endl;

   return out;
}


//end of clock.cpp

// main.cpp : C++ program to create and test class ClockWork

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

int main() {

   Clock clock(17,35,59);
   cout<<clock<<endl;
   clock.incrementTime(121);
   cout<<clock<<endl;
   clock.reset();
   cout<<clock<<endl;
   return 0;
}

//end of main.cpp

Output:

Add a comment
Know the answer?
Add Answer to:
3. write a c++ program: Design and implement a class called Clock that describes 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
  • 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...

  • IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a tick member function that increments the time stored in a Time object by one second. Write a program that tests the tick me...

    IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a tick member function that increments the time stored in a Time object by one second. Write a program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member function works correctly. Be sure to test the following cases: a) Incrementing into the next minute. b) Incrementing...

  • IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a...

    IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a tick member function that increments the time stored in a Time object by one second. Write a program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member function works correctly. Be sure to test the following cases: a) Incrementing into the next minute. b) Incrementing...

  • Can someone solve it with C plz Write a program that inputs a time from the...

    Can someone solve it with C plz Write a program that inputs a time from the console. The time should be in the format “HH:MM AM” or “HH:MM PM”. Hours may be one or two digits, for example, “1:10 AM” or “11:30 PM”. Your program should include a function that takes a string parameter containing the time. This function should convert the time into a four digit military time based on a 24 hour clock. For example, “1:10 AM” would...

  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( );...

    Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • The class declaration (interface) and the function definitions (implementation) must be in separate files - the...

    The class declaration (interface) and the function definitions (implementation) must be in separate files - the interface or "header" file has a .hpp extension and the implementation has a .cpp extension. As usual, all data members should be private. Write a class called BankAccount that has: a string data member called customerName, a string data member called customerID, and a double data member called customerBalance a constructor that takes two strings and a double (name, ID, balance) and uses them...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

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