ulrail.gif

HW J1: Triathlon

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Slides | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif spacer.gif

 

This homework is due electronically by 10 a.m. on Friday 10 February 2006.  Submission details are at the bottom of this page.

 

Purpose

You have successfully mastered the Olympic Triathlon and now are looking for a challenge: you want to consider the Ironman Triathlon but you're unsure how long it might take given your current times on the Olympic Triathlon (the Ironman Triathlon is considerably longer than the Olympic Triathlon).

For this homework, you are to write a program that will compute the expected duration for the Ironman Triathlon, optimistically assuming that you're able to keep the same pace as you did on the three phases of your Olympic Triathlon (swim, bike, run). The user will have to input three values:

  1. The time it took you to swim the 1.5 km of your Olympic Triathlon
  2. The time it took you to bike the 40 km of your Olympic Triathlon
  3. The time it took you to run the 10 km of your Olympic Triathlon

The program will then compute and print out the following output values:

  1. The rates at which you completed each phase of the Olympic Triathlon (for each of the three parts, the rates to be computed are min/km and min/mile)
  2. The time in hours it would take you to complete each phase of the Ironman Triathlon, given these rates
  3. The total time for the Ironman Triathlon

Background

All Triathlons have three parts: a swim, a bike ride, and a run.  There are a few different triathlon distances -- we are focusing on only two of them.

  • The Olympic triathlon has a 1.5 km swim, a 40 km bicycle ride, and a 10 km run.
     
  • A Ironman triathlon has a 3.8 km swim, a 180 km bicycle ride, and a 42.195 km run (a marathon).

You should use these values in order to get the same results we show below.  If you use similar values (such as 112 miles for the bike ride), you will still get full credit, but your output will be slightly different than what is displayed below, and thus it will be harder for you to test your program.

Design

The program will have the following seven steps:

  1. Define the constants you are using. In particular, there are 1.609344 kilometers per mile. You also need to define constants for all of the distances of the three phases of the two types of Triathlons (i.e. IRONMAN_RUN_LENGTH_KM is 42.195, etc.)
  2. Print out an appropriate legend (see below)
  3. Set up the input stream (i.e., create a new Scanner object).  Only create one of these in your program!
  4. Ask the user for the three inputs described above, and read in those input values
  5. Echo back the input to the user (see below)
  6. Compute all the values.  This is the hard part of the program, as there are a number of conversions required.  If you feel confused, try doing this conversion using a hand calculator so that you understand the conversions required (remember that you can't make a computer do something that you do not know how to do yourself).  There are a number of things that need to be computed for this program:
    • The rates for each of the three segments.  Each rate needs to be computed (and eventually displayed) in min/km and min/mile.  Thus, a total of 6 rates need to be computed.
    • The times for each of the three segments of the Ironman Triathlon (all in hours)
    • The total time for the Ironman (in hours)
  7. Print out the computed values (the six rates, the three segment times, and the total time).  Remember that if you want to print out multiple things with a System.out.println() statement, you need to separate them with a + (a plus sign).  You are welcome to print out the values as they are computed, if you would like.

You should be able to determine the formulas for the computations -- see a TA or professor for help if you are unsure about them. When asking for input, please only ask for those 3 values -- the program is going to be run automatically, so if you ask for more input values, your program will not execute correctly for the graders.

While we have provided most of the design for this program above, you need to provide some design yourself -- in particular, you need to figure out the formula for computing the various output values, what variables will be needed to store them, and how to implement those computations.

How and where to start

We have provided a skeleton Triathlon.java file for you to start with. Please don't change the class name -- doing so will cause it not to compile correctly and result in a loss of lots of points.

The best way to proceed is to go through the seven steps described above, testing the program after each step or two. For example, after you write the code to read in the input from the user, you should print those values out to the screen, so that you can test your input routines. And after you make some partial calculations, print those out to the screen to test the program.  You are welcome to try to write the whole program and debug it all at once -- however, this will take you much more time than testing it incrementally.  Of course, if you have lots of print statements in there, you should probably remove them when the program is working properly, so that the output looks similar to what is shown below.  Lastly, be sure you fully understand how to do the conversions before you try to program that in Java.

Lastly, you may want to look at the BMICalculator.java program (also on page 75 of the textbook and discussed in lecture).  This program, while it computes something different, uses a lot of the same steps described above.

Good programming practices

The following are considered good programming practices.  All homeworks must include these whenever possible.  Not including them will receive points off from the grade. The BMICalculator.java program (on page 75 of the textbook) is an example of a program that follows most of these practices (all but the test code part).

  • Header: The comments at the very beginning of the file, and need to include your name, e-mail ID, 101 section (or 101-E, if appropriate), and a line or two about what this program does.
     
  • Comments: A line or two before each major "block" (section of code, class, method, variable, etc.) describing what it does.
     
  • Whitespace: A line or two between separate elements of the code (methods, code segments, etc.) and good indentation.
     
  • Legend: The program should print out a line or two that states what the purpose of the program is (i.e. "This is a BMI calculator", or "Are you ready for an Ironman Triathlon? Let's find out!"). This should be the first line(s) printed to the screen.
     
  • Echoing input: All user input should be echoed back to the screen. This can be a line saying "you entered 5" after the user enters each input, but can also be a single line saying, "You entered: Swim: 0.5, Bike: 1.3, Run: 0.7".
     
  • Variable names: All final variables should be in all caps with underscores between the words, such as BOX_WIDTH. Non-final variable names should have the firstLetterOfEachWordCaptilized in the variable name (excepting the very first letter). Variable names should be relevant and informative, such as mileage and not m.
     
  • Line length: The lines should not go over about 72 characters. Essentially, when displayed on an 80-character wide screen (what we are using), the line should not go past the end of the screen. You can have a println() statement, for example, use multiple lines (as long as each string starts and ends on the same line). JCreator tells you the column that the cursor is currently in (at the bottom on the status bar), so you can use that to gauge how long your lines are.
     
  • Test code: ALL programs must include proof that you tested the program. For this program, the test is just required as comments -- enter what values you used to test your program, and what the output is.  You do not need to copy-and-paste the output run.  Just inputting random values will not get you full points here -- you need to think about what sort of values will sufficiently test your program, as well as what values you can hand-check to make sure the program works as desired.

When we test your program, we will only use positive values (i.e., greater than zero) for the input values. You only need to test with these types of values as well.

Sample execution

Your program needs to print out similar information, but the format does not have to be the same.  However, the values computed should be the same or very similar.  Note that the text in red is what was input by the user.  And don't worry about really long decimal numbers (such as 2.533333333333333 ) -- we'll see how to convert this into 2.53 (or similar) later in the semester.

Are you ready for an Ironman Triathlon? Let's find out!

Please enter how long each leg of an Olympic Triathlon took in hours:
The swimming part: 1
The biking part: 2
The running part: 1
You entered the following times: Swim: 1.0, Bike: 2.0, Run: 1.0

You swam at a rate of: 40.0 min/km (64.3707756678468 min/mile)
You biked at a rate of: 3.0 min/km (4.82780817508851 min/mile)
You ran at a rate of: 6.0 min/km (9.65561635017702 min/mile)

At this rate, you would do an Ironman in:
        Swim: 2.533333333333333 hr.
        Bike: 9.0 hr.
        Run: 4.2195 hr.
For a total of 15.752833333333333 hrs!

Grading

The following lists the criteria that the graders will be look at when the homework is graded.  Note that these criteria will not be provided on the other homeworks, only on this first one.  Also, the points for each of the following criteria have not yet been determined.

  • Program header (includes name, e-mail ID, and purpose)
  • Good commenting and use of whitespace
  • Prints out an appropriate legend
  • Proper use of Scanner for the input stream
  • Properly prompts user for input
  • Input is echoed to the screen
  • Properly computes the necessary values
  • Prints out the necessary values
  • Variable declaration (good names, proper initialization, etc.)
  • Testing cases shown in comments

If your program does not compile, you will receive 25 points off, in addition to any other penalties. If you do not understand a compilation error, please see a TA or professor (thus, you probably don't want to wait until the night before to start this homework).  We have lots of office hours available, please make use of them!

Submission

When you are finished, submit the Triathlon.java file.  You will notice that we ask more survey questions for the homeworks.

spacer.gif
spacer.gif footer-middle.gif spacer.gif