user1928899 - 1 year ago 84

Java Question

My assessment was to do the program and the instructions are listed below.

- The radius of the circle should be 1.0.

- Use the appropriate Math class methods in your arithmetic expression(s).

- The value of the x coordinate should change by 0.1 during initial testing. After the program is working verify that the increment can

also be 0.01, or 0.001 with only minor changes to the code.

- Display the information in a neatly formatted table. (See expected output.) Use a Formatting Grid to save time developing the layout for

your output. Background: Recall from your algebra class that the

Pythagorean Theorem can be used to determine the x or y coordinate if

you know the radius of the circle and the value of either x or y.

Assume that you are dealing with a circle whose radius is 1. If you

iterate through successive values of x, then you can calculate the

corresponding value of y. Be sure to use methods of the Math class to

set up the arithmetic expression.

This is the circle: http://i.stack.imgur.com/NypKz.png

I did the program below.

`public class PointsOnACircleV1 {`

public static void main(String[ ] args)

{

double[] x1 = { 1.0, 0.90, 0.80, 0.70, 0.60, 0.50, 0.40, 0.30, 0.20,

0.10, 0.00, -0.10, -0.20, -0.30, -0.40, -0.50, -0.60, -0.70,

-0.80, -0.90, -1.00 };

double r = 1;

System.out.println(" Points on a Circle of Radius 1.0");

System.out.println(" x1 y1 x2 y2");

System.out.println("---------------------------------------------");

for (int i = 0; i < x1.length; i++) {

double y1 = Math.sqrt(Math.pow(r, 2) - Math.pow(x1[i], 2));

double y2 = Math.sqrt(Math.pow(r, 2) - Math.pow(x1[i], 2));

System.out.printf("%10.2f%10.2f%10.2f%10.2f%n", x1[i], y1, x1[i], y2);

}

System.out.println("");

}

}

The output is supposed to be like this: http://i.stack.imgur.com/pywwJ.png

I am getting a different output. My output is coming like this.

`Points on a Circle of Radius 1.0`

x1 y1 x2 y2

---------------------------------------------

1.00 0.00 1.00 0.00

0.90 0.44 0.90 0.44

0.80 0.60 0.80 0.60

0.70 0.71 0.70 0.71

0.60 0.80 0.60 0.80

0.50 0.87 0.50 0.87

0.40 0.92 0.40 0.92

0.30 0.95 0.30 0.95

0.20 0.98 0.20 0.98

0.10 0.99 0.10 0.99

0.00 1.00 0.00 1.00

-0.10 0.99 -0.10 0.99

-0.20 0.98 -0.20 0.98

-0.30 0.95 -0.30 0.95

-0.40 0.92 -0.40 0.92

-0.50 0.87 -0.50 0.87

-0.60 0.80 -0.60 0.80

-0.70 0.71 -0.70 0.71

-0.80 0.60 -0.80 0.60

-0.90 0.44 -0.90 0.44

-1.00 0.00 -1.00 0.00

Answer Source

You have calculated y1 and y2 to be the same value because the code is identical:

```
double y1 = Math.sqrt(Math.pow(r, 2) - Math.pow(x1[i], 2));
double y2 = Math.sqrt(Math.pow(r, 2) - Math.pow(x1[i], 2));
```

It seems from the results that the value of y2 should just be the negation of y1. Try this instead:

```
double y1 = Math.sqrt(Math.pow(r, 2) - Math.pow(x1[i], 2));
double y2 = -y1;
```