School of Engineering and Applied Science
Department of Computer Science
CSci 53 -- Introduction to Software Development
Prof. Michael B. Feldman

Lab Exercise #2
for lab meeting September 14, 2004

Objectives: This exercise will help you explore floating point quantities and output formatting.

Part I. Running a test plan for an example program.

In the lecture on Sept. 9, we designed an example program that finds the average speed of a car trip, and ran it with a test plan, but did not examine the Java source code. Your first task is to
  1. copy that example,, from the programs53 directory
  2. compile the program, then examine the listing file and make sure you understand it
  3. now run it with this test plan, which is adapted from the one we wrote in lecture. Write down the actual results.
Miles Hours Reason Expected Result Brief description of actual result
5 0 zero denominator we hope we get something indicating that we can't divide by 0
50 -3 negative hours hours is bogus (shouldn't be negative), so let's see what happens
0 5 zero numerator no problem - result should be 0
-15 -5 both values negative hmmm - 2 bogus inputs will give a proper output?
250 5 typical 50
250.7 4.5 decimal values the program expects integers; what will happen?
xyz 5 letters - bad value we hope we get something indicating this

Part II: Modifying the example program to use floating-point values instead of integers.

AverageSpeed is rather limited, in that it can handle only integer values. A real miles-per-hour caculator ought to be able to handle values like 3.25 hours or 413.7 miles and give results with fractional parts. It's easy to modify to do this.
  1. Change the three variable declarations from int to double (double means "double-length floating point" and is what we'll use throughout the course for quantities with fractional parts)
  2. Change the statements that read miles and hours from the keyboard, so that they use Keyboard.readDouble() instead of Keyboard.readInt().
  3. Change the user prompts to indicate that decimal values are now OK.
Now compile the program. Try it with a number of test values, including a mixture of integer and decimal values. Try 500 and 7 as one of your input pairs; observe the number of decimal places. Here's a blank table you can use for a test plan and test results:
Miles Hours Reason Expected Result Actual Result











Part III. Exploring formatting of floating-point output values

Java provides a lot of flexibility in formatting floating point output values. To see an example, copy and compile Look carefully at the listing file, especially line 9, line 29, and lines 31 and 32. Try modifying your average speed program by imitating the way it's done in those four lines. (We learn lots of things by imitating others' examples. This is very true in learning how programs work. Observe the example, try to imitate, observe the results.) You can record what you try in this table.
Old code Old output New Code New output










(end of lab)