Time Format: Design a class called MilTime that is derived from the Time class.
ID: 3757853 • Letter: T
Question
Time Format:
Design a class called MilTime that is derived from the Time class. The MilTime class should convert time in military (24-hour) format to the standard time format used by the Time class. The class should have the following member variables: • milHours: Contains the hour in 24-hour format. For example, 1:00 pm would be stored as 1300 hours, and 4:30 pm would be stored as 1630 hours. • milSeconds: Contains the seconds in standard format. The class should have the following member functions:
• Constructor: The constructor should accept arguments for the hour and seconds, in military format. The time should then be converted to standard time and stored in the hours , min , and sec variables of the Time class.
• setTime: Accepts arguments to be stored in the milHours and milSeconds variables. The time should then be converted to standard time and stored in the hours, min, and sec variables of the Time class. • getHour: Returns the hour in military format.
• getStandHr: Returns the hour in standard format.
•
Demonstrate the class in a program that asks the user to enter the time in military format. The program should then display the time in both military and standard format. Be sure to comment each line of code.
Input Validation: The MilTime class should not accept hours greater than 2359, or less than 0. It should not accept seconds greater than 59 or less than 0.
Time Clock:
Design a class named TimeClock. The class should be derived from the MilTime class you designed above. The class should allow the programmer to pass two times to it: starting time and ending time. The class should have a member function that returns the amount of time elapsed between the two times. For example, if the starting time is 900 hours (9:00 am), and the ending time is 1300 hours (1:00 pm), the elapsed time is 4 hours. Be sure to comment each line of code.
Input Validation: The class should not accept hours greater than 2359 or less than 0.
Contents of Time.h 1
// Specification file for the Time class
2 #ifndef TIME_H
3 #define TIME_H
4
5 class Time
6 {
7 protected:
8 int hour;
9 int min;
10 int sec;
11 public:
12 // Default constructor
13 Time()
14 { hour = 0; min = 0; sec = 0; }
15
16 // Constructor
17 Time(int h, int m, int s)
18 { hour = h; min = m; sec = s; }
19
20 // Accessor functions
21 int getHour() const
22 { return hour; }
23
24 int getMin() const
25 { return min; }
26
27 int getSec() const
28 { return sec; }
29 };
30 #endif
Explanation / Answer
Program:
// Include the required head files
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
using namespace std;
class Time
{
protected:
int hour;
int min;
int sec;
public:
// Default constructor
Time(){ hour = 0; min = 0; sec = 0; }
// Constructor
Time(int h, int m, int s)
{
hour = h; min = m; sec = s;
}
// Accessor functions
int getHour() const
{ return hour; }
// Method to get the minutes
int getMin() const
{ return min; }
// Method to get the second
int getSec() const
{ return sec; }
};
// Class MilTime
class MilTime:public Time
{
//Declare the required variables
protected:
int milHrs;
int milSec;
bool blpm;
public:
// Constructor
MilTime()
{
milHrs = 0;
milSec = 0;
blpm = false;
}
// Constructor
MilTime(int milho, int misc)
{
blpm = false;
setTime(milho, misc);
}
// Constructor
MilTime(long seconds)
{
hour = seconds/3600;
int rem = seconds%3600;
min = rem/60;
rem = rem%60; //take out minutes
sec = rem;
blpm = false;
setTime(((hour * 100) + min), sec);
}
// Method to set the time
void setTime(int milho, int misc)
{
milHrs = milho;
milSec = misc;
hour = (milho/100);
if(hour > 12)
{ hour = hour-12;
blpm = true;
}
min = milho%100;
sec = misc;
}
// Method to get the military hour
int getHour()
{return milHrs;}
// Method to get the standard hour
int getStandHr()
{return hour;}
//VMethod to verify the time
static MilTime getTime(string aString)
{
MilTime myTime(0,0);
int h, s;
bool flag = false;
while(flag == false)
{
cout << aString;
cin >> h;
if(h < 0 || h > 2359)
{
flag = false;
cout << "the time is wrong, please try it again. ";
}
else
flag = true;
}
flag = false;
while(flag == false)
{
cout << "Enter a second : ";
cin >> s;
if(s < 0 || s > 59)
{
flag = false;
cout << "the time is wrong, please try it again. ";
}
else
flag = true;
}
myTime.setTime(h,s);
return myTime;
}
// Method to convert the military time to seconds
long totalSecond()
{
long totalS = milSec;
totalS = totalS + (min * 60);
totalS = totalS + (hour * 3600);
if(blpm)
totalS = totalS + (12 * 3600);
return totalS;
}
};
// Class timeclock
class TimeClock:public MilTime
{
protected:
MilTime endtm;
public:
//Constructor
TimeClock(): MilTime(0,0)
{ MilTime endTime(0,0);
}
// Constructor to set the end time
TimeClock(MilTime sttm, MilTime ettm): MilTime(sttm)
{
endtm = ettm;
}
// Method to return the end time
MilTime getEndTime()
{ return endtm;}
//Method to calculate elapsed time
MilTime elapsedTime()
{
MilTime elpstm;
elpstm = MilTime( endtm.totalSecond()- totalSecond());
return elpstm;
}
};
int main()
{
MilTime sttme, endtm, elpsTime;
MilTime a1(1923,12);
cout<<"initial hour as military time: " <<a1.getHour()<<endl;
cout<<"initial hour as standard time: " <<a1.getStandHr()<<endl;
sttme = sttme.getTime("Enter Start Time in military time : ");
endtm = endtm.getTime("Enter End Time in military time : ");
TimeClock tClock(sttme, endtm);
cout << " total start sec : " << sttme.totalSecond() << endl;
cout << " total end sec : " << endtm.totalSecond() << endl;
cout << " " << endl;
elpsTime = tClock.elapsedTime();
cout << "ElapsedTime is : " << elpsTime.getStandHr() << " : " << elpsTime.getMin();
cout << " : " << elpsTime.getSec()<< endl;
cout << " " << endl;
// converted the military time to standard time.
cout << "Military hours for end time is : " << endtm.getHour() << " and " << endtm.getSec();
cout << " seconds " << endl;
cout << "Standard Time for end time is: " << endtm.getStandHr() << ":" << endtm.getMin() << ":" << endtm.getSec();
cout << endl;
return 0;
}
Result:
Initial hour as military time: 1923
Initial hour as standard time: 7
Enter Start Time in military time: 2112
Enter a second: 21
Enter End Time in military time: 1201
Enter a second: 25
Total start sec: 76341
Total end sec: 43285
Elapsed Time is: -9 : -10 : -56
Military hours for end time is: 1201 and 25 seconds
Standard Time for end time is: 12:1:25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.