Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that allows the user to enter time using hours and minutes.

ID: 3873780 • Letter: W

Question

Write a C++ program that allows the user to enter time using hours and minutes. (NOTE: Hours are to be between 1 and 12 while minutes are to be between 0 and 59.) Next ask the user how much time is to be added to the original time. Then display the new time. Finally, ask the user how much time to subtract and display the final time. The program needs to have a time class that is able to add and subtract the times using overloaded + and – operators to calculate the new time when the user adds or subtracts time from the original time and get functions to retrieve the hours and the minutes. Here is how the program should work. Ask the user for the hours and for the minutes. Create an object with the hours and minutes passed to it. Then ask the user how many hours to add and how many minutes to add. Create a second object with these new values. Also, create a third object without any values passed to it. Use the overloaded + operator to add the two objects together and place the result in the third object (ie. third = first + second). Next call the get functions to display the hours and the minutes in the third object. Now ask the user how much time to subtract and (after creating a fourth object with the time to be subtracted and a fifth object without any values) use the overloaded – operator to subtract the time from the third object (ie. fifth = third - fourth). Finally, call get functions to display the hours and the minutes in the fifth object. Make sure to have your class be in a separate file from your main program and you must use overloaded + and – operators in your class for adding and subtracting time. Also, turn in a UML diagram. Don't forgot to document your program.

Explanation / Answer


#ifndef myTime_H
#define myTime_H

#include <iostream>
//Class myTime definition
class myTime
{
//Data member to store hours
int hours;
//Data member to store minutes
int minutes;
public:
//Default constructor
myTime()
{
//Assigns zero to hours and seconds
hours = minutes = 0;
}//End of default constructor
//End of parameterized constructor
myTime(int h, int m)
{
hours = h;
minutes = m;
}//End of parameterized constructor
//Function to return hours
int getHours()
{
return hours;
}//End of function
//Function to return minutes
int getMinutes()
{
return minutes;
}//End of function
//Overloads + operator to add two time objects
myTime operator + (myTime sec)
{
//Object to stores result
myTime res;
//Adds minutes
res.minutes = minutes + sec.minutes;
//Checks if the result minute is greater than 60
if(res.minutes > 60)
{
//Add one to the hour
sec.hours += 1;
//Subtracts 60 from the minutes
res.minutes -= 60;
}//End of if
//Adds the hours
res.hours = hours + sec.hours;
//Checks if the hour is greater than 12
if(res.hours > 12)
//Subtract 12 from result hours
res.hours = res.hours - 12;
//Returns the result object
return res;
}//End of function
//Overloads - operator to subtract two time objects
myTime operator - (myTime sec)
{
//Object to stores result
myTime res;
//Subtracts minutes
res.minutes = minutes - sec.minutes;
//Checks if the result minute is negative
if(res.minutes < 0)
{
//Subtract one from the hour
sec.hours += 1;
//Subtracts first object minutes from the second object minute and then subtract it from 60
res.minutes = (60 - (sec.minutes - minutes));
}//End of if
//Subtracts the hours
res.hours = hours - sec.hours;
//Checks if hours is negative
if(res.hours < 0)
//Subtract second hour value from first hour value then subtract it from 12
res.hours = 12 - (sec.hours - hours);
//Returns the result object
return res;
}//End of function
};//End of class
#endif

//File Name: myTime.cpp

#include<iostream>
#include "myTime.h"
using namespace std;
//Validates the hour and minute
void acceptTime(int &h, int &m)
{
//Loops till valid hour entered
do
{
//Accept hours
cout<<" Enter hours (Hours are to be between 1 and 12): ";
cin>>h;
//If hour is greater than 12 or less than one display error message
if(h > 12 || h < 1)
cout<<" Invalid hour. Try again";
else
break;
}while(1);//End of do - while loop

//Loops till valid minute entered
do
{
//Accept minutes
cout<<" Enter minutes (Minutes are to be between 0 and 59): ";
cin>>m;
//Checks if minute is greater than 59 or negative display error message
if(m > 59 || m < 0)
cout<<" Invalid Minutes. Try again";
else
break;
}while(1);//End of do - while
}//End of function


//Main function definition
int main()
{
//To store hours and minutes
int h, m;
//Calls the function to accept hour and minute
acceptTime(h, m);
myTime first(h,m);

//Accept hours and minutes to add
cout<<" Enter hours and minutes to add";
//Calls the function to accept hour and minute
acceptTime(h, m);
myTime second(h,m);
//Adds first and second time
myTime third = first + second;
//Displays result time
cout<<" Hours: "<<third.getHours()<<" Minutes: "<<third.getMinutes();

cout<<" Enter hours and minutes to subtract";
//Calls the function to accept hour and minute
acceptTime(h, m);
myTime fourth(h,m);
//Subtracts third and fourth time
myTime fifth = third - fourth;
//Displays result time
cout<<" Hours: "<<fifth.getHours()<<" Minutes: "<<fifth.getMinutes();
}

Sample Run:


Enter hours (Hours are to be between 1 and 12): 14

Invalid hour. Try again
Enter hours (Hours are to be between 1 and 12): 12

Enter minutes (Minutes are to be between 0 and 59): 40

Enter hours and minutes to add
Enter hours (Hours are to be between 1 and 12): 3

Enter minutes (Minutes are to be between 0 and 59): 30

Hours: 4 Minutes: 10
Enter hours and minutes to subtract
Enter hours (Hours are to be between 1 and 12): 6

Enter minutes (Minutes are to be between 0 and 59): 05

Hours: 10 Minutes: 5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote