Answer to Question #212241 in Java | JSP | JSF for nimra

Question #212241

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


1
Expert's answer
2021-06-30T07:12:51-0400
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();
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS