Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Mark Davis
- * @version 11/22/2013
- */
- import java.util.Scanner;
- import java.io.File;
- import java.io.IOException;
- public class Weight
- {
- static final double G = 6.67 * Math.pow(10,-17);
- public static void main(String[] args)throws IOException
- {
- // Extension idea... instead of hard codeing the weight, you may propt the user for input.
- double earthWeight = 100.0; // initalize Earth weight to 100 lbs.
- String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
- double[] gravity = getGravity(); // static method you write
- double[] weight = calcWeight(earthWeight, gravity); // static method you write
- printResults(names, gravity, weight); // static method you write
- }
- public static double[] getGravity()
- {
- double[] radii = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
- double[] masses = {3.3022 * Math.pow(10,23), 4.8685 * Math.pow(10,24), 5.9736 * Math.pow(10,24), 6.4185 * Math.pow(10,23), 1.8986 * Math.pow(10,27), 5.6846 * Math.pow(10,26), 8.6810 * Math.pow(10,25), 1.0243 * Math.pow(10,26), 1.312 * Math.pow(10,22)};
- double gravities[] = new double[radii.length];
- for (int i = 0; (i <= radii.length -1); i++)
- {
- double result = calcGravity(radii[i], masses[i]);
- gravities[i] = result;
- }
- return gravities;
- }
- public static double calcGravity(double radius, double mass)
- {
- return (G * mass)/Math.pow(radius, 2.0);
- }
- public static double[] calcWeight(double earthWeight, double[] gravity)
- {
- double[] weight = new double[gravity.length];
- for (int i = 0; (i <= gravity.length -1); i++)
- {
- weight[i] = (earthWeight / G) * gravity[i];
- }
- return weight;
- }
- public static void printResults(String[] a, double[] b, double[] c)
- {
- System.out.printf("%7s %7s %22s", "Names", "Gravity", "Weights");
- for (int i = 0; (i <= a.length -1); i++)
- {
- System.out.println();
- System.out.printf("%7s %7.2f %22.0f", a[i], b[i], c[i]);
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement