-
Notifications
You must be signed in to change notification settings - Fork 6
Distance
The Distance class is used to represent distance in the program. Its base unit is meters, but can be converted to any distance type (i.e. inches, feet, etc.).
DistanceUnits, the enum that has the distance types, implements iUnitType. See iUnitType for more information about the methods available.
There are 8 distance types:
- Meters
- Centimeters
- Millimeters
- Kilometers
- Inches
- Feet
- Yards
- Miles
The Distance class can be created in two different ways, directly from a meter value (the base unit), or from any Distance Unit.
Distance oneMeter = new Distance(1); // Create a distance from a meter value
Distance tenInches = new Distance(10, DistanceUnits.INCH); // Create a distance from a inch value
Distance implements the Operable interface, giving it methods to add, subtract, multiply, and divide. These are not mutator methods, meaning both original units do not get modified ** NOTE ** Multiplication and division scale the distance, they do not create m^2 or a value. i.e. A distance of 2 meters, multiplied by another distance of 5 meters, will return a distance of 10 meters, not 10 m^2
Distance twoMeters = new Distance(2); // Create a distance of two meters
Distance fiveMeters = new Distance(5); // Create a distance of five meters
Distance added = twoMeters.add(fiveMeters); // Add two and five meters, getting a distance of seven meters
Distance subtracted = fiveMeters.subtract(twoMeters); // Subtract two meters from five meters, getting a distance of three meters
Distance multiplied = twoMeters.multiply(fiveMeters); // Multiply two meters by a scale of five, getting a distance of ten meters
Distance divided = twoMeters.divide(fiveMeters); // Divide two meters by a scale of five, getting a distance of 0.4 meters