Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"robot-name",
"meetup",
"phone-number",
"grade-school"
"grade-school",
"space-age"
],
"deprecated": [

],
"ignored": [
"_template",
Expand Down
11 changes: 11 additions & 0 deletions space-age/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.10"
}
69 changes: 69 additions & 0 deletions space-age/example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
public class SpaceAge {

private enum Planet {
EARTH, MERCURY, VENUS, MARS, JUPITER, SATURN, URANUS, NEPTUNE
}

private double seconds;

private static final double EARTH_ORBITAL_PERIOD_IN_SECONDS = 31557600.0;
private static final int PRECISION = 2;
private static final Map<Planet, Double> relativeOrbitalPeriods = new HashMap<>();

static {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could fold this into the Planet enum itself. (Oddly enough, it's in the official tutorial :-)

https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

relativeOrbitalPeriods.put(Planet.EARTH, 1.0);
relativeOrbitalPeriods.put(Planet.MERCURY, 0.2408467);
relativeOrbitalPeriods.put(Planet.VENUS, 0.61519726);
relativeOrbitalPeriods.put(Planet.MARS, 1.8808158);
relativeOrbitalPeriods.put(Planet.JUPITER, 11.862615);
relativeOrbitalPeriods.put(Planet.SATURN, 29.447498);
relativeOrbitalPeriods.put(Planet.URANUS, 84.016846);
relativeOrbitalPeriods.put(Planet.NEPTUNE, 164.79132);
}

public SpaceAge(double seconds) {
this.seconds = seconds;
}

public double getSeconds() {
return seconds;
}

public double onEarth() {
return calculateAge(relativeOrbitalPeriods.get(Planet.EARTH));
}

public double onMercury() {
return calculateAge(relativeOrbitalPeriods.get(Planet.MERCURY));
}

public double onVenus() {
return calculateAge(relativeOrbitalPeriods.get(Planet.VENUS));
}

public double onMars() {
return calculateAge(relativeOrbitalPeriods.get(Planet.MARS));
}

public double onJupiter() {
return calculateAge(relativeOrbitalPeriods.get(Planet.JUPITER));
}

public double onSaturn() {
return calculateAge(relativeOrbitalPeriods.get(Planet.SATURN));
}

public double onUranus() {
return calculateAge(relativeOrbitalPeriods.get(Planet.URANUS));
}

public double onNeptune() {
return calculateAge(relativeOrbitalPeriods.get(Planet.NEPTUNE));
}

private double calculateAge(double relativeOrbitalPeriod) {
double age = seconds / (EARTH_ORBITAL_PERIOD_IN_SECONDS * relativeOrbitalPeriod);

return new BigDecimal(age).setScale(PRECISION, BigDecimal.ROUND_HALF_UP).doubleValue();
}
}
Empty file added space-age/src/main/java/.keep
Empty file.
2 changes: 2 additions & 0 deletions space-age/src/main/java/SpaceAge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class SpaceAge {
}
76 changes: 76 additions & 0 deletions space-age/src/test/java/SpaceAgeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import org.junit.Test;

public class SpaceAgeTest {

private static final double MAXIMUM_DELTA = 1E-02;

@Test
public void ageInSeconds() {
SpaceAge age = new SpaceAge(1000000);

assertEquals(age.getSeconds(), 1000000, MAXIMUM_DELTA);
}

@Test
public void ageOnEarth() {
SpaceAge age = new SpaceAge(1000000000);

assertEquals(age.onEarth(), 31.69, MAXIMUM_DELTA);
}

@Test
public void ageOnMercury() {
SpaceAge age = new SpaceAge(2134835688);

assertEquals(age.onEarth(), 67.65, MAXIMUM_DELTA);
assertEquals(age.onMercury(), 280.88, MAXIMUM_DELTA);
}

@Test
public void ageOnVenus() {
SpaceAge age = new SpaceAge(189839836);

assertEquals(age.onEarth(), 6.02, MAXIMUM_DELTA);
assertEquals(age.onVenus(), 9.78, MAXIMUM_DELTA);
}

@Test
public void ageOnMars() {
SpaceAge age = new SpaceAge(2329871239L);

assertEquals(age.onEarth(), 73.83, MAXIMUM_DELTA);
assertEquals(age.onMars(), 39.25, MAXIMUM_DELTA);
}

@Test
public void ageOnJupiter() {
SpaceAge age = new SpaceAge(901876382);

assertEquals(age.onEarth(), 28.58, MAXIMUM_DELTA);
assertEquals(age.onJupiter(), 2.41, MAXIMUM_DELTA);
}

@Test
public void ageOnSaturn() {
SpaceAge age = new SpaceAge(3000000000L);

assertEquals(age.onEarth(), 95.06, MAXIMUM_DELTA);
assertEquals(age.onSaturn(), 3.23, MAXIMUM_DELTA);
}

@Test
public void ageOnUranus() {
SpaceAge age = new SpaceAge(3210123456L);

assertEquals(age.onEarth(), 101.72, MAXIMUM_DELTA);
assertEquals(age.onUranus(), 1.21, MAXIMUM_DELTA);
}

@Test
public void ageOnNeptune() {
SpaceAge age = new SpaceAge(8210123456L);

assertEquals(age.onEarth(), 260.16, MAXIMUM_DELTA);
assertEquals(age.onNeptune(), 1.58, MAXIMUM_DELTA);
}
}