THINK FIRST·CODE LATER

← All labs

From pseudocode to Java: temperature converter

Problem

Translate this pseudocode into a Java class TempConvert:

INPUT celsius
fahrenheit ← celsius × 9 / 5 + 32
kelvin ← celsius + 273.15
OUTPUT celsius, fahrenheit, kelvin

Read a real number and print it as 25.0 C = 77.0 F = 298.15 K (1, 1 and 2 decimals, using printf).

Before coding, trace the algorithm by hand for −40 (what is special about the result?).

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

25
What your program must print for l2-1-data-1.txt
25.0 C = 77.0 F = 298.15 K

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

Windows PowerShell does not understand <:

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