Note: You can work either individually or with a partner for Lab 4. You can select your partner or TA can assign you one.

 

This lab will explore the way objects interact with each other.  You will be creating two objects, one that the other uses to store some information.

 

The idea is that we want to have a runner who has a pace.  A pace is how fast a runner completes a mile. The two classes we will implement are a runner class and a time class.

Details:

As previously stated, we will be implementing two classes: a runner class and a time class.

 

The runner class will have one private field that is its pace which is an object of the time class.  It will have one method to set a new pace and one method to get the pace returned as a string.

 

The time class will have three private fields to store hours, minutes, and seconds.  This class will be able to represent pace information and time need to complete the distance since both are really just a time.  It will have a setter method and a getter method for each of the private fields.  You will have a method that will return a string that indicates the time that is stored in the object.  You will need a method that takes a number of seconds and sets the hours, minutes, and seconds accordingly.  Finally, you will need a method that returns the number of seconds the time represents.

Interface:

Each class will present the “user” with an interface.  An interface is a set of constructors and methods that the user can call.  The previous two paragraphs explain in English what that interface will look like to an extent.  Below are the methods and constructors your classes must implement. Please note that you will have to name your fields (or class variables) for time class hours, minutes and seconds. Thus, both the parameters and fields have the same name.

 

This is for the Runner class:

Constructor:

    public Runner(Time myPace)

 

Methods:

    public String getPace()

    public void setPace( Time newPace )

 

This is for the Time class:

Constructors:

    public Time()

    public Time(int hours, int minutes, int seconds)

Methods:

    public void setHours( int hours )

    public void setMinutes( int minutes )

    public void setSeconds( int seconds )

    public int getHours(  )

    public int getMinutes(  )

    public int getSeconds(  )

    public String toString()

    public int toSeconds()

    public fromSeconds(int totalSeconds)

 

The one thing that needs to be mentioned is what the string looks like when a Time object is asked to use the toString method.  The string the Time object returns has two places for each field.  So an example of what might get returned if the Time object was storing 1 hour 3 minutes and 9 seconds is: “01:03:09”.  Notice that there is a zero in front of each single digit number and a colon “:” in between each of the hours, minutes and seconds.