Answer to Question #290914 in Java | JSP | JSF for dhana

Question #290914

Your task is to find the sum of the length of all lines (in kms) that can be drawn between co-ordinates of these countries.

  • Assume radius of earth: 6371 km
  • Round length of each line and final result to 2 decimal points
  • If co-ordinates are missing for any country use 0.000 N 0.000 E
1
Expert's answer
2022-01-26T15:28:27-0500
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        int r = 6371000;// metres
        ArrayList<Country> countries = new ArrayList<>();
        double total = 0;
        for (int i = 0; i < countries.size(); i++) {
            for (int j = i + 1; j < countries.size(); j++) {
                double fi1 = countries.get(i).getLatitude() * Math.PI / 180; // φ, λ in radians
                double fi2 = countries.get(j).getLatitude() * Math.PI / 180;
                double dFi = (countries.get(j).getLatitude() - countries.get(i).getLatitude()) * Math.PI / 180;
                double dLam = (countries.get(j).getLongitude() - countries.get(i).getLongitude()) * Math.PI / 180;
                double a = Math.sin(dFi / 2) * Math.sin(dFi / 2) +
                        Math.cos(fi1) * Math.cos(fi2) * Math.sin(dLam / 2) * Math.sin(dLam / 2);
                double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
                total += r * c;
            }
        }
        System.out.printf("Total distance: %.2f\n", total / 1000);
    }
}

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
New on Blog
APPROVED BY CLIENTS