Science calculating energy

The problem

Write a program that calculates the energy needed to heat water from an initial temperature to a final temperature. Your program should ask 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. Here is a sample run:

Enter the amount of water in kilograms: 55.5
Enter the initial temperature: 3.5
Enter the final temperature: 10.5
The energy needed is 1625484.0

Breaking it down

public static void main(String[] Strings) {

    Scanner input = new Scanner(;

    System.out.print("Enter the amount of water in kilograms: ");
    double kilograms = input.nextDouble();

    System.out.print("Enter the initial temperature: ");
    double initialTemp = input.nextDouble();

    System.out.print("Enter the final temperature: ");
    double finalTemp = input.nextDouble();


    double energyNeeded = calculateEnergyNeeded(kilograms, initialTemp,
    System.out.print("The energy needed is " + energyNeeded);

private static double calculateEnergyNeeded(double kilograms,
        double initialTemp, double finalTemp) {
    double energyNeeded = kilograms * (finalTemp - initialTemp) * 4184;
    return energyNeeded;


Enter the amount of water in kilograms: 30
Enter the initial temperature: 67
Enter the final temperature: 99
The energy needed is 4016640.0