THINK FIRST·CODE LATER

← All labs

Cost of a road trip

Problem

Four friends share the fuel of a road trip. Write the algorithm first, then the program RoadTrip.

INPUT distance (km), kmPerLitre, pricePerLitre, people
litres ← distance / kmPerLitre
cost   ← litres × pricePerLitre
share  ← cost / people
OUTPUT litres, cost, share

For the input

460 14.5 7.8 4

the program prints (two decimals, with printf)

Fuel needed: 31.72 litres
Cost of the trip: 247.45
Share of each of the 4 people: 61.86

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-7-data-1.txt

460 14.5 7.8 4
What your program must print for l2-7-data-1.txt
Fuel needed: 31.72 litres
Cost of the trip: 247.45
Share of each of the 4 people: 61.86

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-7-data-1.txt

Windows PowerShell does not understand <:

Get-Content l2-7-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.