Java

OOP GUIDE / WORK

 

DURATION CLASS

 

Topics

  • Instance variables, constructors, methods
  • Using encapsulation

 

 

TASK – PART 1 – DURATION CLASS

 

You will create a Duration class which will hold the duration of something like a movie or a song.  Here are the specifications:

 

  • It has 3 private integers called hours, minutes and seconds.

  • It has 3 constructors.

    • Constructor #1 gets a value for each instance variable.

    • Constructor #2 gets two integers, one for minutes and one for seconds.  The value of hours is to be set to 0.

    • Constructor #3 gets one integer for seconds.  Both hours and minutes should be set to 0.

  • It has the following instance methods:

    • The method getTotalSeconds() returns the duration as total seconds.  Remember that 1 hour has 3600 seconds and 1 minute has 60 seconds.

    • The method toString() returns a String in the format AhBmCs where A is the hours, B is the minutes and C is the seconds.  For example, a 2 hour 5 minute and 12 second duration would have a String representation of “2h5m12s”.

 

 

TASK – PART 2 – TESTING THE DURATION CLASS

 

Create a DurationTester class.  Create three Duration objects each using a different constructor.  Make sure to test the instance methods.

 

 

TASK – PART 3 – SHORT ANSWER QUESTIONS

 

a)    Constructor #2 (and Constructor #3) can be accidentally misused.  Can you predict how a programmer might make an error?

b)    The method getTotalSeconds() requires a calculation that might be challenging for some.  How would you test it to make sure that is working correctly like it is intended to be?