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-2017 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: 244 $, $LastChangedDate: 2017-04-28 20:04:33 +0200 (Fri, 28 Apr 2017) $, by $Author: averbraeck $, initial version 3 sep. 2015 <br>
22   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
23   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
24   */
25  public final class MultiplyAndDivide implements UNITS
26  {
27      /** */
28      private MultiplyAndDivide()
29      {
30          // utility constructor.
31      }
32      
33      /**
34       * Create some scalar values to demonstrate conversion from and to related units.
35       * @param args String[]; the command line arguments; not used
36       * @throws ValueException in case of error
37       */
38      public static void main(final String[] args) throws ValueException
39      {
40          Locale.setDefault(Locale.US); // Ensure that floating point values are printed using a dot (".")
41          Speed speed = new Speed(50, KM_PER_HOUR);
42          Duration duration = new Duration(0.5, HOUR);
43          System.out.println("speed is " + speed); // prints 50.000km/h
44          System.out.println("duration is " + duration); // prints 0.500h
45          Length distance = speed.multiplyBy(duration);
46          System.out.println("distance is " + distance); // prints 2.500e+04m
47          Length finish = new Length(100, KILOMETER);
48          Duration timeToFinish = finish.divideBy(speed);
49          System.out.println("at speed " + speed + " it will take " + timeToFinish + " to travel " + finish);
50          Speed requiredSpeed = finish.divideBy(duration);
51          System.out.println("speed required to reach finish at " + finish + " in " + duration + " is "
52                  + requiredSpeed.toString(KM_PER_HOUR));
53          DoubleScalar.Rel<SpeedUnit> speed1 = new DoubleScalar.Rel<>(1.2, SpeedUnit.SI);
54          DoubleScalar.Rel<SpeedUnit> speed2 = speed1.multiplyBy(2.0); 
55          DoubleScalar.Rel<SpeedUnit> speed3 = speed1.multiplyBy(3.0);
56          double[] sv = new double[] {1, 2, 3, 4, 5}; 
57          DoubleVector.Rel<SpeedUnit> speedVector = new DoubleVector.Rel<SpeedUnit>(sv, SpeedUnit.SI, StorageType.DENSE);
58      }
59  
60  }