Skip to content

Instantly share code, notes, and snippets.

@zantetsuken88
Created July 20, 2018 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zantetsuken88/5805f67417c3a4ffcde5a6b4b863658e to your computer and use it in GitHub Desktop.
Save zantetsuken88/5805f67417c3a4ffcde5a6b4b863658e to your computer and use it in GitHub Desktop.

Space Age Given an age in seconds, calculate how old someone would be on:

Earth: orbital period 365.25 Earth days, or 31557600 seconds Mercury: orbital period 0.2408467 Earth years Venus: orbital period 0.61519726 Earth years Mars: orbital period 1.8808158 Earth years Jupiter: orbital period 11.862615 Earth years Saturn: orbital period 29.447498 Earth years Uranus: orbital period 84.016846 Earth years Neptune: orbital period 164.79132 Earth years So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, go watch this youtube video.

Running the tests You can run all the tests for an exercise by entering

$ gradle test in your terminal.

Source Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise.

class SpaceAge {
private static final double EARTH_SECONDS = 31557600;
private static final double MERCURY_ORBIT = 0.2408467;
private static final double VENUS_ORBIT = 0.61519726;
private static final double MARS_ORBIT = 1.8808158;
private static final double JUPITER_ORBIT = 11.862615;
private static final double SATURN_ORBIT = 29.447498;
private static final double URANUS_ORBIT = 84.016846;
private static final double NEPTUNE_ORBIT = 164.79132;
private double seconds;
SpaceAge(double seconds) {
this.seconds = seconds;
}
private double getSeconds(double orbit) {
return this.seconds / (EARTH_SECONDS * orbit);
}
double onEarth() {
return getSeconds(1);
}
double onMercury() {
return getSeconds(MERCURY_ORBIT);
}
double onVenus() {
return getSeconds(VENUS_ORBIT);
}
double onMars() {
return getSeconds(MARS_ORBIT);
}
double onJupiter() {
return getSeconds(JUPITER_ORBIT);
}
double onSaturn() {
return getSeconds(SATURN_ORBIT);
}
double onUranus() {
return getSeconds(URANUS_ORBIT);
}
double onNeptune() {
return getSeconds(NEPTUNE_ORBIT);
}
}
import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;
public class SpaceAgeTest {
private static final double MAXIMUM_DELTA = 1E-02;
@Test
public void ageOnEarth() {
SpaceAge age = new SpaceAge(1000000000);
assertEquals(31.69, age.onEarth(), MAXIMUM_DELTA);
}
@Ignore("Remove to run test")
@Test
public void ageOnMercury() {
SpaceAge age = new SpaceAge(2134835688);
assertEquals(280.88, age.onMercury(), MAXIMUM_DELTA);
}
@Ignore("Remove to run test")
@Test
public void ageOnVenus() {
SpaceAge age = new SpaceAge(189839836);
assertEquals(9.78, age.onVenus(), MAXIMUM_DELTA);
}
@Ignore("Remove to run test")
@Test
public void ageOnMars() {
SpaceAge age = new SpaceAge(2329871239L);
assertEquals(39.25, age.onMars(), MAXIMUM_DELTA);
}
@Ignore("Remove to run test")
@Test
public void ageOnJupiter() {
SpaceAge age = new SpaceAge(901876382);
assertEquals(2.41, age.onJupiter(), MAXIMUM_DELTA);
}
@Ignore("Remove to run test")
@Test
public void ageOnSaturn() {
SpaceAge age = new SpaceAge(3000000000L);
assertEquals(3.23, age.onSaturn(), MAXIMUM_DELTA);
}
@Ignore("Remove to run test")
@Test
public void ageOnUranus() {
SpaceAge age = new SpaceAge(3210123456L);
assertEquals(1.21, age.onUranus(), MAXIMUM_DELTA);
}
@Ignore("Remove to run test")
@Test
public void ageOnNeptune() {
SpaceAge age = new SpaceAge(8210123456L);
assertEquals(1.58, age.onNeptune(), MAXIMUM_DELTA);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment