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.util.UNITS;
7   import org.djunits.value.ValueRuntimeException;
8   import org.djunits.value.storage.StorageType;
9   import org.djunits.value.vdouble.scalar.Duration;
10  import org.djunits.value.vdouble.scalar.Length;
11  import org.djunits.value.vdouble.scalar.Speed;
12  import org.djunits.value.vdouble.vector.SpeedVector;
13  import org.djunits.value.vdouble.vector.base.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="https://djunits.org/docs/license.html">DJUNITS License</a>.
20   * <p>
21   * @version $Revision: 672 $, $LastChangedDate: 2019-10-18 14:32:01 +0200 (Fri, 18 Oct 2019) $, by $Author: averbraeck $,
22   *          initial version 3 sep. 2015 <br>
23   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
24   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">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 ValueRuntimeException in case of error
38       */
39      public static void main(final String[] args) throws ValueRuntimeException
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.times(duration);
47          System.out.println("distance is " + distance); // prints 2.500e+04m
48          Length finish = new Length(100, KILOMETER);
49          Duration timeToFinish = finish.divide(speed);
50          System.out.println("at speed " + speed + " it will take " + timeToFinish + " to travel " + finish);
51          Speed requiredSpeed = finish.divide(duration);
52          System.out.println("speed required to reach finish at " + finish + " in " + duration + " is "
53                  + requiredSpeed.toString(KM_PER_HOUR));
54          Speed speed1 = new Speed(1.2, SpeedUnit.SI);
55          Speed speed2 = speed1.times(2.0);
56          Speed speed3 = speed1.times(3.0);
57          double[] sv = new double[] {1, 2, 3, 4, 5};
58          SpeedVector speedVector = DoubleVector.instantiate(sv, SpeedUnit.SI, StorageType.DENSE);
59      }
60  
61  }