160k views
3 votes
Write a program that takes as input a number of kilometers and prints the corresponding number of nautical miles. Use the following approximations: A kilometer represents 1/10,000 of the distance between the North Pole and the equator. There are 90 degrees, containing 60 minutes of arc each, between the North Pole and the equator. A nautical mile is 1 minute of an arc.

1 Answer

4 votes

Answer:

import java.util.Scanner;

public class NauticalMiles{

public static void main (String [ ] args){

Scanner input = new Scanner(System.in);

System.out.println("Please enter the number of kilometers");

double km = input.nextDouble();

double nm = 0.54 * km;

System.out.println("The corresponding number of nautical miles is " + nm);

}

}

Step-by-step explanation:

1 => Pre-code analysis

According to the question,

(a) A nautical mile (nm) is 1 minute of an arc.

=> 1 nm = 1 minute of an arc

(b)There are 90 degrees, each containing 60 minutes of arc, between the North pole and the equator.

=> Each of the degrees in 90 degrees has 60 minutes of arc

=>
1^(0) = 60 minutes of arc

=>
90^(0) = 90 x 60 minutes of arc

=>
90^(0) = 5400 minutes of arc

(c) A kilometer (km) represents 1/10,000 of the distance between the North pole and the equator.

=> A kilometer = (1 / 10000) x distance between the North pole and equator.

Remember that the distance between the North pole and the equator as shown in (b) above is 90 degrees which is equal to 5400 minutes of arc.

=> 1 km = (1 / 10000) x 5400 minutes of arc

=> 1 km = (1 / 10000) x 5400 x 1 minute of arc

As shown in (a) above, 1 minute of arc is 1 nautical mile(nm).

=> 1 km = (1 / 10000) x 5400 x 1 nm

=> 1 km = 0.54 nm.

=========================================================

2 => The code analysis

The code has been written in Java.

The following is the line-by-line explanation of the code written as comments.

// Import the scanner class to allow for user's inputs

import java.util.Scanner;

// Declare the main class to hold and run the application

public class NauticalMiles {

// Write the main method where execution will begin

public static void main (String [ ] args) {

// Create an object input of the Scanner class

Scanner input = new Scanner(System.in);

// Prompt the user to enter the number of kilometers

System.out.println("Please enter the number of kilometers");

// Store the user's input in a double variable called km

double km = input.nextDouble();

// Convert the input to nautical miles using the formula from the

// pre-code analysis above. i.e 1km = 0.54nm.

// Store the result in a double variable called nm

double nm = 0.54 * km;

// Print out the result (the nautical mile)

System.out.println("The corresponding number of nautical miles is " + nm);

} // End of main method

} // End of class declaration

=========================================================

3 => Sample output

> Please enter the number of kilometers

>> 45

> The corresponding number of nautical miles is 24.3

User Cherrylyn
by
3.8k points