Question

Create a class called time that has three data members hours, minutes, and seconds. The class...

Create a class called time that has three data members hours, minutes, and seconds. The class has the following member functions:
- Overloaded Constructors
- Display function to display it, in 11:59:59 format.
- SetTime()
- IsValid(): Check if the time is valid or not
- AddTime(Time t1, Time t2): Add two time values passed as a parameter leaving the result in third time variable.
- Increment(): Tells what will be time after a second
- Decrement(): Tells what was the time a second earlier

In C++ language

Homework Answers

Answer #1

SOURCE CODE:

#include<iostream>

using namespace std;

class Time //class Time

{

private:

int hours;

int minutes; //member variables hours minutes seconds

int seconds;

public:

Time() //default constructer which sets all to 0

{

hours = 0;

minutes = 0;

seconds = 0;

}

Time(int h, int m, int s) //overloaded constructor which accepts hours minutes and seconds

{

hours = h;

minutes = m;

seconds = s;

}

void setTime(int h,int m,int s){ //setTime() methods which accepts hours minutes and seconds

hours = h;

minutes = m;

seconds = s;

}

void display() //display method which displays time in 11:59:59 format

{

if(hours<10) //if hours < 10 then add extra 0

cout<<"0"; //printing 0

cout<<hours<<":"; //printing hours

if(minutes<10) //if minutes <10 add extra 0

cout<<"0"; //printing 0

cout<<minutes<<":"; //printing minutes

if(seconds<10) //if seconds < 10 add extra 0

cout<<"0"; //printing 0

cout<<seconds<<endl; //printing seconds

}

void isValid(){ //isValid() methods which tells the time is valid or not

if(hours<0 || hours>23){

cout<<"Time is Invalid"<<endl;

}

else if(minutes<0 || minutes>59){

cout<<"Time is Invalid"<<endl;

}

else if(seconds<0 || seconds>59){

cout<<"Time is Invalid"<<endl;

}

else{

cout<<"Time is valid"<<endl;

}

}

void Increment(){ //Increment method which increments time by 1 seconds

seconds=seconds+1;

if(seconds>59){

seconds=0;

minutes=minutes+1;

}

if(minutes>59){

minutes=0;

hours=hours+1;

}

if(hours>23){

hours=0;

}

}

void Decrement(){ //Decrement() method which decrement time by 1 second

seconds=seconds-1;

if(seconds<0){

seconds=59;

minutes=minutes-1;

}

if(minutes<0){

minutes=59;

hours=hours-1;

}

if(hours<0){

hours=23;

}

}

Time AddTime(Time t1, Time t2) //AddTime method which adds two times

{

int finalHours = t1.hours + t2.hours;

int finalMinutes = t1.minutes + t2.minutes; //first add two hours minutes and seconds

int finalSeconds = t1.seconds + t2.seconds;

if (finalHours > 23) { //check if hour >23 if so then

finalHours -= 24;

}

if (finalMinutes > 59) { //check if minutes>59 if so then

finalMinutes -= 60;

finalHours += 1;

}

if (finalSeconds > 59) { //check if seconds>59 if so then

finalSeconds -= 60;

finalMinutes += 1;

}

Time t3(finalHours,finalMinutes ,finalSeconds);

return t3;

}

};

int main()

{

//testing code

Time t1(12,40,30);

t1.display();

t1.isValid();

Time t2(21,23,43);

t2.display();

t2.isValid();

Time t3=t1.AddTime(t1, t2);

t3.display();


return 0;

}

SCREENSHOT:

OUTPUT:

Know the answer?
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
Similar Questions
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the...
The program shown below reads in (N) values of time (hours, minutes, and seconds). If the values for hours, minutes and seconds are a legal military time (i.e. 00 00 00 to 23 59 59) the program should display the formatted results (i.e. 12 34 56 should be displayed as 12:34:56). If there's an error for any of the entered values, an exception should be thrown and an error message should be displayed. Note, there are three exception conditions, one...
Create and implement a class called clockType with the following data and methods Data: Hours, minutes,...
Create and implement a class called clockType with the following data and methods Data: Hours, minutes, seconds Methods: Set and get hours Set and get minutes Set and get seconds printTime(…) to display time in the form of hh:mm:ss default and overloading constructor
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
we defined, implemented, and tested a class Time. In this lab, we will continue working with...
we defined, implemented, and tested a class Time. In this lab, we will continue working with the Time class. A. Separate the class definition and class implementation from the client program. Move the class definition into a header file named Time.h and move the class implementation into a cpp file named Time.cpp. Use preprocessor directive to include header files as needed. B. Modify the Time class as follows: 1. Replace the default constructor and overloaded constructor with a constructor with...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...