CSCI 240 Practice Test Questions with Verified
Correct Answers
Write the constructor for the Time class. It takes three integer arguments: the potential
hour, minute, and second values for the Time object. The constructor should pass the
individual arguments to the setHour, setMinute, and setSecond methods to initialize the
data members.
Time :: Time ( int newHr, int newMin, int newSec )
{
setHour ( newHr );
setMinute ( newHr );
setSecond( newSec );
}
Write the setHour method for the Time class. It takes an integer argument that will
potentially be used to initialize the hour data member. If the passed in value is not
between 1 and 12, inclusive, then initialize the hour data member to 12. Otherwise, use
the passed in value to initialize the hour data member.
void Time :: setHour( int diffHr )
{
if (diffHr < 1 or diffHr > 12)
{
hour = 12;
}
else
{
, hour = diffHr;
}
}
Write the getSecond method for the Time class. It takes no arguments and returns an
integer. This method should simply return the contents of the second data member.
int Time :: getSecond()
{
return second;
}
Write the printTime method for the Time class. It takes no arguments and returns
nothing. This method should print the time in the format hour:minute:second. For
minutes and seconds, if the value is less than 10, display a leading 0.
void Time :: printTime()
{
cout << hour << ":";
if ( minute < 10 )
cout << "0";
cout << minute << ":";
if ( second < 10 )
cout << "0";
Correct Answers
Write the constructor for the Time class. It takes three integer arguments: the potential
hour, minute, and second values for the Time object. The constructor should pass the
individual arguments to the setHour, setMinute, and setSecond methods to initialize the
data members.
Time :: Time ( int newHr, int newMin, int newSec )
{
setHour ( newHr );
setMinute ( newHr );
setSecond( newSec );
}
Write the setHour method for the Time class. It takes an integer argument that will
potentially be used to initialize the hour data member. If the passed in value is not
between 1 and 12, inclusive, then initialize the hour data member to 12. Otherwise, use
the passed in value to initialize the hour data member.
void Time :: setHour( int diffHr )
{
if (diffHr < 1 or diffHr > 12)
{
hour = 12;
}
else
{
, hour = diffHr;
}
}
Write the getSecond method for the Time class. It takes no arguments and returns an
integer. This method should simply return the contents of the second data member.
int Time :: getSecond()
{
return second;
}
Write the printTime method for the Time class. It takes no arguments and returns
nothing. This method should print the time in the format hour:minute:second. For
minutes and seconds, if the value is less than 10, display a leading 0.
void Time :: printTime()
{
cout << hour << ":";
if ( minute < 10 )
cout << "0";
cout << minute << ":";
if ( second < 10 )
cout << "0";