Creating a Measurement

The following Java code creates a measurement that represents one meter in length.

import org.xspace.mx.Measurement;
import org.xspace.mx.physics.immutable.*;

...

Measurement mx = new Meter();
System.out.println("mx: " + mx);

... which produces the following output ...

mx: 1.0 [ Length^1.0 ]

Notice that the default string representation of the measurement displays measurements as a multiple of "standard units" of length, time, mass, etc. This specific implementation of the physical measurements stores values as multiples of meters, seconds, kilograms, etc. Had this sample code created a measurement representing one foot instead of one meter, observe the difference in the output.

import org.xspace.mx.Measurement;
import org.xspace.mx.physics.immutable.*;

...

Measurement x = new Foot();
System.out.println("mx: " + mx);

... which produces the following output ...

mx: 0.3048 [ Length^1.0 ]

Converting Measurements

The following Java code demonstrates how to convert between different measurements of time such as day, hours, minutes, and seconds. The function getValue() is used to express the Measurement as a multiple of another Measurement.

Measurement minute = new Minute();
System.out.println("1 Minute = " + minute);
System.out.println("1 Minute as a multiple of seconds: " + minute.getValue(new Second()));

... which produces the following output ...

1 Minute = 60.0 [ Time^1.0 ]
1 Minute as a multiple of seconds: 60.0

For simplicity, one could also use a set of "standard measurements" located in the Units class for simpler but equivalent code.

Measurement minute = new Minute();
System.out.println("1 Minute = " + minute);
System.out.println("1 Minute as a multiple of seconds: " + minute.getValue(Units.s));

... which produces the following output ...

1 Minute = 60.0 [ Time^1.0 ]
1 Minute as a multiple of seconds: 60.0

The next example demonstrates conversions of the measurements of one day.

Measurement day = new Day();
System.out.println("One Day = " + day);
System.out.println("One Day as a multiple of hours: " + day.getValue(Units.hr));
System.out.println("One Day as a multiple of minutes: " + day.getValue(Units.min));

... which produces the following output ...

One Day = 86400.0 [ Time^1.0 ]
One Day as a multiple of hours: 24.0
One Day as a multiple of minutes: 1440.0

Measurement Arithmetic

The following example demonstrates some arithmetic operations possible with measurements.

Measurement mxA = new Kilogram(5);
System.out.println();
System.out.println("mxA: " + mxA);

Measurement mxB = mxA.mult(3);
System.out.println();
System.out.println("mxA: " + mxA);
System.out.println("mxB: " + mxB);

Measurement mxC = mxB.div(2);
System.out.println();
System.out.println("mxA: " + mxA);
System.out.println("mxB: " + mxB);
System.out.println("mxC: " + mxC);

Measurement mxD = new Kilogram(100); // 100 kg
Measurement mxAddition = mxA.plus(mxD);
System.out.println();
System.out.println("mxA: " + mxA);
System.out.println("mxD: " + mxD);
System.out.println("mxAddition: " + mxAddition);

... which produces the following output ...

mxA: 5.0 [ Mass^1.0 ]

mxA: 5.0 [ Mass^1.0 ]
mxB: 15.0 [ Mass^1.0 ]

mxA: 5.0 [ Mass^1.0 ]
mxB: 15.0 [ Mass^1.0 ]
mxC: 7.5 [ Mass^1.0 ]

mxA: 5.0 [ Mass^1.0 ]
mxD: 100.0 [ Mass^1.0 ]
mxAddition: 105.0 [ Mass^1.0 ]

Advanced Measurement Conversions

Measurement conversions are not restricted to any "standard" units or measurements. In fact, the user can convert between any measurements which have the same dimensions. For example, converting between two length measurements is a valid operation; converting between a mass measurement and a time measurement is not a compatible operation.

The following Java code demonstrates measurement conversions of speeds.

// 65 kph
Measurement speed = new Kilometer(65).div(new Hour());
System.out.println("Speed: " + speed);

// Measure the speed in terms of 5 cm/s
Measurement specialSpeed = new Centimeter(5).div(new Second());
System.out.println("5 cm/s: " + specialSpeed);
double value = speed.getValue(specialSpeed);
System.out.println("Speed as a multiple of 5 cm/s: " + value);

... which produces the following output ...

Speed: 18.055555555555557 [ Length^1.0 Time^-1.0 ]
5 cm/s: 0.05 [ Length^1.0 Time^-1.0 ]
Speed as a multiple of 5 cm/s: 361.11111111111114