Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Read date from a text file and does magic(math) with it.
- *
- * @author Bruno Alves
- * @version 8/18/2014
- */
- import java.util.Scanner;
- import java.io.File;
- import java.io.IOException;
- public class Weight
- {
- // Note: formula for finding weight on a planet: Earth weight divided by Gravity constant times surface gravity
- public static double[] getGravity()throws IOException
- {
- File gravityFile = new File("GravityV1.txt");
- Scanner file = new Scanner(gravityFile);
- double[] gravity = new double[9];
- for(int i = 0; file.hasNextDouble();i++)
- {
- gravity[i] = file.nextDouble() / 10;
- }
- file.close();
- return gravity;
- }
- public static double[] calcWeight(double earthWeight, double[] gravity)
- {
- double[] weight = new double[gravity.length];
- for (int i = 0; i < weight.length; i++)
- {
- weight[i] = (earthWeight * 433.59237) / gravity[i];
- weight[i] = weight[i] * 453.592;
- }
- return weight;
- }
- public static void printResults(String[] names, double[] gravity, double[] weight)
- {
- System.out.printf(" My Weight On The Planets%n");
- System.out.printf("Planet Gravity Weight(lbs)%n");
- System.out.printf("*************************************%n");
- for(int i = 0; i < gravity.length; i++)
- {
- System.out.printf("%-10s %-12.2f %-12.2f %n", names[i], gravity[i], weight[i]);
- }
- }
- public static void main(String[] args)throws IOException
- {
- Scanner kb = new Scanner(System.in);
- double earthWeight;
- System.out.print("Whats your weight on Earth: ");
- earthWeight = kb.nextDouble();
- 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
- kb.close(); //Close scanner.
- } //end main
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement