Sha256: 3960d1788ae044ad3f6e920891cb825df4e01280d9c05aaf38498b4daa243f7e

Contents?: true

Size: 1.74 KB

Versions: 167

Compression:

Stored size: 1.74 KB

Contents

import java.math.BigDecimal;

public class SpaceAge {

    private enum Planet {
        EARTH(1.0),
        MERCURY(0.2408467),
        VENUS(0.61519726),
        MARS(1.8808158),
        JUPITER(11.862615),
        SATURN(29.447498),
        URANUS(84.016846),
        NEPTUNE(164.79132);

        private final double relativeOrbitalPeriod;

        Planet(double relativeOrbitalPeriod) {
            this.relativeOrbitalPeriod = relativeOrbitalPeriod;
        }

        public double getRelativeOrbitalPeriod() {
            return relativeOrbitalPeriod;
        }
    }

    private static final double EARTH_ORBITAL_PERIOD_IN_SECONDS = 31557600.0;
    private static final int PRECISION = 2;

    private double seconds;

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

    public double getSeconds() {
        return seconds;
    }

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

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

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

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

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

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

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

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

    private double calculateAge(Planet planet) {
        double age = seconds / (EARTH_ORBITAL_PERIOD_IN_SECONDS * planet.getRelativeOrbitalPeriod());

        return new BigDecimal(age).setScale(PRECISION, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}

Version data entries

167 entries across 167 versions & 1 rubygems

Version Path
trackler-2.1.0.11 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.10 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.9 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.8 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.7 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.6 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.5 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.4 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.3 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.2 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.1 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.1.0.0 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.0.8.55 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.0.8.54 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.0.8.53 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.0.8.52 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.0.8.51 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.0.8.50 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.0.8.49 tracks/java/exercises/space-age/src/example/java/SpaceAge.java
trackler-2.0.8.48 tracks/java/exercises/space-age/src/example/java/SpaceAge.java