Program To Calculate Energy

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.

```/**
*
* @Author: Aghatise Osazuwa
* Website: www.cscprogrammingtutorials.com
*
* Exercise 2.10 - Science: Calculating Energy
*
*/

import java.util.Scanner;

public class CalculatingEnergy {

public static void main(String[] args) {

System.out.println("This Program Calculates The Energy Needed To Heat "
+ "Water From An Initial Temperature To A Final Temperature.\n");

Scanner input = new Scanner (System.in);

System.out.println("Enter the amount of water in kilograms:");
double kilograms = input.nextDouble();
System.out.println("Enter the initial temperature:");
double initialTemperature = input.nextDouble();
System.out.println("Enter the final temperature:");
double finalTemperature = input.nextDouble();

double energy = kilograms * (finalTemperature - initialTemperature) * 4184;

System.out.println("\nThe energy needed is " + energy);
}
}
```

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

Post a Comment

DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.

Thanks.