In: Computer Science
import java.util.Scanner;
public class Lab9Q3
{
public static void main (String [] atgs)
{
double mass;
double velocity;
double totalkineticEnergy;
Scanner keyboard = new Scanner (System.in);
System.out.println ("Please enter the objects mass in
kilograms");
mass = keyboard.nextDouble();
System.out.println ("Please enter the objects velocity in meters
per second: ");
velocity = keyboard.nextDouble();
double actualTotal = kineticEnergy (mass, velocity);
System.out.println ("Objects Kinetic Energy: " +
actualTotal);
}
public static double kineticEnergy (double mass, double
velocity)
{
double totalkineticEnergy = ((mass * velocity * 2)/2); //This is my
issue
return totalkineticEnergy;
}
}
My current code is able to be compiled, however I am trying to find the kinetic energy of an object using the formula KE = ½ mv2 my code is only multiplying mass * velocity my overall question is how do I fix this to fit the equation and give me the correct answer.
Using JAVA
Corrected code is given below.change made is shown in bold case.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Error in the code
Corrected code
import java.util.Scanner; public class Lab9Q3 { public static void main(String[] args) { double mass; double velocity; double totalkineticEnergy; Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble(); System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble(); double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy = ((mass * velocity * velocity)/2); //Issue corrected return totalkineticEnergy; } }
Screen shot of the code and output