increments the time stored in a Time object by one second. The Time object should always remain in a consistent state. Write a driver 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 AM) // Section 16, Question 8 // time.h #ifndef time_h #define time_h class Time { public: Time( int = 0, int = 0, int = 0 ); void setTime( int, int, int ); void setHour( int ); void setMinute( int ); void setSecond( int ); int getHour(); int getMinute(); int getSecond(); void printStandard(); void tick( const int ); private: int hour; int minute; int second; }; #endif // Section 16, Question 8 // time.cpp #include <iostream> using std::cout; #include "time.h"
Time::Time( int hr, int min, int sec )
{ setTime( hr, min, sec ); } void Time::setTime( int h, int m, int s ) { setHour( h ); setMinute( m ); setSecond( s ); } void Time::setHour( int h) { hour = ( h >= 0 && h < 24 ) ? h : 0; } void Time::setMinute( int m) { minute = ( m >= 0 && m < 60 ) ? m : 0; } void Time::setSecond( int s) { second = ( s >= 0 && s < 60 ) ? s : 0; } int Time::getHour() { return hour; } int Time::getMinute() { return minute; } int Time::getSecond() { return second; } void Time::printStandard() { cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":" << ( minute < 10 ? "0" : "" ) << minute << ":" << ( second < 10 ? "0" : "" ) << second << ( hour < 12 ? " AM" : " PM" ); } void Time::tick( const int count ) { cout << "\n\nIncrementing second " << count << " times:\nStart Time: "; printStandard(); for ( int i = 0; i < count; i++ ) { setSecond( ( getSecond() + 1 ) %60 ); if ( getSecond() == 0 ) { setMinute( ( getMinute() + 1 ) % 60 ); if ( getMinute() == 0 )
// main.cpp #include <iostream> using std::cout; using std::endl; #include "time.h" int main() { Time t; t.setTime( 11, 59, 55 ); cout << "Time is set to: "; t.printStandard(); t.tick( 10 ); return 0; }
Time is set to: 11:59:55 AM
Incrementing second 10 times: Start Time: 11:59:55 AM second + 1: 11:59:56 AM second + 1: 11:59:57 AM second + 1: 11:59:58 AM second + 1: 11:59:59 AM second + 1: 12:00:00 PM second + 1: 12:00:01 PM second + 1: 12:00:02 PM second + 1: 12:00:03 PM second + 1: 12:00:04 PM second + 1: 12:00:05 PM
Here are the answers to the questions on Python revision:1. _type 2. >3. and 4. i5. ii6. ii 7. i8. i9. i10. No output is printed as the code is just assigning values to variables
The Advanced Roblox Coding Book: An Unofficial Guide, Updated Edition: Learn How to Script Games, Code Objects and Settings, and Create Your Own World!
Learn Python Programming for Beginners: Best Step-by-Step Guide for Coding with Python, Great for Kids and Adults. Includes Practical Exercises on Data Analysis, Machine Learning and More.