Question: I simply need the following code to be commented on so I can better understand what is happening at a more detailed level. I am
I simply need the following code to be commented on so I can better understand what is happening at a more detailed level. I am in an intro class and want to better understand. Please feel free to correct or improve upon anything you see. Code is found below the problem explaination
Java Program: Create an application that determines gravitational potential energy in joules for 3 objects based upon their inputted height (in meters and the ground is considered to be a zero height position.) and relevant mass (in kg).The output window displays all 3 in order from largest gravitational potential energy to smallest with each formula and calculation result. All input/output must accommodate 2 digits post decimal point. All Input/output done in JOptionPane
Gravitational Potential Energy Formula (in joules): Potential Energy = m *g * h
m is the mass of the object in kilograms
h is the height of the object in meters
g is the gravitational constant equal to 9.8 m/sec 2
--------------------------------------------------------------------------------------------------------------
import java.text.DecimalFormat; import javax.swing.JFrame; import javax.swing.JOptionPane; //for user input
public class PotentialEnergy { public static void main(String[] args) { DecimalFormat dc = new DecimalFormat("#.00"); double mass[] = new double[3]; double heights[] = new double[3]; double results[] = new double[3]; //obtain user input for the mass and height
JFrame frame = new JFrame("User Input"); for (int i = 0; i < 3; i++) { mass[i] = Double.parseDouble(JOptionPane.showInputDialog(frame, "Enter the mass of object" + (i + 1))); heights[i] = Double.parseDouble(JOptionPane.showInputDialog(frame, "Enter the height of object" + (i + 1)));
results[i] = heights[i] * mass[i] * 9.8; } //determine order for output int order[] = new int[3]; int highest = highest(results); int secondHighest = secondHighest(results, highest); int thirdHighest = (0 + 1 + 2) - highest - secondHighest;
//ensure the results are displayed from highest to lowest in the JOptionPane window order[0] = highest; order[1] = secondHighest; order[2] = thirdHighest;
StringBuilder sb = new StringBuilder(); sb.append(" Mass Height Gravity Result "); for (int i = 0; i < 3; i++) { int index = order[i]; sb.append(String.format("%5s %5s %5s %10s ",dc.format(mass[index]),dc.format(heights[index]), dc.format(9.8),dc.format(results[index]))); } System.out.println(sb.toString()); JOptionPane.showMessageDialog(frame, sb.toString(), sb.toString(), JOptionPane.PLAIN_MESSAGE);
}
private static int secondHighest(double[] results, int highest) { if (highest == 0) return (results[1] > results[2]) ? 1 : 2; else if (highest == 1) return (results[0] > results[2]) ? 0 : 2; else return (results[1] > results[0]) ? 1 : 0; }
private static int highest(double[] results) { return (results[0] > results[1]) ? (results[1] > results[2] ? 1 : 2) : (results[1] > results[2] ? 1 : 2); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
