Write a Java program for the following. Use the pseudo code and data dictionary shown below.
An athlete is interested in their average speed over a distance. The user is to input the distance (in km) and time taken (in seconds) and the average speed in kilometres per hour is to be displayed. Hint: to calculate the speed, first convert the seconds to hours, then divide the distance by the time taken.
Note: making 3600 a double by putting a .0 after it is important in Java, as it results in float point division rather than integer division. e.g. 1800 / 3600 is 0 (how many times does 3600 go into 1800 as a whole number?) whereas 1800 / 3600.0 is 0.5. Try it without the .0 and see the difference.
Run Distance (km) ? 15 Time taken (sec) ? 3600 Average speed = 15kph Run Distance (km) ? 5 Time taken (sec) ? 1500 Average speed = 12kph
calcAverageSpeed
Input distance, timeInSeconds
timeInHours = timeInSeconds / 3600.0
averageSpeed = distance / timeInHours
Display averageSpeed
STOP
| Name | Data Type | Description |
|---|---|---|
| averageSpeed | Double | The average speed over the run in kph (Kilometres per hour) |
| distance | Double | Distance run in kilometres |
| timeInHours | Double | The time taken to run in hours and fractions of hours |
| timeInSeconds | Integer | The time taken to for the run (in seconds) to the nearest whole second |
Due Date: Tutorial week 13
Marks: 2%
Question 1 above is Assessable Tutorial E. Hand in a title page as specified in Assessable Tutorial A, and a printout of the code for question 1.
Written by Tim Whitfort.