Please edit this Java program below however you see fit so that the following in
ID: 3860693 • Letter: P
Question
Please edit this Java program below however you see fit so that the following instructions are met. I got this wrong the first time because I used an infinite loop which it should not and the program should stop the while loop when the user enters 0. I use jGrasp. Also a screenshot of the jgrasp program would be helpful along with not using things like cout because i do not understand what cout is.
The following formula can be used to calculate a moving object's kinetic energy:
k = (1/2)(m)(v)^2. k is the kinetic energy, m is the object's weight in kilograms, and v is the object's velocity in meters per second.
Write a program that asks the user to enter m and v, then calculates and outputs the kinetic energy if the user inputs are valid. If the user inputs are invalid, outputs an error message.
- The program should repeatedly accept the user inputs and calculate the result until the user chooses not to.
- In the end, the average value is to be calculated.
- The results should be formatted appropriately.
import java.util.Scanner; //use a Scanner object to represent the keyboard
public class Energy
{
public static void main(String[] args)
{
//create the Scanner object representing the keyboard
Scanner console = new Scanner(System.in);
// declare variables
double mass;
double velocity;
double kinetic;
// User Information
System.out.print ("Enter the mass in Kilograms or -1 to end: ");
mass = console.nextDouble();
while (mass != -1)
{
System.out.print ("Enter the velocity in meters per second: ");
velocity = console.nextDouble();
kinetic = (0.5)*(mass)*(velocity)*(velocity);
// print the results
System.out.printf ("Your Kinetic Energy is %f ", kinetic);
}
}// end main
}// end class
Explanation / Answer
import java.util.Scanner;
public class Energy {
public static void main(String[] args) {
// create the Scanner object representing the keyboard
Scanner console = new Scanner(System.in);
// declare variables
double mass;
double velocity;
double kinetic;
// User Information
System.out.print("Enter the mass in Kilograms (0 to Quit): ");
mass = console.nextDouble();
while (mass != 0) {
System.out.print("Enter the velocity in meters per second: ");
velocity = console.nextDouble();
kinetic = (0.5) * (mass) * (velocity) * (velocity);
// print the results
System.out.printf("Your Kinetic Energy is %.2f ", kinetic);
System.out.print("Enter the mass in Kilograms (0 to Quit): ");
mass = console.nextDouble();
}
}// end main
}// end class
__________________
Output:
Enter the mass in Kilograms (0 to Quit): 150
Enter the velocity in meters per second: 5.5
Your Kinetic Energy is 2268.75
Enter the mass in Kilograms (0 to Quit): 120
Enter the velocity in meters per second: 2.5
Your Kinetic Energy is 375.00
Enter the mass in Kilograms (0 to Quit): 200
Enter the velocity in meters per second: 7.5
Your Kinetic Energy is 5625.00
Enter the mass in Kilograms (0 to Quit): 0
_________Thank You