C++ VERSION ANWER (NO JAVA VERSION) Define a class to represent time. The class
ID: 640940 • Letter: C
Question
C++ VERSION ANWER (NO JAVA VERSION)
Define a class to represent time. The class should provide the
following methods in the public user interface:
A constructor that takes one integer parameter, a time in minutes.
A constructor that takes two integer parameters, a number of hours
and a number of minutes.
A set method that takes one integer parameter, a time in minutes.
A set method that takes two integer parameters, a number of hours and
a number of minutes.
A getMinutes method that returns the time as all minutes.
A getWholeHours method that returns the number of whole hours.
A getHours method that returns the time in hours as a double.
A getRemainingMinutes that returns the number of minutes after
accounting for the whole hours. For example, it time t represents 135
minutes, then t.getMinutes() is 135, t.getWholeHours() is 2,
t.getRemainingMinutes() is 15 and t.getHours() is 1.25
An add method that returns the calling time plus the parameter time.
A sub method that returns the calling time minus the parameter time.
For example, if t1 represents 30 minutes and t2 represents 15 minutes
then t1.add(t2) returns a time object representing 4 minutes.
A getMilTime method that takes two reference parameters, hours and
minutes, and stores in the parameters the hours and minutes of the
time on a 24 hour military clock. If t represents 75 minutes, then
t.getMilTime(h, m) should store 1 in h and 15 in m.
A getCivTime method that takes three reference parameters, hours,
minutes, and a char ampm and stores in the parameters the hours and
minutes of the time on a 12 hour civilian clock and stores in the
ampm parameter 'a' for am and 'p' for pm. If t represents 75 minutes,
then t.getCivTime(h, m, ap) should store 1 in h, 15 in m, and 'a' in
ap.
Write a main program that creates several time objects and tests the
various class methods. Hand in a printout of the source code and
output from a test run.
Explanation / Answer
That's what I have.
#include "stdafx.h"
#include <ctime>
#include <iostream>
using namespace std;
class Time
{
private:
int minutes;
int hours;
public:
Time(int hour, int min);
Time(int min);
void setTime(int hour, int min);
void setTime(int min);
int getminutes();
int getWholeHours();
int gethours();
int getRemainingMin();
void getCivtime(int& hours, int& minutes, char& ampm);
Time add(Time T);
Time sub(Time T);
};
Time::Time(int min)
{
minutes = min;
}
Time::Time(int hour, int min)
{
minutes = (60 * hour) + min;
}
void Time::setTime(int min)
{
hours = (int)(min / 60.0);
}
void Time::setTime(int hour, int min)
{
minutes = 60 * hour + min;
}
int Time::getminutes()
{
return minutes;
}
int Time::getWholeHours()
{
return minutes / 60;
}
int Time::gethours()
{
return hours;
}
int Time::getRemainingMin()
{
return minutes % 60;
}
Time Time::add(Time T)
{
return (0, minutes + T.minutes);
}
Time Time::sub(Time T)
{
return (0, minutes - T.minutes);
}
void Time::getCivtime(int& hours, int& minutes, char& ampm)
{
if (hours >= 12)
{
this->hours = hours - 12;
this->minutes = minutes;
ampm = 'p';
}
else
{
this->hours = hours;
this->minutes = minutes;
ampm = 'a';
}
}
int main()
{
Time T1(3, 4), T2(6, 6), T3(7, 11);
T2.setTime(15, 15);
T3 = T2.sub(T1);
T2 = T3.add(T2);
int hours = T2.getWholeHours();
int minutes = T2.getRemainingMin();
char ampm;
T2.getCivtime(hours, minutes, ampm);
cout << "Time = " << T2.gethours() << " hour " << T2.getminutes() << " min " << ampm << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.