THINK FIRST·CODE LATER

← All labs

Running pace: from an algorithm to a program

Problem

A runner enters a distance in km and a time in minutes and seconds. Design the algorithm first (input → steps → output), then write RunPace:

INPUT km, minutes, seconds
totalSeconds ← minutes × 60 + seconds
paceSeconds  ← whole part of totalSeconds / km
speed        ← km / (totalSeconds / 3600)
OUTPUT distance, time, pace as "min s per km", speed

For the input

10 52 30

the program prints

Distance: 10.00 km
Time: 3150 seconds
Pace: 5 min 15 s per km
Speed: 11.4 km/h

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l2-4-data-1.txt

10 52 30
What your program must print for l2-4-data-1.txt
Distance: 10.00 km
Time: 3150 seconds
Pace: 5 min 15 s per km
Speed: 11.4 km/h

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l2-4-data-1.txt

Windows PowerShell does not understand <:

Get-Content l2-4-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.