Tutorial 22 - Java 2 (Sequence)


Home > Tutorials > Tutorial22

 

** 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. (This problem is from question 2 in tutorial 6)

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.

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

Note: beware of integer division when calculating the time in hours. Dividing  by 3600.0 (a double) rather than 3600 (an int) ensures that floating pint division occurs.

Question 2

Write a Java program that allows the user to input two integers, then calculates and displays result of integer division and the modulus of the two numbers.

Example

Run
Numerator ? 9
Denominator ? 4

Integer Division of 9 / 4 = 2
Modulus of 9 / 4 = 1

Question 3

Copy question 2, and change it to use doubles rather than integers.

Run
Numerator ? 9
Denominator ? 4

Floating Point Division of 9 / 4 = 2.25
Modulus of 9 / 4 = 1

Assessable Tutorial E

Due Date: Tutorial 24
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 .

 


Previous Tutorial | Tutorial Index | Next Tutorial | Lecture
Last modified 09-May-2005 by Tim Whitfort.
Copyright © 2003-2005
Tim Whitfort