Question

IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a tick member function that increments the time stored in a Time object by one second. Write a program that tests the tick me...

IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a tick member function that increments the time stored in a Time object by one second. Write a program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member function works correctly. Be sure to test the following cases:

a) Incrementing into the next minute.

b) Incrementing into the next hour.

c) Incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 A

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

1.Incrementing into next minute:

#include <iostream>
using namespace std;

class Time
{
   private:
      int hours;             // 0 to 23
      int minutes;           // 0 to 59
   public:
      // required constructors
      Time(){
         hours = 0;
         minutes = 0;
      }
      Time(int h, int m){
         hours = h;
         minutes = m;
      }
      // method to display time
      void displayTime()
      {
         cout << "H: " << hours << " M:" << minutes <<endl;
      }
      // overloaded prefix ++ operator
      Time operator++ ()
      {
         ++minutes;          // increment this object
         if(minutes >= 60)
         {
            ++hours;
            minutes -= 60;
         }
         return Time(hours, minutes);
      }
      // overloaded postfix ++ operator
      Time operator++( int )       
      {
         // save the orignal value
         Time T(hours, minutes);
         // increment this object
         ++minutes;                  
         if(minutes >= 60)
         {
            ++hours;
            minutes -= 60;
         }
         // return old original value
         return T;
      }
};
int main()
{
   Time T1(11, 59), T2(10,40);

   ++T1;                    // increment T1
   T1.displayTime();        // display T1
   ++T1;                    // increment T1 again
   T1.displayTime();        // display T1

   T2++;                    // increment T2
   T2.displayTime();        // display T2
   T2++;                    // increment T2 again
   T2.displayTime();        // display T2
   return 0;
}
2.Incrementing into the next hour:

#include <iostream>
#include <iomanip>
#include <stdexcept>    // Needed for exception handling
#include "Time.h"
using namespace std;

Time::Time(int h, int m, int s) {
   // Call setters to perform input validation
   setHour(h);
   setMinute(m);
   setSecond(s);
}

int Time::getHour() const {
   return hour;
}

void Time::setHour(int h) { // with input validation
   if (h >= 0 && h <= 23) {
      hour = h;
   } else {
      throw invalid_argument("Invalid hour! Hour shall be 0-23.");
            // need <stdexcept>
   }
}

int Time::getMinute() const {
   return minute;
}

void Time::setMinute(int m) {
   if (m >= 0 && m <= 59) {
      minute = m;
   } else {
      throw invalid_argument("Invalid minute! Minute shall be 0-59.");
            // need <stdexcept>
   }
}

int Time::getSecond() const {
   return second;
}

void Time::setSecond(int s) {
   if (s >= 0 && s <= 59) {
      second = s;
   } else {
      throw invalid_argument("Invalid second! Second shall be 0-59.");
            // need <stdexcept>
   }
}

void Time::setTime(int h, int m, int s) {
   // Call setters to validate inputs
   setHour(h);
   setMinute(m);
   setSecond(s);
}

void Time::print() const {
   cout << setfill('0');
   cout << setw(2) << hour << ":" << setw(2) << minute << ":"
        << setw(2) << second << endl;
}
3.incrementing into the Next Day:

#include &lt;iomanip&gt;
using std::setfill;
using std::setw;

#include "Time.h" // include definition of class Time from Time.h

// Time constructor initializes each data member to zero;
// ensures that Time objects start in a consistent state
Time::Time( int hr, int min, int sec )
{
   setTime( hr, min, sec ); // validate and set time
} // end Time constructor

// set new Time value using universal time; ensure that
// the data remains consistent by setting invalid values to zero
void Time::setTime( int h, int m, int s )
{
   setHour( h ); // set private field hour
   setMinute( m ); // set private field minute
   setSecond( s ); // set private field second
} // end function setTime

// set hour value
void Time::setHour( int h )
{
   hour = ( h &gt;= 0 &amp;&amp; h &lt; 24 ) ? h : 0; // validate hour
} // end function setHour

// set minute value
void Time::setMinute( int m )
{
   minute = ( m &gt;= 0 &amp;&amp; m &lt; 60 ) ? m : 0; // validate minute
} // end function setMinute

// set second value
void Time::setSecond( int s )
{
   second = ( s &gt;= 0 &amp;&amp; s &lt; 60 ) ? s : 0; // validate second
} // end function setSecond
[b][color=#FF0000]

void Time::Tick()
{
i need some sort of loop in here is all i assume...
}

[/color][/b]
// return hour value
int Time::getHour()
{
   return hour;
} // end function getHour

// return minute value
int Time::getMinute()
{
   return minute;
} // end function getMinute

// return second value
int Time::getSecond()
{
   return second;
} // end function getSecond

// print Time in universal-time format (HH:MM:SS)
void Time::printUniversal()
{
   cout &lt;&lt; setfill( '0' ) &lt;&lt; setw( 2 ) &lt;&lt; getHour() &lt;&lt; ":"
          &lt;&lt; setw( 2 ) &lt;&lt; getMinute() &lt;&lt; ":" &lt;&lt; setw( 2 ) &lt;&lt; getSecond();
} // end function printUniversal

// print Time in standard-time format (HH:MM:SS AM or PM)
void Time::printStandard()
{
   cout &lt;&lt; ( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 )
          &lt;&lt; ":" &lt;&lt; setfill( '0' ) &lt;&lt; setw( 2 ) &lt;&lt; getMinute()
          &lt;&lt; ":" &lt;&lt; setw( 2 ) &lt;&lt; getSecond() &lt;&lt; ( hour &lt; 12 ? " AM" : " PM" );
} // end function printStandard

Kindly revert for any queries

Thanks.

Add a comment
Answer #2
Sir hamye project assign hua ha aur ais project ky related hamara viva bi ho ga so kinldy plz All information provided
answered by: anonymous

> Bhai tun UAF TTS say ha na

Muhammad Qasim 320 Fri, Jan 14, 2022 4:09 AM

Add a comment
Know the answer?
Add Answer to:
IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a tick member function that increments the time stored in a Time object by one second. Write a program that tests the tick me...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a...

    IN C++ pls. Modify the Time class of Figs. 9.8–9.9 of your book to include a tick member function that increments the time stored in a Time object by one second. Write a program that tests the tick member function in a loop that prints the time in standard format during each iteration of the loop to illustrate that the tick member function works correctly. Be sure to test the following cases: a) Incrementing into the next minute. b) Incrementing...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (complex Class) Create a class called complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form where i is V-I Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in...

  • Java Programming The program template represents a complete working Java program with one or more key...

    Java Programming The program template represents a complete working Java program with one or more key lines of code replaced with comments. Read the problem description and examine the output, then study the template code. Using the problem-solving tips as a guide, replace the /* */ comments with Java code. Compile and execute the program. Compare your output with the sample output provided. Modify class Time2 to include a tick method that increments the time stored in a Time2 object...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT