In this post, we will learn to code the Java Program to Calculate Kinetic Energy. Let’s understand Kinetic Energy and How to Calculate Kinetic Energy in Java Programming Language.
What is Kinetic Energy ?
In Physics, the Kinetic Energy of an object is the energy that it possesses due to its motion. It is defined as the work needed to accelerate a body of a given mass from the rest of its stated velocity.
Formula of Kinetic Energy
The Formula of Kinetic Energy is as follows:
Kinetic Energy = ½ x Mass x (Velocity)2
We will use the same formula to code the Java Program to Calculate Kinetic Energy.
Java Program to Calculate Kinetic Energy
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the mass (in kg) : "); float mass = sc.nextFloat(); System.out.println("Enter the velocity (in m/s) : "); float velocity = sc.nextFloat(); float kineticEnergy = (1.0f/2.0f) * mass * (velocity * velocity); if(kineticEnergy < 0){ System.out.println("Kinetic Energy cannot be Negative. Please check your input values."); } else{ System.out.println("The Kinetic Energy is "+kineticEnergy+" J."); } } }
Output
Enter the mass (in kg): 25
Enter the velocity (in m/s): 5
The Kinetic Energy is 312.5 J.
How Does This Program Work ?
Scanner sc = new Scanner(System.in); System.out.println("Enter the mass (in kg) : "); float mass = sc.nextFloat(); System.out.println("Enter the velocity (in m/s) : "); float velocity = sc.nextFloat();
In this program, we are taking the input of the mass in kg and velocity in m/s from the user using the Scanner Class in Java.
Then, we stored the mass and velocity in the respective variables of float data type, as the values can be in decimal.
float kineticEnergy = (1.0f/2.0f) * mass * (velocity * velocity);
Then, using the formula of Kinetic Energy we discussed earlier, we calculate the Kinetic energy and store the value in the variable kineticEnergy
of float data type.
if(kineticEnergy < 0){ System.out.println("Kinetic Energy cannot be Negative. Please check your input values."); } else{ System.out.println("The Kinetic Energy is "+kineticEnergy+" J."); }
Then, we print the kinetic energy of the object using the System.out.println()
function. This is the Java Program to Calculate Kinetic Energy.
Conclusion
I hope after going through this post, you understand Java Program to Calculate Kinetic Energy.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.
Also Read:
Type here..
Java program that calculate kinetic energy
Type here..Java program that calculate kinetic energy