Question

In: Computer Science

Q.Write a program that converts between two way of measuring time: 12-hour time (civilian time) and...

Q.Write a program that converts between two way of measuring time: 12-hour time (civilian time) and 24-hour time (military time). We will assume in the context there is no need for seconds, so time12 use only hours (from 1 to 12), minutes, and an "a.m." or "p.m." designation. Our time24 class, which uses hours (from 00 to 23), minutes and seconds.
12-Hours Time 24-Hours Time
12:00 a.m. (midnight) 00:00
12:01 a.m. 00:01
12:00 p.m. 12:00
6:00 p.m. 18:00
11:59 p.m.    23.59

Given the following classes:

class time12
{
private:
bool pm; //true = pm, false = am
int hrs; //1 to 12
int mins; //0 to 59
public: //no-arg constructor
time12() : pm(true), hrs(0), mins(0)
{ }
//3-arg constructor
time12(bool ap, int h, int m) : pm(ap), hrs(h), mins(m)
{ }
void display() const //format: 11:59 p.m.
{
cout << hrs << ':';
if(mins < 10)
cout << '0'; //extra zero for "01"
cout << mins << ' ';
string am_pm = pm ? "p.m." : "a.m.";
cout << am_pm;
}
};

class Time24
{
private:
int hours; //0 to 23
int minutes; //0 to 59
int seconds; //0 to 59
public: //no-arg constructor
time24() : hours(0), minutes(0), seconds(0)
{ }
time24(int h, int m, int s) : //3-arg constructor
hours(h), minutes(m), seconds(s)
{ }
void display() const //format: 23:15:01
{
if(hours < 10) cout << '0';
cout << hours << ':';
if(minutes < 10) cout << '0';
cout << minutes << ':';
if(seconds < 10) cout << '0';
cout << seconds;
}

};

Modify the class Time24 so that the main function below runs successfully.

int main()
{
int h, m, s;

while(true)
{ //get 24-hr time from user
cout << "Enter 24-hour time: \n";
cout << " Hours (0 to 23): "; cin >> h;
if(h > 23) //quit if hours > 23
return(1);
cout << " Minutes: "; cin >> m;
cout << " Seconds: "; cin >> s;

time24 t24(h, m, s); //make a time24
cout << "You entered: "; //display the time24
t24.display();

time12 t12 = t24; //convert time24 to time12

cout << "\n12-hour time: "; //display equivalent time12
t12.display();
cout << "\n\n";
}
return 0;
}

Runtime output

Enter 24-hour time:
Hours (0 to 23): 12
Minutes: 00
Seconds: 00
You entered: 12:00:00
12-hour time: 12:00 p.m.

Enter 24-hour time:
Hours (0 to 23): 23
Minutes: 59
Seconds: 00
You entered: 23:59:00
12-hour time: 11:59 p.m.

Enter 24-hour time:
Hours (0 to 23): 1
Minutes: 00
Seconds: 00
You entered: 01:00:00
12-hour time: 1:00 a.m.

Enter 24-hour time:
Hours (0 to 23): 24

C++ please

Solutions

Expert Solution

Program

#include <iostream>
using namespace std;

class time24
{
private:
int hours; //0 to 23
int minutes; //0 to 59
int seconds; //0 to 59
public: //no-arg constructor
time24() : hours(0), minutes(0), seconds(0){ }
time24(int h, int m, int s) : //3-arg constructor
hours(h), minutes(m), seconds(s){ }
int getHours(){return hours;}
int getMinutes(){return minutes;}

void display() const //format: 23:15:01
{
if(hours < 10) cout << '0';
cout << hours << ':';
if(minutes < 10) cout << '0';
cout << minutes << ':';
if(seconds < 10) cout << '0';
cout << seconds;
}
};


class time12
{
private:
bool pm; //true = pm, false = am
int hrs; //1 to 12
int mins; //0 to 59
public: //no-arg constructor
time12() : pm(true), hrs(0), mins(0){ }
//3-arg constructor
time12(bool ap, int h, int m) : pm(ap), hrs(h), mins(m){ }
//convert from time24 to time12
time12(time24 &t)
{
if(t.getHours()>=12)
pm=true;
else
pm = false;

if(t.getHours()>12)
hrs = t.getHours()%12;
else
hrs = t.getHours();
mins = t.getMinutes();
}
void display() const //format: 11:59 p.m.
{
cout << hrs << ':';
if(mins < 10)
cout << '0'; //extra zero for "01"
cout << mins << ' ';
string am_pm = pm ? "p.m." : "a.m.";
cout << am_pm;
}
};


//main function
int main()
{
int h, m, s;

while(true)
{
//get 24-hr time from user
cout << "Enter 24-hour time: \n";
cout << " Hours (0 to 23): "; cin >> h;
if(h > 23) //quit if hours > 23
return(1);
cout << " Minutes: "; cin >> m;
cout << " Seconds: "; cin >> s;

time24 t24(h, m, s); //make a time24
cout << "You entered: "; //display the time24
t24.display();

time12 t12 = t24; //convert time24 to time12

cout << "\n12-hour time: "; //display equivalent time12
t12.display();
cout << "\n\n";
}
return 0;
}

Output:

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you.
Thank you.


Related Solutions

In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume...
In JAVA Write a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter the time as a 4-digit number with no colon. Define an exception class called InvalidTimeFormatException. If the user enters an invalid time lime 1065 or 2515, the program should throw and handle an InvalidTimeFormatException. NOTE: Assume the user will enter the time as a 4-digit number with no colon. SAMPLE OUTPUT: Enter time in 24-hour notation: 1614 That is the...
*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example,...
*** In C++ Write a program that converts from 24-hour notation to 12-hour notation. For example, it should convert 14:25 to 2:25 P.M. The input is given as two integers. This is what I have so far. The program runs for correct input values, but I cannot figure out how to accommodate for hours > 23 and minutes > 59, or negative values. My code is below: // Writing a program that converts from 24-hour notation to 12-hour notation.// #include...
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation; a function to convert the time from 12-hour notation to 24-hour notation; a function to display the choices; function(s) to get the input;...
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa....
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following functions: a function to convert the time from 24-hour notation to 12-hour notation; a function to convert the time from 12-hour notation to 24-hour notation; a function to display the choices; function(s) to get the input;...
Design a program that will receive a valid time in the 12-hour format (e.g. 11:05 PM)...
Design a program that will receive a valid time in the 12-hour format (e.g. 11:05 PM) and convert it to its equivalent 24-hour format (e.g. 2305). Assume 0000 in 24-hour format is 12:00AM in 12-hour format. The program should continue accepting inputs until a sentinel value of 99:99AM is entered. Ex: If the input is: 09:04AM 10:45AM 11:23AM 99:99AM IN Java please, Thanks the output is: 0904 1045 1123
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts...
In Programming Challenge 12 of Chapter 3, you were asked to write a program that converts a Celsius temperature to Fahrenheit. Modify that program so it uses a loop to display a table of the Celsius temperatures 0–20, and their Fahrenheit equivalents. Display table on screen and it a .txt file. c++
Measuring the distance between two trees, you measure that there are 75.5 steps between the two...
Measuring the distance between two trees, you measure that there are 75.5 steps between the two trees. Give an estimate of the unit of measurement of one step in meters and give an estimate of what you think its uncertainty is. Give the distance that separates the trees in meters as well as feet/inches and round to the correct amount of significant digits. What would you say are the uncertainties for the these two distances
Write a JAVA program called Convertor that has two methods. The first one converts an input...
Write a JAVA program called Convertor that has two methods. The first one converts an input binary into its equivalent decimal number. The second method converts a decimal number into its equivalent binary.​ binary2Decimal, decimal2Binary are the names for those methods.​ binary2Decimal receives a binary number as a string and returns a decimal number as an integer. And vice versa for the decimal2Binary method.​
At a three leg (T) intersection, the two-way peak hour volume on the minor road is...
At a three leg (T) intersection, the two-way peak hour volume on the minor road is 450 veh/hr and that on the major road is 650 veh/hr. As a Traffic Engineer, what type of intersection control will you propose? What is the basis of your proposal?
Two different advertisements were released by a company. They are measuring the time people stay on...
Two different advertisements were released by a company. They are measuring the time people stay on the commercial before switching channels. Below is the data found from the sample: Advertisement 1: # of Viewers: 32 Average time: 25.8 seconds St.Dev: 1.56 Advertisement 2: # of viewers: 27 Average time: 26.2 seconds St.Dev: 1.93 Is there statistically significant evidence to show that advertisement 2 performs significantly better? Enter in the p-value you found from this test.Round to four decimal places.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT