THINK FIRST·CODE LATER

← All labs

Energy to heat water

Problem

Heating 1 kg of water by 1 °C takes 4184 joules. HeatWater reads the mass of water in kg, its starting temperature and the final temperature, and prints the energy needed in joules, in kilojoules and in kilowatt-hours (1 kWh = 3 600 000 J).

Use final double constants for 4184 and 3 600 000. For the input

55.5 3.5 10.5

the program prints

Energy: 1625484.0 J
Energy: 1625.5 kJ
Energy: 0.452 kWh

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

55.5 3.5 10.5
What your program must print for l3-8-data-1.txt
Energy: 1625484.0 J
Energy: 1625.5 kJ
Energy: 0.452 kWh

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

Windows PowerShell does not understand <:

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