Find latitude and longitude of utmost 20 countries, ordered by population, with a population greater or equal to the population limit given below and have atleast one currency exclusively for themselves. (countries like Madagascar, Sri Lanka but not India, USA). Use the country details from this dataset.
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
Population limit: 57100
Answers
Answer:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
int populationLimit = 31028700;
int r = 6371000;// metres
ArrayList<Country> allCountries = new ArrayList<>();
ArrayList<Country> countries = new ArrayList<>();
for (Country country : allCountries) {
if (country.getPopulation() >= populationLimit && country.getNumberOfCurencies() > 1) {
countries.add(country);
if (countries.size() == 20) {
break;
}
}
}
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);
Answer:
I don't know why everyone is ignoring the phrase "have atleast one currency exclusively for themselves. (countries like Madagascar, Sri Lanka but not India, USA)"
Explanation:
I have searched this question through the whole web but still can't find the right answer where someone is looking after the phrase "have atleast one currency exclusively for themselves. (countries like Madagascar, Sri Lanka but not India, USA)"
and please use the dataset in the code "https://cdn.jsdelivr.net/gh/apilayer/restcountries@3dc0fb110cd97bce9ddf27b3e8e1f7fbe115dc3c/src/main/resources/countriesV2.json"
And the code provided here is copied from another website