Advertisement
bha2597

Untitled

Aug 18th, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. /**
  2.  * Read date from a text file and does magic(math) with it.
  3.  *
  4.  * @author Bruno Alves
  5.  * @version 8/18/2014
  6.  */
  7. import java.util.Scanner;
  8. import java.io.File;
  9. import java.io.IOException;
  10.  
  11. public class Weight
  12. {
  13.     // Note: formula for finding weight on a planet:  Earth weight divided by Gravity constant times surface gravity
  14.     public static double[] getGravity()throws IOException
  15.     {
  16.         File gravityFile = new File("GravityV1.txt");
  17.        
  18.         Scanner file = new Scanner(gravityFile);
  19.        
  20.         double[] gravity = new double[9];
  21.            
  22.            
  23.             for(int i = 0; file.hasNextDouble();i++)
  24.             {
  25.                 gravity[i] = file.nextDouble() / 10;
  26.             }
  27.            
  28.            
  29.         file.close();
  30.        
  31.         return gravity;
  32.     }
  33.    
  34.    
  35.     public static double[] calcWeight(double earthWeight, double[] gravity)
  36.     {
  37.         double[] weight = new double[gravity.length];
  38.        
  39.             for (int i = 0; i < weight.length; i++)
  40.             {
  41.                 weight[i] = (earthWeight * 433.59237)  / gravity[i];
  42.                 weight[i] = weight[i] * 453.592;
  43.  
  44.             }
  45.            
  46.         return weight;
  47.     }
  48.  
  49.    
  50.    
  51.     public static void printResults(String[] names, double[] gravity, double[] weight) 
  52.     {
  53.         System.out.printf("     My Weight On The Planets%n");
  54.        
  55.         System.out.printf("Planet    Gravity     Weight(lbs)%n");
  56.         System.out.printf("*************************************%n");
  57.             for(int i = 0; i < gravity.length; i++)
  58.             {
  59.                 System.out.printf("%-10s %-12.2f %-12.2f %n", names[i], gravity[i], weight[i]);
  60.    
  61.             }
  62.     }
  63.    
  64.  
  65.     public static void main(String[] args)throws IOException
  66.     {
  67.         Scanner kb = new Scanner(System.in);
  68.         double earthWeight;
  69.         System.out.print("Whats your weight on Earth: ");
  70.         earthWeight = kb.nextDouble();
  71.        
  72.        
  73.        
  74.        
  75.         String[] names = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
  76.         double[] gravity = getGravity();                            // static method you write
  77.         double[] weight = calcWeight(earthWeight, gravity);  // static method you write
  78.  
  79.         printResults(names, gravity, weight);                   // static method you write
  80.        
  81.         kb.close(); //Close scanner.
  82.  
  83.     } //end main
  84. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement