The program below is the answer to Liang’s Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.10.

Question: Write a program that calculates the energy needed to heat water from an initial temperature to a final temperature. Your program should prompt the user to enter the amount of water in kilograms and the initial and final temperatures of the water. The formula to compute the energy is

Q = M * (finalTemperature – initialTemperature) * 4184

where M is the weight of water in kilograms, temperatures are in degrees Celsius, and energy Q is measured in joules.

1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 2.10 - Science: Calculating Energy<br /> *<br /> */ <br /><br />import java.util.Scanner;<br /><br />public class CalculatingEnergy {<br /><br /> public static void main(String[] args) {<br /> <br /> System.out.println("This Program Calculates The Energy Needed To Heat "<br /> + "Water From An Initial Temperature To A Final Temperature.n");<br /> <br /> Scanner input = new Scanner (System.in);<br /> <br /> System.out.println("Enter the amount of water in kilograms:");<br /> double kilograms = input.nextDouble();<br /> System.out.println("Enter the initial temperature:");<br /> double initialTemperature = input.nextDouble();<br /> System.out.println("Enter the final temperature:"); <br /> double finalTemperature = input.nextDouble();<br /> <br /> double energy = kilograms * (finalTemperature - initialTemperature) * 4184;<br /> <br /> System.out.println("nThe energy needed is " + energy);<br /> }<br />}<br /> |

Click here to see other solutions to Introduction to Java Programming.