In: Computer Science
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
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.