Help with beginner Java HW! Compiling Error!
Go to solution
Solved by IvanSnipedYu,
K looks like i got it to work. I forgot to convert to radian!
import java.util.Scanner;
public class HW04P01{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please enter the coordinates of four city in clockwise order!");
System.out.print("Enter the cordinates of the first city: ");
double xx1 = input.nextDouble();
double yy1 = input.nextDouble();
System.out.print("Enter the cordinates of the second city: ");
double xx2 = input.nextDouble();
double yy2 = input.nextDouble();
System.out.print("Enter the cordinates of the third city: ");
double xx3 = input.nextDouble();
double yy3 = input.nextDouble();
System.out.print("Enter the cordinates of the fourth city: ");
double xx4 = input.nextDouble();
double yy4 = input.nextDouble();
//convert to radian
double x1 = (Math.toRadians(xx1));
double x2 = (Math.toRadians(xx2));
double x3 = (Math.toRadians(xx3));
double x4 = (Math.toRadians(xx4));
double y1 = (Math.toRadians(yy1));
double y2 = (Math.toRadians(yy2));
double y3 = (Math.toRadians(yy3));
double y4 = (Math.toRadians(yy4));
//distance=(radius)arccos(sin(x1)sin(x2)+cos(x1)cos(x2)cos(y1−y2))
double distanceOne = ((6371.01 * Math.acos((Math.sin(x1) * Math.sin(x2)) + (Math.cos(x1) * Math.cos(x2) * Math.cos(y1 - y2)))));
double distanceTwo = ((6371.01 * Math.acos((Math.sin(x2) * Math.sin(x3)) + (Math.cos(x2) * Math.cos(x3) * Math.cos(y2 - y3)))));
double distanceThree = ((6371.01 * Math.acos((Math.sin(x3) * Math.sin(x4)) + (Math.cos(x3) * Math.cos(x4) * Math.cos(y3 - y4)))));
double distanceFour = ((6371.01 * Math.acos((Math.sin(x4) * Math.sin(x1)) + (Math.cos(x4) * Math.cos(x1) * Math.cos(y4 - y1)))));
double distanceFive = ((6371.01 * Math.acos((Math.sin(x2) * Math.sin(x4)) + (Math.cos(x2) * Math.cos(x4) * Math.cos(y2 - y4)))));
double s = ((distanceOne + distanceFive + distanceFour) / 2);
double areaOfTriangle = (Math.sqrt(s * (s - distanceOne) * (s - distanceFive) * (s - distanceFour)));
double sTwo = ((distanceTwo + distanceThree + distanceFive) / 2);
double areaOfTriangleTwo = (Math.sqrt(sTwo * (sTwo - distanceTwo) * (sTwo - distanceThree) * (sTwo - distanceFive)));
double area = (areaOfTriangle + areaOfTriangleTwo);
System.out.printf("The area is: " + "%.3f", area);
}
}

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now