Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have included the whole lab, but the part I need help on is coding String CompDirection using Table II (it is bolded below for

I have included the whole lab, but the part I need help on is coding String CompDirection using Table II (it is bolded below for easier reading)- please include comments and keep it as simple as possible because I am a beginner and need/want to understand. Thank you! Also, feel free to code the whole program, but again the part I can't figure out at all is the String CompDirection using Table II.

For this program, the input is two points in a xy-plane. Each point is specified by providing their x and y-coordinates: Point 1: (x1, y1) and Point 2: (x2, y2). The program will then: a) Compute the distance between the 2 points b) Determine the direction of travel from point 1 to point 2 The directions of travel should translate to either N, S, E, W, NE, NW, SW, SE, ENE, NNE, NNW, WNW, WSW, SSW, SSE, or ESE.

// Sample output for point (3, 3) and (-8, 2) Enter x and y coordinates of two points: Point 1: (x1, y1) x1 = 3 y1 = 3 Point 2: (x2, y2) x2 = -8 y2 = 2 The distance between these points: 11.05 The direction traveled from the first point to the second: W

The main method is used to request input and to call the appropriate methods described below in order to report the distance and direction. Code main using the following algorithm. Prompt the user to enter two points Find the quadrant by calling the findQuadrant method, passing in the x1, y1, x2 and y2 values entered by user. Use the found quadrant to find pt 3: (x3,y3): For Quad I and Quad III:x3 = x2 and y3 = y1 For Quad II and Quad IV: x3 = x1 and y3 = y2 Compute length of three sides, by calling the distance method for each side with the appropriate parameteres. Compute the angle A at point 1, by calling the compAngle method, which uses the Law of Cosines. See ComputeAngles.java code. Compute the direction by calling the compDirection method Display the distance and direction. Round the distance to two decimal places before displaying it. The ComputeAngles.java code shows you how to do this rounding.

Code the other methods as follows:

Method Parameters Description static double distance() double x1, double y1, double x2, double y2 Return the distance from P1: (x1, y1) to P2: (x2, y2) See ComputeAngles.java code.

static double compAngle () double sideA, double sideB, double sideC Return the angle at P1: (x1, y1) in degrees, where sideA is across from P1: (x1, y1), sideB is across from P2: (x2, y2) and sideC is across from P3: (x3, y3) See ComputeAngles.java code.

static String compDirection() double angle, int quadrant Return the direction of travel using the angle and quadrant. This can be coded using a switch block based on quadrant and within each of the four cases, five if/else blocks that determine the direction. Table 2 above gives the angle intervals to check for each direction

Table 2: Direction Intervals Within Quadrants

Quadrant

Direction

Angle Interval

From Axis

I

E

(00.00, 11.25]

+ x-axis

I

ENE

(11.25, 33.75]

+ x-axis

I

NE

(33.75, 56.25]

+ x-axis

I

NNE

(56.25, 78.75]

+ x-axis

I

N

(78.75, 90.00]

+ x-axis

II

N

(00.00, 11.25]

+ y-axis

II

NNW

(11.25, 33.75]

+ y-axis

II

NW

(33.75, 56.25]

+ y-axis

II

WNW

(56.25, 78.75]

+ y-axis

II

W

(78.75, 90.00]

+ y-axis

III

W

(00.00, 11.25]

- x-axis

III

WSW

(11.25, 33.75]

- x-axis

III

SW

(33.75, 56.25]

- x-axis

III

SSW

(56.25, 78.75]

- x-axis

III

S

(78.75, 90.00]

- x-axis

IV

S

(00.00, 11.25]

- y-axis

IV

SSE

(11.25, 33.75]

- y-axis

IV

SE

(33.75, 56.25]

- y-axis

IV

ESE

(56.25, 78.75]

- y-axis

IV

E

(78.75, 90.00]

- y-axis

static int findQuadrant() double x1, double y1, double x2, double y2 Return the quadrant in which the direction of travel takes you from (x1,y1) to (x2,y2). Quads: Q1: when x2 > x1 and y2 >= y1 Q2: when x2 <= x1 and y2 > y1 Q3: when x2 < x1 and y2 <= y1 Q4: when x2 >= x1 and y2 < y1 return 1, 2, 3, or 4

static void main() String [] args See above

**ComputeAngles.java code:

import java.util.Scanner;

public class ComputeAngles {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

// Prompt the user to enter three points System.out.print("Enter three points: "); double x1 = input.nextDouble(); double y1 = input.nextDouble(); double x2 = input.nextDouble(); double y2 = input.nextDouble(); double x3 = input.nextDouble(); double y3 = input.nextDouble();

// Compute three sides double a = Math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3)); double b = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); double c = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

// Compute three angles double A = Math.toDegrees(Math.acos((a * a - b * b - c * c) / (-2 * b * c))); double B = Math.toDegrees(Math.acos((b * b - a * a - c * c) / (-2 * a * c))); double C = Math.toDegrees(Math.acos((c * c - b * b - a * a) / (-2 * a * b)));

// Display results System.out.println("The three angles are " + Math.round(A * 100) / 100.0 + " " + Math.round(B * 100) / 100.0 + " " + Math.round(C * 100) / 100.0); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions