Answer to Question #61788 in Java | JSP | JSF for Bill DeShields

Question #61788
Write a class named Distance that contains the following two methods, then write a test program named TestDistance that uses just one loop to generate the table below. TestDistance should run both statiic methods below in the loop.
1
Expert's answer
2016-09-07T09:18:03-0400
// Distance.java
class Distance {

static double toKm(double miles) {
return miles * 1.60934;
}

static double toMiles(double km) {
return km * 0.621371;
}
}


// TestDistance.java

import java.math.BigDecimal;
import java.math.RoundingMode;

public class TestDistance {
public static void main(String[] args) {
System.out.println(" Miles Km Km Miles");
System.out.println(" ----- -- -- -----");

double km, miles;

for (double i = 1; i < 11; i++) {
km = Distance.toKm(i);
miles = Distance.toMiles(i);

km = new BigDecimal(km).setScale(3, RoundingMode.HALF_UP).doubleValue();
miles = new BigDecimal(miles).setScale(3, RoundingMode.HALF_UP).doubleValue();

if (i < 7) {
System.out.println(" " + i + "00 " + km + " | " + i + "00 " + miles);
}
else
if (i < 8)
System.out.println(" " + i + "00 " + km + " | " + i + "00 " + miles + "0");
else
if (i < 10)
System.out.println(" " + i + "00 " + km + " | " + i + "00 " + miles);
else
System.out.println(i + "00 " + km + " | " + i + "00 " + miles);
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS