# ricodu's solution

## to Space Age in the Delphi Pascal Track

Published at Apr 11 2020 · 0 comments
Instructions
Test suite
Solution

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

• Mercury: orbital period 0.2408467 Earth years
• Venus: orbital period 0.61519726 Earth years
• Earth: orbital period 1.0 Earth years, 365.25 Earth days, or 31557600 seconds
• 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 request help from a mentor.

### uSpaceAgeTests.pas

``````unit uSpaceAgeTests;

interface
uses
DUnitX.TestFramework;

const
CanonicalVersion = '1.2.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(2129871239);
expectedAge := 35.88;
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(1821023456);
expectedAge := 0.35;
Assert.AreEqual(expectedAge, MyAge.OnNeptune);
end;

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

procedure SpaceAgeTests.Age_on_uranus;
var
MyAge: ISpaceAge;
expectedAge: double;
begin
MyAge := NewSpaceAge(1210123456);
expectedAge := 0.46;
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 OnJupiter : double;
function OnMars : double;
function OnNeptune : double;
function OnSaturn : double;
function OnUranus : double;
function OnVenus : double;
function OnMercury : double;

property OnJupiter : double read OnJupiter;
property OnMars : double read OnMars;
property OnNeptune : double read OnNeptune;
property OnSaturn : double read OnSaturn;
property OnUranus : double read OnUranus;
property OnVenus : double read OnVenus;
property OnMercury : double read OnMercury;
end;

type
TSpaceAge = class(TInterfacedObject, ISpaceAge)
private
dNumberOfSecondsInYear : Double;

function OnEarth : double;
function OnJupiter : double;
function OnMars : double;
function OnNeptune : double;
function OnSaturn : double;
function OnUranus : double;
function OnVenus : double;
function OnMercury : double;

type
TOrbitalPeriod = record
Planet : String;
PeriodEarthBasedInYears : Double;
end;
const
NumberOfPlanets : Integer = 8;
OrbitalPeriod : array[0..7] of TOrbitalPeriod =
(
(Planet : 'earth'; PeriodEarthbasedInYears : 1),
(Planet : 'jupiter' ; PeriodEarthbasedInYears : 11.862615),
(Planet : 'mars' ; PeriodEarthbasedInYears : 1.8808158),
(Planet : 'neptune' ; PeriodEarthbasedInYears : 164.79132),
(Planet : 'saturn' ; PeriodEarthbasedInYears : 29.447498),
(Planet : 'uranus' ; PeriodEarthbasedInYears : 84.016846),
(Planet : 'venus' ; PeriodEarthbasedInYears : 0.61519726),
(Planet : 'mercury' ; PeriodEarthbasedInYears : 0.2408467)
);
NbSecondsInEarthYear : Double = 31557600;

function GetOrbitalPeriod(sPlanet:string) : double;

constructor Create(dSeconds:Double);
end; // TSpaceAge type

function NewSpaceAge(dSeconds:Double) : ISpaceAge;

implementation

uses Math;

function NewSpaceAge(dSeconds:Double) : ISpaceAge;
begin
result :=  TSpaceAge.Create(dSeconds);
end;

constructor TSpaceAge.Create(dSeconds:Double);
var dInYear : Double;
begin
dNumberOfSecondsInYear := dSeconds / NbSecondsInEarthYear;
end;

function TSpaceAge.OnEarth: Double;
var dInYear : Double;
begin
result := RoundTo(dNumberOfSecondsInYear/GetOrbitalPeriod('earth'),-2);

end;

function TSpaceAge.OnJupiter: Double;
begin

result := RoundTo(dNumberOfSecondsInYear/GetOrbitalPeriod('jupiter'),-2);

end;

function TSpaceAge.OnMars: Double;
begin
result := RoundTo(dNumberOfSecondsInYear/GetOrbitalPeriod('mars'),-2);
end;

function TSpaceAge.OnNeptune: Double;
begin
result := RoundTo(dNumberOfSecondsInYear/GetOrbitalPeriod('neptune'),-2);
end;

function TSpaceAge.OnSaturn: Double;
begin
result := RoundTo(dNumberOfSecondsInYear/GetOrbitalPeriod('saturn'),-2);
end;

function TSpaceAge.OnUranus: Double;
begin
result := RoundTo(dNumberOfSecondsInYear/GetOrbitalPeriod('uranus'),-2);
end;

function TSpaceAge.OnVenus: Double;
begin
result := RoundTo(dNumberOfSecondsInYear/GetOrbitalPeriod('venus'),-2);
end;

function TSpaceAge.OnMercury: Double;
begin
result :=   RoundTo(dNumberOfSecondsInYear/GetOrbitalPeriod('mercury'),-2);
end;

function TSpaceAge.GetOrbitalPeriod(sPlanet:string) : double;
var i : Integer;
dResultat : Double;
begin
dResultat := -1;
for i := 0 to NumberOfPlanets do
begin
if (OrbitalPeriod[i].Planet =  sPlanet) then
begin
dResultat := OrbitalPeriod[i].PeriodEarthbasedInYears;
break;
end;
end;

result := dResultat;
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?