Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I simply need the following code to be commented on so I can better understand w

ID: 3804513 • Letter: I

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 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);
   }
}

Explanation / Answer


// PotentialEnergy.java


import java.text.DecimalFormat; // for adjusting decimal points
import javax.swing.JFrame; // for creating and displaying GUI
import javax.swing.JOptionPane; //for user input

public class PotentialEnergy
{
public static void main(String[] args)
{
// variable dc from class DecimalFormat storing that there will be 2 decimal places in all the values
DecimalFormat dc = new DecimalFormat("#.00");
// declaring maas array of size 3
double mass[] = new double[3];
// declaring heights array of size 3
double heights[] = new double[3];
// declaring results array of size 3
double results[] = new double[3];

//obtain user input for the mass and height
JFrame frame = new JFrame("User Input");

// iterate over the arrays and get input from the user for all the 3 objects
for (int i = 0; i < 3; i++)
{
// get input user
// showInputDialog print a text message and to take input from user nad parseDOuble converted the input value to double
// the value further gets stored in the index of corresponding array
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];
  
// calling method highest to get the results
// the variable name is change because the name of function and variable should not be the same
int highestValue = highest(results);
// get the second highest and the thirt highest value
int secondHighestValue = secondHighest(results, highestValue);
// get the thirf highest and the thirt highest value
int thirdHighest = (0 + 1 + 2) - highestValue - secondHighestValue;
//ensure the results are displayed from highest to lowest in the JOptionPane window
order[0] = highestValue;
order[1] = secondHighestValue;
order[2] = thirdHighest;

// In order to print the result in a proper format
// string buffer will be used
StringBuilder sb = new StringBuilder();
sb.append(" Mass Height Gravity Result ");

// iterate over the arrays
for (int i = 0; i < 3; i++)
{
int index = order[i];
// convert the array values to 2 decimal place append all of them to string buffer object
sb.append(String.format("%5s %5s %5s %10s ",dc.format(mass[index]),dc.format(heights[index]), dc.format(9.8),dc.format(results[index])));
}

// convert the stringbuffer object to string and output the reuslt to console
System.out.println(sb.toString());
// similarly output the results to Frame in for of a message
JOptionPane.showMessageDialog(frame, sb.toString(), sb.toString(),JOptionPane.PLAIN_MESSAGE);
}

// private method which takes in an array and a values and return the second highest value in array
private static int secondHighest(double[] results, int highestValue)
{

// check if highest value is 0,1, or 2 and return the second highest value accordingly
if (highestValue == 0)
return (results[1] > results[2]) ? 1 : 2;
else if (highestValue == 1)
return (results[0] > results[2]) ? 0 : 2;
else
return (results[1] > results[0]) ? 1 : 0;
}

// private method which takes in array and returns the highest value in an array
private static int highest(double[] results)
{
// if the 1st index value is greater than all the ther elements
// return the 1st element, similarly check for 2nd and if not finally return the 3rd
return (results[0] > results[1]) ? (results[1] > results[2] ? 1 : 2): (results[1] > results[2] ? 1 : 2);
}
}