In physics, an object that is in motion is said to have kinetic energy. The following formula can be used to determine a moving object’s kinetic energy:
KE=1/2mv2
The variables in the formula are as follows: KE is the kinetic energy, m is the object’s mass in kilograms, and v is the object’s velocity in meters per second.
1. Write a function named kinetic_energy that accepts an object’s mass (in kilograms) and velocity (in meters per second) as arguments. The function should return the amount of kinetic energy that the object has.
2. Write a statement that calls the kinetic_energy function to get and display the kinetic energy of an object with the mass of 5 kilograms and velocity of 20 meters per second
import java.util.Scanner;
public class Function {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Func f=new Func();
int operatorm=5;
int operatorv=20;
System.out.println("It's the kinetic energy of an object with the mass of 5 kilograms\n" +
"and velocity of 20 meters per second: " +f.kineticEnergy(operatorm,operatorv));
System.out.print("Enter mass of your object: ");
int massS = scan.nextInt();
System.out.print("Enter velocity of your object: ");
int veloc = scan.nextInt();
System.out.println("Kinetic energy: "+f.kineticEnergy(massS,veloc));
}
}
class Func{
double kineticEnergy(int mass, int velocity) {
return (double) (mass * (velocity * velocity)) / 2;
}
}
Comments
Leave a comment