iWE HAVE TO WRITE A PROGRAM TO COMPUTE THE DISTANCE BETWEEN TWO POINTS AND DIREC
ID: 667214 • Letter: I
Question
iWE HAVE TO WRITE A PROGRAM TO COMPUTE THE DISTANCE BETWEEN TWO POINTS AND DIRECTION FROM ONE POINT TO OTHER. i WROTE THIS BUT ITS NOT DISPLAYING ANYTHING What is wrong with following code, its not running :
import java.util.*;
public class ComputeDirections {
static int findQuadrant(double x1, double y1, double x2, double y2) {
if (x2 > x1 && y2 >= y1) {
return 1;
} else if (x2 <= x1 && y2 > y1) {
return 2;
} else if (x2 < x1 && y2 <= y1) {
return 3;
} else {
return 4;
}
}
static double distance(double x1, double y1, double x2, double y2) {
double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return Math.round(d * 100.0) / 100.0;
}
static String compDirection(double angle, int quadrant){
String direction = null;
if (quadrant==1 && angle<=11.25)
return direction="East";
else if (quadrant==1 && angle>=11.25 && angle<=33.75)
return direction="EastNorthEast";
else if (quadrant==1 && angle>=33.75&& angle<=56.25)
return direction= "NorthEast";
else if (quadrant==1 && angle>=56.25 && angle <=78.75)
return direction= "NorthNorthEast";
else if (quadrant==1 && angle>=78.75 && angle <=90.00)
return direction= "North";
else if (quadrant==2 && angle<=11.25)
return direction=" North" ;
else if (quadrant==2 && angle>=11.25&&angle<=33.75)
return direction="NorthNorthWest";
else if (quadrant==2 && angle>=33.75&&angle<=56.25)
return direction= "NorthWest";
else if (quadrant==2 && angle>=56.25 && angle <=78.75)
return direction= "WestNorthWest";
else if (quadrant==2 && angle>=78.75 && angle <=90.00)
return direction ="West";
else if (quadrant==3 && angle<=11.25)
return " West";
else if (quadrant==3 && angle>=11.25&&angle<=33.75)
return direction= "WestSouthWest";
else if (quadrant==3 && angle>=33.75&&angle<=56.25)
return direction= "SouthWest";
else if (quadrant==3 && angle>=56.25 && angle <=78.75)
return direction= "SouthSouthWest";
else if (quadrant==3 && angle>=78.75 && angle <=90.00)
return direction= "South";
else if (quadrant==4 && angle<=11.25)
return direction =" South";
else if (quadrant==4 && angle>=11.25&&angle<=33.75)
return direction= "SouthSouthEast";
else if (quadrant==4 && angle>=33.75&&angle<=56.25)
return direction= "SouthEast";
else if (quadrant==4 && angle>=56.25 && angle <=78.75)
return direction ="EastSouthEast";
else if (quadrant==4 && angle>=78.75 && angle <=90.00)
return direction= "East";
return direction;
}
static double compAngle(double side1, double side2, double side3) {
double a = side1;
double b = side2;
double c = side3;
double A;
double B;
double C;
A = Math.acos((b * b + c * c - a * a) / (2.0 * b * c)); // law of
// cosines
B = Math.acos((a * a + c * c - b * b) / (2.0 * a * c));
C = Math.acos((a * a + b * b - c * c) / (2.0 * a * b));
return side1;
}
public static void main(String[] args) {
double x2, x1, y2, y1, x3, y3;
double distance;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the x coordinate for point 1: ");
x1 = scan.nextDouble();
System.out.println("Enter the y coordinate for point 1: ");
y1 = scan.nextDouble();
System.out.println("Enter the x coordinate for point 2: ");
x2 = scan.nextDouble();
System.out.println("Enter the y coordinate for point 2: ");
y2 = scan.nextDouble();
int quadrant = findQuadrant(x1, y1, x2, y2);
if (quadrant == 1) {
x3 = x2;
y3 = y1;
} else if (quadrant == 2) {
x3 = x1;
y3 = y1;
} else if (quadrant == 3) {
x3 = x2;
y3 = y1;
} else if (quadrant == 4) {
x3 = x1;
y3 = y2;
double c = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
double a = Math.sqrt((x3 - x2) * (x2 - x1) + (y3 - y1) * (y3 - y1));
double b = Math.sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y2 - y1));
double angle = compAngle(a, b, c);
String direction = compDirection(angle, quadrant);
System.out.println("The direction tavelled from first point to second points is " + direction
+ " .");
distance = distance(x1, y1, x2, y2);
System.out.println("The distance between the two points is " + distance
+ " .");
}
}
}
Explanation / Answer
use scan.hasNext() while reading the data
the code is as-
import java.util.*;
import java.util.Arrays;
public class ComputeDirections
{
static int findQuadrant(double x1, double y1, double x2, double y2)
{
if (x2 > x1 && y2 >= y1)
{
return 1;
}
else if (x2 <= x1 && y2 > y1)
{
return 2;
}
else if (x2 < x1 && y2 <= y1)
{
return 3;
}
else
{
return 4;
}
}
static double distance(double x1, double y1, double x2, double y2)
{
double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return Math.round(d * 100.0) / 100.0;
}
static String compDirection(double angle, int quadrant)
{
String direction = null;
if (quadrant==1 && angle<=11.25)
return direction="East";
else if (quadrant==1 && angle>=11.25 && angle<33.75)
return direction="EastNorthEast";
else if (quadrant==1 && angle>=33.75&& angle<56.25)
return direction= "NorthEast";
else if (quadrant==1 && angle>=56.25 && angle <78.75)
return direction= "NorthNorthEast";
else if (quadrant==1 && angle>=78.75 && angle <=90.00)
return direction= "North";
else if (quadrant==2 && angle<11.25)
return direction=" North" ;
else if (quadrant==2 && angle>=11.25&&angle<33.75)
return direction="NorthNorthWest";
else if (quadrant==2 && angle>=33.75&&angle<56.25)
return direction= "NorthWest";
else if (quadrant==2 && angle>=56.25 && angle <78.75)
return direction= "WestNorthWest";
else if (quadrant==2 && angle>=78.75 && angle <=90.00)
return direction ="West";
else if (quadrant==3 && angle<11.25)
return " West";
else if (quadrant==3 && angle>=11.25&&angle<33.75)
return direction= "WestSouthWest";
else if (quadrant==3 && angle>=33.75&&angle<56.25)
return direction= "SouthWest";
else if (quadrant==3 && angle>=56.25 && angle <78.75)
return direction= "SouthSouthWest";
else if (quadrant==3 && angle>=78.75 && angle <90.00)
return direction= "South";
else if (quadrant==4 && angle<11.25)
return direction =" South";
else if (quadrant==4 && angle>=11.25&&angle<33.75)
return direction= "SouthSouthEast";
else if (quadrant==4 && angle>=33.75&&angle<56.25)
return direction= "SouthEast";
else if (quadrant==4 && angle>=56.25 && angle <78.75)
return direction ="EastSouthEast";
else if (quadrant==4 && angle>=78.75 && angle <=90.00)
return direction= "East";
return direction ;
}
static double compAngle(double side1, double side2, double side3)
{
double a = side1;
double b = side2;
double c = side3;
double A;
double B;
double C;
A = Math.acos((b * b + c * c - a * a) / (2.0 * b * c)); // law of
// cosines
B = Math.acos((a * a + c * c - b * b) / (2.0 * a * c));
C = Math.acos((a * a + b * b - c * c) / (2.0 * a * b));
return side1;
}
public static void main(String[] args)
{
double x2, x1, y2, y1, x3, y3;
double[] arr = new double[4];
double distance;
Scanner scan = new Scanner(System.in);
ComputeDirections cd=new ComputeDirections();
for(int i=0; i<4 &&scan.hasNext(); i++)
{
System.out.println("Enter the coordinate");
arr[i]=scan.nextDouble();
}
x1=arr[0];
x2=arr[1];
y1=arr[2];
y2=arr[3];
int quadrant = cd.findQuadrant(x1, y1, x2, y2);
if (quadrant == 1)
{
x3 = x2;
y3 = y1;
}
else if (quadrant == 2)
{
x3 = x1;
y3 = y1;
}
else if (quadrant == 3)
{
x3 = x2;
y3 = y1;
}
else
{
x3 = x1;
y3 = y2;
}
double c = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
double a = Math.sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1));
double b = Math.sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y2 - y1));
double angle = cd.compAngle(a, b, c);
String direction =cd.compDirection(angle, quadrant);
System.out.println("The direction tavelled from first point to second points is " + direction
+ " .");
distance = cd.distance(x1, y1, x2, y2);
System.out.println("The distance between the two points is " + distance
+ " .");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.