Question

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 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
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...
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 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...

  • 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...

  • Write in C++ please In Chapter 10, the class clockType was designed to implement the time...

    Write in C++ please In Chapter 10, the class clockType was designed to implement the time of day in a program. Certain applications, in addition to hours, minutes, and seconds, might require you to store the time zone. Derive the class extClockType from the class clockTypeby adding a member variable to store the time zone. Add the necessary member functions and constructors to make the class functional. Also, write the definitions of the member functions and the constructors. Finally, write...

  • 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...

  • Time.cpp: #include "Time.h" #include <iostream> using namespace std; Time::Time(string time) {    hours = 0;   ...

    Time.cpp: #include "Time.h" #include <iostream> using namespace std; Time::Time(string time) {    hours = 0;    minutes = 0;    isAfternoon = false;    //check to make sure there are 5 characters    if (//condition to check if length of string is wrong)    {        cout << "You must enter a valid military time in the format 00:00" << endl;    }    else    {        //check to make sure the colon is in the correct...

  • Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name: True/False: Circle one Assignment...

    Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name: True/False: Circle one Assignment 1 ID: 1. True / False 2. True / False 3. True / False 4. True / False 5. True / False 6. True / False 7. True / False 8. True / False 9. True / False 10. True / False Variable and functions identifiers can only begin with alphabet and digit. Compile time array sizes can be non-constant variables. Compile time array...

  • C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include ...

    C LANGUAGE. PLEASE INCLUDE COMMENTS :) >>>>TheCafe V2.c<<<< #include <stdio.h> int main() { int fries; // A flag denoting whether they want fries or not. char bacon; // A character for storing their bacon preference. double cost = 0.0; // The total cost of their meal, initialized to start at 0.0 int choice; // A variable new to version 2, choice is an int that will store the // user's menu choice. It will also serve as our loop control...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • I need help in C++ . Project 3 – Parking Deck Ticketing System Objectives: Use if,...

    I need help in C++ . Project 3 – Parking Deck Ticketing System Objectives: Use if, switch, and loop statements to solve a problem. Use input and output statements to model a real world application Incorporate functions to divide the program into smaller segments Instructions: Your task is to write a program that simulates a parking meter within the parking deck. The program will start by reading in the time a car arrives in the parking deck. It will then...

  • Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin...

    Code is in C# Your instructor would like to thank to Marty Stepp and Hélène Martin at the University of Washington, Seattle, who originally wrote this assignment (for their CSE 142, in Java) This program focuses on classes and objects. Turn in two files named Birthday.cs and Date.cs. You will also need the support file Date.dll; it is contained in the starter project for this assignment. The assignment has two parts: a client program that uses Date objects, and a...

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
Active Questions
ADVERTISEMENT