Thank you for all your help with this. But I can’t quite get it to work. I have got it down to one error!
Same folder structure as above, namely:
myapp
├── Main.java
└── GFG4
└── GFG.java
└── points
└── ObtainPoints.java
Main.java:
import points.*;
import GFG4.*;
public class Main {
public static void main(String[] args) {
ObtainPoints point0 = new ObtainPoints();
point0.displayPrompt(point0);
HashMap<String,Double> usersPoints = point0.getCoordinate();
//System.out.println("Start latitude is " + lat1 + System.lineSeparator());
System.out.println("Back in Main");
// Print keys and values of HashMap object usersPoints
for (String i : usersPoints.keySet()) {
System.out.println("key: " + i + " value: " + usersPoints.get(i));
}
}
}
GFG.java:
package GFG4;
// Java program to calculate distance between two points on Earth
import java.util.*;
import java.lang.*;
public class GFG {
public static double distance(double lat1,
double lat2, double lon1,
double lon2)
{
// The math module contains a function named toRadians which converts from degrees to radians.
lon1 = Math.toRadians(lon1);
lon2 = Math.toRadians(lon2);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
// Haversine formula to claculate distance from the GPS coordinates
double dlon = lon2 - lon1;
double dlat = lat2 - lat1;
double a = Math.pow(Math.sin(dlat / 2), 2)
+ Math.cos(lat1) * Math.cos(lat2)
* Math.pow(Math.sin(dlon / 2),2);
double c = 2 * Math.asin(Math.sqrt(a));
// Radius of earth in miles. Use 6371 for km
double r = 3956;
// calculate the result
return(c * r);
}
/*
// driver code
public static void main(String[] args)
{
double lat1 = 53.32055555555556;
double lat2 = 53.31861111111111;
double lon1 = -1.7297222222222221;
double lon2 = -1.6997222222222223;
System.out.println(distance(lat1, lat2,
lon1, lon2) + " K.M");
}
*/
}
// This code is contributed by Prasad Kshirsagar
ObtainPoints.java:
package points;
import java.util.Scanner;
import java.util.HashMap;
public class ObtainPoints {
//create a HashMap object that will contain the coordinates entered by the user
HashMap<String, Double> usersPoints = new HashMap<String, Double>();
// I don't think this next line is applicable any longer
//private double coordinate;
public HashMap<String,Double> getCoordinate() {
return usersPoints; //usersPoints;
}
public void setCoordinate(HashMap<String,Double> newCoordinate) {
this.usersPoints = newCoordinate;
}
public void displayPrompt(ObtainPoints points) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter start latitude:");
//create a variable, lat1, with the value that was entered by the user
double lat1 = scanner.nextDouble();
// Add key and value to the HashMap object usersPoints
usersPoints.put("lat1", lat1);
Scanner scanner1 = new Scanner(System.in);
System.out.println("Enter start longitude:");
//create a variable, lat1, with the value that was entered by the user
double lon1 = scanner1.nextDouble();
// Add key and value to the HashMap object usersPoints
usersPoints.put("lon1", lon1);
//I'm not sure aboout this line - I think I may be doing something wrong here
//points.usersPoints = coords;
// Print keys and values of HashMap object usersPoints
for (String i : usersPoints.keySet()) {
System.out.println("key: " + i + " value: " + usersPoints.get(i));
}
}
}
I have only put routines to get the coordinates of the firt point, so far, to see if I can get the latitude and longitude for the first point.
ObtainPoints.java and GFG.java compile with no errors. But when compiling main.java I get the following error:
main.java:9: error: cannot find symbol
HashMap<String,Double> usersPoints = point0.getCoordinate();
^
symbol: class HashMap
location: class Main
1 error
I clearly do not have a deep enough understanding here of what I am doing. Am I making a simple error or am I doing it all wrong (just aiming to get a simple console based routine that asks the user for coordinates and calculates the distance between them.