You have a map that has a fractional scale of 1:24,000 meaning that 1 unit on th
ID: 3756235 • Letter: Y
Question
You have a map that has a fractional scale of 1:24,000 meaning that 1 unit on the map is equal to 24,000 units on the ground. You are planning a hike on the map, and want to know how many miles you will walk on the ground. Design and implement a program in Java that reads from the user the length of a hiking trail on the map, and prints to the screen the equivalent number of miles on the ground. You may assume that the user will enter a non-negative number, and that 1 inch on the map 24,000 inches on the ground, 12 inches-1 foot, and 5280 feet-1 mile. Each time you run your program, you will test your program on a different input. Include among your tests the following inputs: 0 and 18.5. For the input of 18.5 inches on the map, your program should output that the equivalent distance on the ground is 7.0 miles. Make sure to 1) prompt the user what they need to enter (i.e. number of inches on the map), 2) hand in the output of all your tests, in addition to the source code, 3) use appropriately named variables to store the input value and computed result(s), and 4) use appropriately named constants (i.e. descriptive and following the naming conventions we discussed) to store the conversion factors, e.g. final int INCHES PER FOOT 12;Explanation / Answer
import java.util.Scanner;
public class HikeOnMapToGround {
private static final int INCHES_PER_FOOT = 12;
private static final int FEET_PER_MILE = 5280;
private static final int INCHES_ON_GROUND_PER_MAP = 24000;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please Enter hike on map in inches...");
try {
// Getting hike on map in inches
double inches_on_map = in.nextDouble();
// Converting hike on ground in inches
double inches_on_ground = inches_on_map * INCHES_ON_GROUND_PER_MAP;
// Converting hike on ground in feet
double feet_on_ground = inches_on_ground / INCHES_PER_FOOT;
// Converting hike on ground in miles
double miles_on_ground = feet_on_ground / FEET_PER_MILE;
// Displayed hike on ground in Miles
System.out.println("Miles on Ground : " + String.format("%.1f", miles_on_ground));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.