Write a program using class ‘FindEnergy’ to compute the Potential Energy and kinetic Energy of an object. The class FindEnergy should have following members.
• Four data members mass, height, velocity and gravity (9.8m/s2) take gravity as Constant.
• One member function ‘setInputData’ to assign values to data members.
• One member function ‘potentialEnergy’ to compute. And return the Potential Energy.
HINT: P.E=mgh
• One member function ‘kineticEnergy’ to compute. And return the Kinetic Energy.
HINT: K.E=1/2 mv2
FindEnergy.java
public class FindEnergy {
private double mass,height,velocity;
public final double gravity = 9.8;
public void setInputData(double mass, double height, double velocity)
{
this.mass = mass;
this.height = height;
this.velocity = velocity;
}
public double potentialEnergy()
{
return mass*gravity*height;
}
public double kineticEnergy()
{
return (1.0/2)*mass*Math.pow(velocity, 2);
}
}
Main.java
import java.util.Scanner;
public class Main
{
public static void main(String []args)
{
FindEnergy calculator= new FindEnergy();
Scanner sc = new Scanner(System.in);
System.out.print("Enter mass of object: ");
double mass = sc.nextDouble();
System.out.print("Enter height of object: ");
double height = sc.nextDouble();
System.out.print("Enter velocity of object: ");
double velocity = sc.nextDouble();
calculator.setInputData(mass, height, velocity);
System.out.printf("Potential Energy of Object is: %.2f\n",calculator.potentialEnergy());
System.out.printf("Kinetic Energy of Object is: %.2f\n",calculator.kineticEnergy());
sc.close();
}
}
Comments
Leave a comment