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

C++ Time Class Define a class to represent time. The class should provide the fo

ID: 643024 • Letter: C

Question

C++ Time Class

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.

Internally, store the time as an integer that is the time in minutes. You can assume all times are positive, and it will be the programmers responsibility to avoid creating a negative time. We may expand the program to handle negative times later. We can also enhance this program with some operator overloading. You should also think about what other functionality a user might want in the Time class.

Explanation / Answer

#include <iostream>
#include <string>

using namespace std;


class Time {

public:

   void setTimeInMinutes(int timeInMinutes);
   void setTimeInMinutes(int hours, int minutes) ;

   int getMinutes();

   int getWholeHours();

   double getHours() ;

   int getRemainingMinutes();

   int add(int time);

   int sub(int time);

   void getMilTime(int &h, int &m) ;

   void getCivTime(int &h, int &m, string &ap);

   Time(int timeInMinutes);

   Time(int hours, int minutes);

private:
   int timeInMinutes;

};


Time::Time(int timeInMins) {
       timeInMinutes = timeInMins;

   }

Time::Time(int hours, int minutes) {

       timeInMinutes = hours * 60 + minutes;

   }

   void Time::setTimeInMinutes(int timeInMins) {
       timeInMinutes = timeInMins;
   }

   void Time::setTimeInMinutes(int hours, int minutes) {

       timeInMinutes = hours * 60 + minutes;

   }

   int Time::getMinutes() {
       cout << " ------" << timeInMinutes << endl;
       return timeInMinutes;
   }

   int Time::getWholeHours() {
       return (int)timeInMinutes/60;;
   }

   double Time::getHours() {
       return ((double)timeInMinutes/60.0);
   }

   int Time::getRemainingMinutes() {
       return timeInMinutes%60;
   }

   int Time::add(int time) {
       return timeInMinutes+time;
   }

   int Time::sub(int time) {
           return timeInMinutes-time;
   }

   void Time::getMilTime(int& h, int& m) {
       h = timeInMinutes/60;
       m = timeInMinutes % 60;
   }

   void Time::getCivTime(int& h, int& m, string& ap) {
       int temp = timeInMinutes/60;

       if(temp >= 12) {
           //ap = "pm";
           h = temp-12;
       }
       else {
           //ap = "am";
           h = temp;
       }

       m = timeInMinutes % 60;
   }


   int main () {

       Time time1(10);
       Time time2(8,50);
       cout << " Time In Minutes Time In Minutes :-- "+ time1.getMinutes() << endl;
       cout << "Time In (Hours: Minutes) : "+ time1.getHours() << ":" << time1.getMinutes() << endl;
          cout << "WholeHours : "+ time1.getWholeHours() << "-- RemainingMinutes "<< time1.getRemainingMinutes()<< endl;
       int h;
       int m;
       h = 14;
       m=15;
       time1.getMilTime(&h, &m);
       cout << " Military Time: " << h <<":" <<m << endl;
       string ap ="";
       time1.getCivTime(&h, &m, &ap);
       cout << " Civil Time: " << h <<":" <<m << " "<< ap << endl;

       cout << " Time In Minutes Time In Minutes :-- "+ time2.getMinutes() << endl;
               cout << "Time In (Hours: Minutes) : "+ time2.getHours() << ":" << time2.getMinutes() << endl;
                  cout << "WholeHours : "+ time2.getWholeHours() << "-- RemainingMinutes "<< time2.getRemainingMinutes()<< endl;

       h = 14;
       m=15;
       time2.getMilTime(&h, &m);
       cout << " Military Time: " << h <<":" <<m << endl;
       string ap ="";
       time2.getCivTime(&h, &m, &ap);
       cout << " Civil Time: " << h <<":" <<m << " "<< ap << endl;

   }

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