Tutorial Week 13 - Java 2


Home


** This tutorial contains Assessable Tutorial E **

This tutorial is in the Unix lab.

Question 1

Write a Java program for the following. Use the pseudo code and data dictionary shown below.

Problem Description

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.

Example Runs

Run
Distance (km) ?  15
Time taken (sec) ? 3600
Average speed = 15kph

Run
Distance (km) ?  5
Time taken (sec) ? 1500
Average speed = 12kph

Pseudo Code

calcAverageSpeed
    Input distance, timeInSeconds
    timeInHours = timeInSeconds / 3600.0
    averageSpeed = distance / timeInHours
    Display averageSpeed
STOP

Data Dictionary

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

2. Catch up on your tutorials


Assessable Tutorial E

Due Date: Tutorial week 13
Marks: 2%

Question 1 above is Assessable Tutorial E. Show your tutor a printout of the code for question 1.

 


Written by Tim Whitfort.