# nilsojr's solution

## to Space Age in the Delphi Pascal Track

Published at Nov 03 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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.

## Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

If Delphi is properly installed, and `*.dpr` file types have been associated with Delphi, then double clicking the supplied `*.dpr` file will start Delphi and load the exercise/project. `control + F9` is the keyboard shortcut to compile the project or pressing `F9` will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the `File` drop down menu.

### When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

### Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the `exercism/delphi/<exerciseName>` directory.

For example, if you're submitting `ubob.pas` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas`.

## 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 may receive assistance from a mentor.

### uSpaceAgeTests.pas

``````unit uSpaceAgeTests;

interface
uses
DUnitX.TestFramework;

const
CanonicalVersion = '1.1.0';

type

[TestFixture]
SpaceAgeTests = class(TObject)
public
[Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]
procedure Age_on_earth;

[Test]
[Ignore]
procedure Age_on_mercury;

[Test]
[Ignore]
procedure Age_on_venus;

[Test]
[Ignore]
procedure Age_on_mars;

[Test]
[Ignore]
procedure Age_on_jupiter;

[Test]
[Ignore]
procedure Age_on_saturn;

[Test]
[Ignore]
procedure Age_on_uranus;

[Test]
[Ignore]
procedure Age_on_neptune;
end;

implementation
uses uSpaceAge;

{ SpaceAgeTests }

procedure SpaceAgeTests.Age_on_earth;
var
MyAge: ISpaceAge;
expectedAge: double;
begin
MyAge := NewSpaceAge(1000000000);
expectedAge := 31.69;
Assert.AreEqual(expectedAge, MyAge.OnEarth);
end;

procedure SpaceAgeTests.Age_on_jupiter;
var
MyAge: ISpaceAge;
expectedAge: double;
begin
MyAge := NewSpaceAge(901876382);
expectedAge := 2.41;
Assert.AreEqual(expectedAge, MyAge.OnJupiter);
end;

procedure SpaceAgeTests.Age_on_mars;
var
MyAge: ISpaceAge;
expectedAge: double;
begin
MyAge := NewSpaceAge(2329871239);
expectedAge := 39.25;
Assert.AreEqual(expectedAge, MyAge.OnMars);
end;

procedure SpaceAgeTests.Age_on_mercury;
var
MyAge: ISpaceAge;
expectedAge: double;
begin
MyAge := NewSpaceAge(2134835688);
expectedAge := 280.88;
Assert.AreEqual(expectedAge, MyAge.OnMercury);
end;

procedure SpaceAgeTests.Age_on_neptune;
var
MyAge: ISpaceAge;
expectedAge: double;
begin
MyAge := NewSpaceAge(8210123456);
expectedAge := 1.58;
Assert.AreEqual(expectedAge, MyAge.OnNeptune);
end;

procedure SpaceAgeTests.Age_on_saturn;
var
MyAge: ISpaceAge;
expectedAge: double;
begin
MyAge := NewSpaceAge(3000000000);
expectedAge := 3.23;
Assert.AreEqual(expectedAge, MyAge.OnSaturn);
end;

procedure SpaceAgeTests.Age_on_uranus;
var
MyAge: ISpaceAge;
expectedAge: double;
begin
MyAge := NewSpaceAge(3210123456);
expectedAge := 1.21;
Assert.AreEqual(expectedAge, MyAge.OnUranus);
end;

procedure SpaceAgeTests.Age_on_venus;
var
MyAge: ISpaceAge;
expectedAge: double;
begin
MyAge := NewSpaceAge(189839836);
expectedAge := 9.78;
Assert.AreEqual(expectedAge, MyAge.OnVenus);
end;

initialization
TDUnitX.RegisterTestFixture(SpaceAgeTests);
end.``````
``````unit uSpaceAge;

interface

type
ISpaceAge = interface
function OnEarth: Double;
function OnMercury: Double;
function OnVenus: Double;
function OnMars: Double;
function OnJupiter: Double;
function OnSaturn: Double;
function OnUranus: Double;
function OnNeptune: Double;
end;

TSpaceAge = class(TInterfacedObject, ISpaceAge)
private
FDays,
FSeconds: Double;
function Round(AValue: Double): Double;
public
constructor Create(ASeconds: Double);

function OnEarth: Double;
function OnMercury: Double;
function OnVenus: Double;
function OnMars: Double;
function OnJupiter: Double;
function OnSaturn: Double;
function OnUranus: Double;
function OnNeptune: Double;
end;

function NewSpaceAge(ASeconds: Double): ISpaceAge;

const
EARTH_DAYS = 365.25;
MERCURY_EARTH_YEARS = 0.2408467;
VENUS_EARTH_YEARS = 0.61519726;
MARS_EARTH_YEARS = 1.8808158;
JUPITER_EARTHYEARS = 11.862615;
SATURN_EARTH_YEARS = 29.447498;
URANUS_EARTH_YEARS = 84.016846;
NEPTUNE_EARTH_YEARS = 164.79132;

implementation

uses
System.Math;

function NewSpaceAge(ASeconds: Double): ISpaceAge;
begin
Result := TSpaceAge.Create(ASeconds);
end;

{ TSpaceAge }

constructor TSpaceAge.Create(ASeconds: Double);
begin
FDays := (((ASeconds / 60) / 60) / 24);
FSeconds := ASeconds;
end;

function TSpaceAge.OnEarth: Double;
begin
Result := Self.Round(FDays / EARTH_DAYS);
end;

function TSpaceAge.OnJupiter: Double;
begin
Result := Self.Round(OnEarth / JUPITER_EARTHYEARS);
end;

function TSpaceAge.OnMars: Double;
begin
Result := Self.Round(OnEarth / MARS_EARTH_YEARS);
end;

function TSpaceAge.OnMercury: Double;
begin
Result := Self.Round(OnEarth / MERCURY_EARTH_YEARS);
end;

function TSpaceAge.OnNeptune: Double;
begin
Result := Self.Round(OnEarth / NEPTUNE_EARTH_YEARS);
end;

function TSpaceAge.OnSaturn: Double;
begin
Result := Self.Round(OnEarth / SATURN_EARTH_YEARS);
end;

function TSpaceAge.OnUranus: Double;
begin
Result := Self.Round(OnEarth / URANUS_EARTH_YEARS);
end;

function TSpaceAge.OnVenus: Double;
begin
Result := Self.Round(OnEarth / VENUS_EARTH_YEARS);
end;

function TSpaceAge.Round(AValue: Double): Double;
begin
Result := RoundTo(RoundTo(AValue, -3), -2);
end;

end.``````

### What can you learn from this solution?

A huge amount can be learned from reading other peopleโs code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?