Time to combine everything from this section. Build a program that converts between miles and kilometers using the right variable types.
Your program should:
- Declare a
finalconstant for the conversion rate ( mile = km) - Store the input value in a
double - Calculate the converted value
- Build an output
Stringusing concatenation - Print the result
final double MILES_TO_KM = 1.60934;
double miles = 26.2;
double kilometers = miles * MILES_TO_KM;
String output = miles + " miles = " + kilometers + " km";
System.out.println(output);
Extend it yourself: add a boolean flag called toMetric. When true, convert miles to kilometers. When false, convert the other direction by dividing instead of multiplying.