CS 1054: Project 2

Project 2: Runner and Pace

Due Date: Wednesday March 1/ Thursday March 2

Note

This project should be your and only your work. Please do not discuss this with any of your classmates. If you need help contact TAs or the instructor.

Description

This project 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, one method to get the pace returned as a string, and a method to return information about pace in seconds.

The time class will have three private fields to store hours, minutes, and seconds.  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:

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() // Will print information about the pace on Screen.

    public int toSeconds() // Will print information about the pace in seconds on Screen.

    public void setPace(Time newPace) 

This is for the Time class:

Constructors:

    public Time()

    public Time(int hours, int minutes, int seconds) // Make sure you check for the time user

                                                     // is trying to input

 

Methods:

    public void updateHours(int hours) // will update the hours

    public void updateMinutes(int minutes) // will update the number of minutes, make sure to

                                          // automatically update hours and minutes if user enters

                                          // value more than 59.

    public void updateSeconds(int seconds) // will update the number of seconds, make sure to

                                          // automatically update minutes and seconds if user

                                          // enters value more than 59.

 

    public void setTime (int seconds)   // will reset all the fields to the time user inputs in

                                        // seconds

    public int getHours()

    public int getMinutes()

    public int getSeconds()

    public String toString()

    public int toSeconds() // Will return the entire time in equivalent seconds.

 

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.  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.

Checks

Since you are storing time, you will have need appropriate checks both in setter methods and constructors so that the information is stored properly. For instance none of the minutes and seconds should be greater than 59.  If someone inputs minutes larger than 59, then you should implicitly change the minutes and hours to represent correct time. E.g.: If someone inputs 150 minutes, your methods should appropriately increase the hour field by 2 and add 30 minutes.  Same for minutes and seconds.

 

Another important thing is how to deal with negative integers? I will let you think about a way to handle this.

Please document your code

You must document your code to get a full credit.

Program Compilation

Your program must compile and run.

Pledge

Every program submission for this class must include an Honor Code pledge. Specifically, you must always include the following pledge statement in the header comment for your program:

/****************************************************************************

*

* On my honor:

*

* - I have not discussed the Java language code in my program with

* anyone other than my instructor or the teaching assistants

* assigned to this course.

*

* - I have not used Java language code obtained from another student,

* or any other unauthorized source, either modified or unmodified.

*

* - If any Java language code or documentation used in my program

* was obtained from another source, such as a text book or course

* notes, that has been clearly noted with a proper citation in

* the comments of my program.

*

*

* Your Name

* Your PID

****************************************************************************/

Failure to include this pledge in a submission is a violation of the Honor Code.

 

 


© Mir Farooq Ali 2003.