View Javadoc
1   package org.djunits.demo.examples;
2   
3   import java.util.Locale;
4   
5   import org.djunits.unit.SpeedUnit;
6   import org.djunits.unit.UNITS;
7   import org.djunits.value.StorageType;
8   import org.djunits.value.ValueException;
9   import org.djunits.value.vdouble.scalar.DoubleScalar;
10  import org.djunits.value.vdouble.scalar.Duration;
11  import org.djunits.value.vdouble.scalar.Length;
12  import org.djunits.value.vdouble.scalar.Speed;
13  import org.djunits.value.vdouble.vector.DoubleVector;
14  
15  /**
16   * This Java code demonstrates multiplication and division using DJUNITS.
17   * <p>
18   * Copyright (c) 2015-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="http://djunits.org/docs/license.html">DJUNITS License</a>.
20   * <p>
21   * @version $Revision: 324 $, $LastChangedDate: 2019-01-18 00:35:01 +0100 (Fri, 18 Jan 2019) $, by $Author: averbraeck $,
22   *          initial version 3 sep. 2015 <br>
23   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
25   */
26  public final class MultiplyAndDivide implements UNITS
27  {
28      /** */
29      private MultiplyAndDivide()
30      {
31          // utility constructor.
32      }
33  
34      /**
35       * Create some scalar values to demonstrate conversion from and to related units.
36       * @param args String[]; the command line arguments; not used
37       * @throws ValueException in case of error
38       */
39      public static void main(final String[] args) throws ValueException
40      {
41          Locale.setDefault(Locale.US); // Ensure that floating point values are printed using a dot (".")
42          Speed speed = new Speed(50, KM_PER_HOUR);
43          Duration duration = new Duration(0.5, HOUR);
44          System.out.println("speed is " + speed); // prints 50.000km/h
45          System.out.println("duration is " + duration); // prints 0.500h
46          Length distance = speed.multiplyBy(duration);
47          System.out.println("distance is " + distance); // prints 2.500e+04m
48          Length finish = new Length(100, KILOMETER);
49          Duration timeToFinish = finish.divideBy(speed);
50          System.out.println("at speed " + speed + " it will take " + timeToFinish + " to travel " + finish);
51          Speed requiredSpeed = finish.divideBy(duration);
52          System.out.println("speed required to reach finish at " + finish + " in " + duration + " is "
53                  + requiredSpeed.toString(KM_PER_HOUR));
54          DoubleScalar.Rel<SpeedUnit> speed1 = new DoubleScalar.Rel<>(1.2, SpeedUnit.SI);
55          DoubleScalar.Rel<SpeedUnit> speed2 = speed1.multiplyBy(2.0);
56          DoubleScalar.Rel<SpeedUnit> speed3 = speed1.multiplyBy(3.0);
57          double[] sv = new double[] { 1, 2, 3, 4, 5 };
58          DoubleVector.Rel<SpeedUnit> speedVector = new DoubleVector.Rel<SpeedUnit>(sv, SpeedUnit.SI, StorageType.DENSE);
59      }
60  
61  }