(Returning Error Indicators from Class Time’s set Functions) Modify the set functions in the Time class of Figs. 9.10–9.11 to return appropriate error values if an attempt is made to set a data member of an object of class Time to an invalid value. Write a program that tests your new version of class Time. Display error messages when set functions return error values.

What will be an ideal response?


```
#ifndef TIME_H
#define TIME_H

class Time
{
public:
Time( int = 0, int = 0, int = 0 ); // default constructor
bool setTime( int, int, int ); // set hour, minute, second
bool setHour( int ); // set hour
bool setMinute( int ); // set minute
bool setSecond( int ); // set second
int getHour(); // get hour
int getMinute(); // get minute
int getSecond(); // get second
void printUniversal(); // print universal time
void printStandard(); // print standard time
private:
int hour; // 0-23
int minute; // 0-59
int second; // 0-59
}; // end class Time

#endif
```
```
// Member-function definitions for class Time.
#include
#include "Time.h" // include definition of class Time
using namespace std;

Time::Time( int hr, int min, int sec )
{
setTime( hr, min, sec );
} // end Time constructor

bool Time::setTime( int h, int m, int s )
{
bool hourValid = setHour( h ); // invokes function setHour
bool minuteValid = setMinute( m ); // invokes function setMinute
bool secondValid = setSecond( s ); // invokes function setSecond
return hourValid && minuteValid && secondValid;
} // end function setTime

bool Time::setHour( int hr )
{
if ( hr >= 0 && hr < 24 )
{
hour = hr;
return true; // hour is valid
} // end if
else
{
hour = 0;
return false; // hour is invalid
} // end else
} // end function setHour

bool Time::setMinute( int min )
{
if ( min >= 0 && min < 60 )
{
minute = min;
return true; // minute is valid
} // end if
else
{
minute = 0;
return false; // minute is invalid
} // end else
} // end function setMinute

bool Time::setSecond( int sec )
{
if ( sec >= 0 && sec < 60 )
{
second = sec;
return true; // second is valid
} // end if
else
{
second = 0;
return false; // second is invalid
} // end else
} // end function setSecond

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

void Time::printUniversal()
{
cout << ( hour < 10 ? "0" : "" ) << hour << ':'
<< ( minute < 10 ? "0" : "" ) << minute << ':'
<< ( second < 10 ? "0" : "" ) << second;
} // end function printUniversal

void Time::printStandard()
{
cout << ( ( hour % 12 == 0 ) ? 12 : hour % 12 ) << ':'
<< ( minute < 10 ? "0": "" ) << minute << ':'
<< ( second < 10 ? "0": "" ) << second
<< ( hour < 12 ? " AM" : " PM" );
} // end function printStandard
```
```
#include
#include "Time.h" // include definition of class Time
using namespace std;

int getMenuChoice(); // prototype

int main()
{
Time time; // the Time object
int choice = getMenuChoice();
int hours;
int minutes;
int seconds;

while ( choice != 4 )
{
switch ( choice )
{
case 1: // set hour
cout << "Enter Hours: ";
cin >> hours;

if ( !time.setHour( hours ) )
cout << "Invalid hours." << endl;
break;
case 2: // set minute
cout << "Enter Minutes: ";
cin >> minutes;

if ( !time.setMinute( minutes ) )
cout << "Invalid minutes." << endl;
break;
case 3: // set seconds
cout << "Enter Seconds: ";
cin >> seconds;

if ( !time.setSecond( seconds ) )
cout << "Invalid seconds." << endl;
break;
} // end switch

cout << "Hour: " << time.getHour() << " Minute: "
<< time.getMinute() << " Second: " << time.getSecond() << endl;
cout << "Universal time: ";
time.printUniversal();
cout << " Standard time: ";
time.printStandard();
cout << endl;

choice = getMenuChoice();
} // end while
} // end main

// prints a menu and returns a value corresponding to the menu choice
int getMenuChoice()
{
int choice;

cout << "1. Set Hour\n2. Set Minute\n3. Set Second\n"
<< "4. Exit\nChoice: " << endl;
cin >> choice;
return choice;
} // end function getMenuChoice
```
1. Set Hour
2. Set Minute
3. Set Second
4. Exit
Choice:
1
Enter Hours: 17
Hour: 17 Minute: 0 Second: 0
Universal time: 17:00:00 Standard time: 5:00:00 PM
1. Set Hour
2. Set Minute
3. Set Second
4. Exit
Choice:
2
Enter Minutes: 65
Invalid minutes.
Hour: 17 Minute: 0 Second: 0
Universal time: 17:00:00 Standard time: 5:00:00 PM
1. Set Hour
2. Set Minute
3. Set Second
4. Exit
Choice:
3
Enter Seconds: 23
Hour: 17 Minute: 0 Second: 23
Universal time: 17:00:23 Standard time: 5:00:23 PM
1. Set Hour
2. Set Minute
3. Set Second
4. Exit
Choice:
4

Computer Science & Information Technology

You might also like to view...

Which of the following statements is false?

a. Prior to Java SE 8, it was common to associate with an interface a class containing static helper methods for working with objects that implemented the interface. b. Class Collections contains many static helper methods for working with objects that implement interfaces Collection, List, Set and more. c. Collections method sort can sort objects of any class that implements interface List. d. With non-static interface methods, helper methods can now be declared directly in interfaces rather than in separate classes.

Computer Science & Information Technology

Which of the following policies makes it difficult for an individual to violate InfoSec and is quite useful in monitoring financial affairs?

A. task rotation B. mandatory vacations C. separation of duties D. job rotation

Computer Science & Information Technology

In general, use no more than _____ font types in a worksheet.

A. two B. four C. six D. eight

Computer Science & Information Technology

The fadeToBlack() and fadeFromBlack() methods should always be used in pairs.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology