Computer Science and Programming

Rowan University
Prof. Gregory Safko

Lab 6

Assigned: March 24, 2003

Due March 26, 2003

 


1. Time.addSeconds(int s)

Write a public (mutator) method in your Time object called addSeconds(int s) that accepts an argument of type int, and adds that number to the private second member variable. 

 

As a test, the body of your code could looks as follows:

 

Time t1(12, 10, 15);

t1.printStandard();

cout << endl;

t1.addSeconds(30);

t1.printStandard();

cout << endl;

 

The output will look like:

12:10:15PM

12:10:45PM

 

As another test, the body of your code could looks as follows:

 

Time t2(11, 45, 1);

t2.printStandard();

cout << endl;

t2.addSeconds(1830);

t2.printStandard();

cout << endl;

 

The output will look like:

11:45:01AM

12:15:31PM

 

Write appropriate error checking which will ensure that anything that puts the second variable over 60 will spill into the minute variable, and anything that puts the minute variable over 60 will spill into the hour variable. Anything that spills the hour variable greater than 24 should just report the hour modulus by 24, as in the following:

 

Time t3(23, 55, 10);

t3.printStandard();

cout << endl;

t3.addSeconds(3602);

t3.printStandard();

cout << endl;

 

The output will look like:

11:55:10PM

12:55:12AM

 

Here's are two examples where the user adds too many seconds (pushing the time object past midnight). 

 

Time t4(23, 59, 59);

t4.printStandard();

cout << endl;

t4.addSeconds(7201);

t4.printStandard();

cout << endl;

 

The output will look like:

11:59:59PM

02:00:00AM

 

Time t5(13, 14, 15);

t5.printStandard();

cout << endl;

t5.addSeconds(86400);

t5.printStandard();

cout << endl;

 

The output will look like:

01:14:15PM

01:14:15PM

 

(This occurred because there are 86400 seconds in 24 hours!) 

The prototype should look like  void addSeconds(int a);

 


2. Time.getTotalSeconds( )

Write a private (facilitator) method in your Time object called getTotalSeconds() that returns a value of type int, which is the time object reported as seconds. In other words, it will return the hours, minutes and seconds as the total amount of seconds. 

Since it is a private method, there is no way to test it directly in your code. You will use it in part 3 of this lab.

The prototype should look like  int getTotalSeconds( );

You will declare this prototype in the private area of your Time class.

 


3. Time.subtractSeconds(int s)

Write a public (mutator) method in your Time object called subtractSeconds(int s) that accepts an argument of type int, and subtract that number of seconds s from your Time object.

 

As a test, the body of your code could looks as follows:

 

Time t6(12, 10, 15);

t6.printStandard();

cout << endl;

t6.subtractSeconds(90);

t6.printStandard();

cout << endl;

 

The output will look like:

12:10:15PM

12:08:45PM

 

As another test, the body of your code could looks as follows:

 

Time t7(9, 10, 4);

t7.printStandard();

cout << endl;

t7.subtractSeconds(7321);

t7.printStandard();

cout << endl;

 

The output will look like:

09:10:04AM

07:08:03AM

 

 

Write appropriate error checking which will ensure that anytime you subtract more seconds than your object has should set the object to midnight (hour = minute = second = 0). This is the same plan to use if the user subtracts more seconds than there are in a day. 

 

Time t8(1, 5, 20);

t8.printStandard();

cout << endl;

t8.subtractSeconds(7201);

t8.printStandard();

cout << endl;

 

The output will look like:

01:05:20AM

12:00:00AM

 

Here's an example where the user subtracts an outrageous number of seconds. 

 

Time t9(20, 25, 32);

t9.printStandard();

cout << endl;

t9.subtractSeconds(100000);

t9.printStandard();

cout << endl;

 

The output will look like:

08:25:32PM

12:00:00AM

 

Use the method getTotalSeconds( ) that you wrote in part 2. It is easier to convert the entire object to total seconds, then subtract s, and then reconvert the total seconds into hours, minutes, and seconds.

 

 


 

Back to the main course page