Melanie
Melanie

Reputation: 317

Why does the compiler not recognize Math.PI()?

UPDATE: New problem in the code (see below)
For Java homework, we need to compute the cos and sin values at each interval of pi/4 of the unit circle and print it out.But mines is rounding incorrectly, even though I used the rounding technique the instructions provided using the assignment of a double doubleName and doubleName = Math.round(doubleName* 100) / 100.0.

The outcome should be:
Radians: (cos, sin)
0.0: 1.0, 0.0
0.79: 0.7, 0.71
1.57: 0.0, 1.0
2.36: -0.71, 0.7
3.14: -1.0, 0.0
3.93: -0.7, -0.71
4.71: 0.0, -1.0
5.5: 0.71, -0.71

But my output is:
Radians: (cos, sin)
0.0: 1.0, 0.0
0.79: 0.7, 0.71
1.58: -0.01, 1.0
2.37: -0.72, 0.7
3.16: -1.0, -0.02
3.95: -0.69, -0.72
4.74: 0.03, -1.0
5.53: 0.73, -0.68

My code:

public class UnitCircle extends ConsoleProgram {
  public void run() {
    System.out.println("Radians: (cos, sin)");
    for (double i = 0; i <= 2 * Math.PI; i += Math.PI / 4) {
      i = Math.round(i * 100.0) / 100.0;
      double x = Math.cos(i);
      x = Math.round(x * 100.0) / 100.0;
      double y = Math.sin(i);
      y = Math.round(y * 100.0) / 100.0;
      System.out.println(i + ": " + x + ", " + y);
    }

  }
}

Upvotes: 0

Views: 1488

Answers (1)

Jesse
Jesse

Reputation: 2074

You are using Math.PI(). Have a look at the Java Math Class Documentation found here: https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html

There is no PI method, however there is a PI Field. So try:

Math.PI

Upvotes: 2

Related Questions