Ruby
3 credits
Distance class created | 10 |
Distance class can store values as Miles or Kilometers | 10 |
Addition can be performed correctly | 20 |
Subtraction can be performed correctly | 20 |
Multiplication can be performed correctly | 20 |
Division can be performed correctly | 20 |
Total | 100 pts |
To practice working with classes and operators
Create a class called Distance whose objects can store a distance measured in either miles or kilometers. Set up your Distance class so its objects can be added, subtracted, multiplied, or divided by other distance objects.
When the two distance objects that are being worked with are not in the same units a conversion will have to take place. To convert miles to kilometers multiple the value by 1.60934 and to convert kilometers to miles multiple the value by 0.621371
Your distance class is correct when it can run the attached code and produce the following output:
Adding:
1.621371 Miles
4 Miles
4
m
6 Kilometers
10.43736 Kilometers
------------------------------
Subtracting:
0.378629 Miles
0 Miles
0
m
0 Kilometers
-2.43736 Kilometers
------------------------------
Multiplying:
0.621371 Miles
4 Miles
4
m
9 Kilometers
25.74944 Kilometers
------------------------------
Dividing:
1.6093444978925633 Miles
1 Miles
1
m
1 Kilometers
0.6213727366498067 Kilometers