Java

OOP GUIDE / WORK

 

DURATION CLASS 2

 

Topics

  • Implementing advanced methods
  • Private instance method

 

 

TASK – PART 1 – SETUP

 

Copy and paste the Duration class from the Duration class solutions.

 

 

TASK – PART 2 – PRIVATE METHOD (SIMPLIFY)

 

As it stands, the Duration class can have a value for seconds or minutes that exceeds 60.  For example, the code

 

     Duration d = new Duration(1, 200, 302);

 

would give a String representation of 1h200m302s.

 

We want to remove this.  We want the 302 seconds to become 5 minutes and 2 seconds.  Then we want the 205 minutes to become 2 hours and 25 minutes.  So in total, we should get a String representation of 3h25m2s.

 

You are asked to create a method called simplify.  It will check the instance variables and make sure that both seconds and minutes are under 60.  If they aren’t, it will reduce them and increase the proper instance variable.

 

Also, we want this method to be called immediately when the object is created.  So we will call it from each constructor. 

 

Furthermore, we do not want this method to be used from outside the class.  It only needs to get used once.  So let’s make it private.

 

TASK – PART 3 – TESTING YOUR SIMPLIFY METHOD

 

Test that your method works.