THINK FIRST·CODE LATER

← All labs

Unit converter with named constants

Problem

Write UnitConverter with three named constants (public static final double): KM_PER_MILE = 1.609344, KG_PER_POUND = 0.45359237, CM_PER_INCH = 2.54.

Read miles, pounds and inches and print (2 decimals):

10.00 miles = 16.09 km
150.00 lb = 68.04 kg
12.00 in = 30.48 cm

Why are constants better than writing 1.609344 directly in the formula?

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.

l3-5-data-1.txt

10 150 12
What your program must print for l3-5-data-1.txt
10.00 miles = 16.09 km
150.00 lb = 68.04 kg
12.00 in = 30.48 cm

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

Windows PowerShell does not understand <:

Get-Content l3-5-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.