Advertisement
markd315

Untitled

Nov 22nd, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. /**
  2. * @author Mark Davis
  3. * @version 11/22/2013
  4. */
  5. import java.util.Scanner;
  6. import java.io.File;
  7. import java.io.IOException;
  8.  
  9. public class Weight
  10. {
  11. static final double G = 6.67 * Math.pow(10,-17);
  12.  
  13. public static void main(String[] args)throws IOException
  14. {
  15.  
  16. // Extension idea... instead of hard codeing the weight, you may propt the user for input.
  17.  
  18. double earthWeight = 100.0; // initalize Earth weight to 100 lbs.
  19.  
  20. String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
  21. double[] gravity = getGravity(); // static method you write
  22. double[] weight = calcWeight(earthWeight, gravity); // static method you write
  23. printResults(names, gravity, weight); // static method you write
  24.  
  25. }
  26.  
  27. public static double[] getGravity()
  28. {
  29.  
  30. double[] radii = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
  31. 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)};
  32. double gravities[] = new double[radii.length];
  33. for (int i = 0; (i <= radii.length -1); i++)
  34. {
  35. double result = calcGravity(radii[i], masses[i]);
  36. gravities[i] = result;
  37. }
  38. return gravities;
  39. }
  40.  
  41. public static double calcGravity(double radius, double mass)
  42. {
  43. return (G * mass)/Math.pow(radius, 2.0);
  44.  
  45. }
  46.  
  47. public static double[] calcWeight(double earthWeight, double[] gravity)
  48. {
  49. double[] weight = new double[gravity.length];
  50.  
  51. for (int i = 0; (i <= gravity.length -1); i++)
  52. {
  53. weight[i] = (earthWeight / G) * gravity[i];
  54. }
  55. return weight;
  56. }
  57.  
  58. public static void printResults(String[] a, double[] b, double[] c)
  59. {
  60. System.out.printf("%7s %7s %22s", "Names", "Gravity", "Weights");
  61.  
  62. for (int i = 0; (i <= a.length -1); i++)
  63. {
  64. System.out.println();
  65. System.out.printf("%7s %7.2f %22.0f", a[i], b[i], c[i]);
  66. }
  67. System.out.println();
  68.  
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement