Skip to content

Instantly share code, notes, and snippets.

Created June 27, 2014 11:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/62936cc55c7f46fd574a to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package planetarygravitycalculator;
/**
*
* @author Dan
*/
public class Planet
{
public final double G = 0.0000000000667;
String planetName;
double radius,density,mass,volume;
public Planet (String name,double rad, double den)
{
planetName = name;
radius = rad;
density = den;
this.solveMass();
}
public String getName()
{
return planetName;
}
public double getRadius()
{
return radius;
}
public double getDensity()
{
return density;
}
public double getMass()
{
return mass;
}
public double getVolume()
{
return volume;
}
public void setName(String newName)
{
planetName = newName;
}
public void setRadius(double newRad)
{
radius = newRad;
}
public void setDensity(double newDen)
{
density = newDen;
}
public void solveMass()
{
volume = (4.0/3.0) * Math.PI * Math.pow(radius, 3);
mass = volume * density;
}
public double gravPull(double objMass)
{
double gravPull = G * ((mass * objMass) / (Math.pow(radius,2)));
return gravPull;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package planetarygravitycalculator;
/**
*
* @author Dan
*/
import java.util.*;
import java.text.DecimalFormat;
public class PlanetaryGravityCalculator {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
List<Planet> planetArray = new ArrayList<>();
DecimalFormat df = new DecimalFormat("#.###");
System.out.println("Welcome to the Planetary Gravity Calculator!" + "\n" + "--------------------------------------------");
System.out.println("Enter the mass of an object: ");
double objMass = scan.nextDouble();
System.out.println("Enter how many planets you would like to calculate: ");
int numOfPlanets = scan.nextInt();
for(int i = 0; i < numOfPlanets; i++)
{
System.out.println("Name of planet: ");
scan.nextLine();
String planName = scan.nextLine();
System.out.println("The radius of " + planName +": ");
double planRad = scan.nextDouble();
System.out.println("The average density of " + planName + ": ");
double planDen = scan.nextDouble();
Planet newPlanet = new Planet(planName,planRad,planDen);
planetArray.add(newPlanet);
}
for(Planet p : planetArray)
{
System.out.println(p.getName() + ": " + df.format(p.gravPull(objMass)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment