Jump to content

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);
	
	
	}
}

 

import java.util.Scanner;

public class HW04P01{
	public static void main(String[] args){
	Scanner input = new Scanner(System.in);
	
	System.out.print("Please enter the coordinates of four city in clockwise order!");
	System.out.print("Enter the cordinates of the first city: ");
	double cityOneAlt = input.nextDouble();
	double cityOneLat = input.nextDouble();
	
	System.out.print("Enter the cordinates of the second city: ");
	double cityTwoAlt = input.nextDouble();
	double cityTwoLat = input.nextDouble();
	
	System.out.print("Enter the cordinates of the third city: ");
	double cityThreeAlt = input.nextDouble();
	double cityThreeLat = input.nextDouble();
	
	System.out.print("Enter the cordinates of the fourth city: ");
	double cityFourAlt = input.nextDouble();
	double cityFourLat = input.nextDouble();
	
	double distanceOne = ((6371.01 * acos(sin(cityOneAlt) * sin(cityTwoAlt) + (cos(cityOneAlt) * cos(cityTwoAlt) * cos(cityOneLat - cityTwoLat)))));
	double distanceTwo = ((6371.01 * acos(sin(cityTwoAlt) * sin(cityThreeAlt) + (cos(cityTwoAlt) * cos(cityThreeAlt) * cos(cityTwoLat - cityThreeLat)))));
	double distanceThree = ((6371.01 * acos(sin(cityThreeAlt) * sin(cityFourAlt) + (cos(cityThreeAlt) * cos(cityFourAlt) * cos(cityThreeLat - cityFourLat)))));
	double distanceFour = ((6371.01 * acos(sin(cityFourAlt) * sin(cityOneAlt) + (cos(cityFourAlt) * cos(cityOneAlt) * cos(cityFourLat - cityOneLat)))));
	
	double s = ((distanceOne + distanceTwo + distanceFour) / 2);
	double areaOfTriangle = (Math.sqrt(s * (s - distanceOne) * (s - distanceTwo) * (s - distanceFour)));
	
	double sTwo = ((distanceTwo + distanceThree + distanceFour) / 2);
	double areaOfTriangleTwo = (Math.sqrt(s * (s - distanceTwo) * (s - distanceThree) * (s - distanceFour)));
	
	double area = (areaOfTriangle + areaOfTriangleTwo);
	
	System.out.print("The area is: " + area);
	
	
	}
}

Can someone tell me why this wont compile?

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to comment
https://linustechtips.com/topic/586967-help-with-beginner-java-hw-compiling-error/
Share on other sites

Link to post
Share on other sites

Okay, so i got it to compile. Turns out that i forgot the Math.sin/Math.cos etc. But now I'm getting a logic error, when i input the sample I don't get the right answer :(

Sample output:
Please enter the coordinates in a clockwise order.

Enter the GPS coordinates for the 1st city: 35.2270869 -80.8431267

Enter the GPS coordinates for the 2nd city: 32.0835407 -81.0998342

Enter the GPS coordinates for the 3rd city: 28.5383355 -81.3792365

Enter the GPS coordinates for the 4th city: 33.7489954 -84.3879824

 

The area is: 117863.342

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to post
Share on other sites

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 x1 = input.nextDouble();
	double y1 = input.nextDouble();
	
	System.out.print("Enter the cordinates of the second city: ");
	double x2 = input.nextDouble();
	double y2 = input.nextDouble();
	
	System.out.print("Enter the cordinates of the third city: ");
	double x3 = input.nextDouble();
	double y3 = input.nextDouble();
	
	System.out.print("Enter the cordinates of the fourth city: ");
	double x4 = input.nextDouble();
	double y4 = input.nextDouble();
	
	//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.print("The area is: " + Math.rint(area));
	
	
	}
}

Here's where I'm at right now :(

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to post
Share on other sites

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);
	
	
	}
}

 

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×