-3

I'm trying to calculate the power of a double to calculate the Quadratic Formula

Here is the code:

private Scanner sc;
double a, b, c;
// Input Coefficients
public void InputCoeff() {
    sc = new Scanner(System.in);

    System.out.println("Please enter the coefficients of this quadratic equation.");
    System.out.println("'ax2 + bx + c = 0'");
    System.out.print("a = ");
    a = sc.nextDouble();
    if(a == 0){
        System.out.println("Coefficient of x2 ('a') can't be zero.");
        System.out.println("Otherwise, it'll be a linear funciton.");
    }
    System.out.print("b = ");
    b = sc.nextDouble();
    System.out.print("c = ");
    c = sc.nextDouble();
}

public void SqRt(double x, double y, double z) {
    double sqrt;
    sqrt = pow(y, 2) - (4 * x * z); // This generates an error
    System.out.println();
}

Why this error?

Thanks.

  • 4
    Why **what** error? Is `pow` a method in your class? – Michael Petrotta Nov 04 '12 at 06:19
  • 2
    Why do people up-vote questions without a clear error message? I gave a -1 and a vote to close; being able to state the error message/problem clearly - which is a *copy and paste operation here* - is an important skill to master. *Please don't up-vote questions unless there are deserving of it*. This question, with lack of error message and vague title is just garbage to future searches. –  Nov 04 '12 at 06:31
  • 1
    BTW, it's not very "Java-like" to declare variables first and assign to them later. You can simply do `double sqrt = someExpression`. – Sanjay T. Sharma Nov 04 '12 at 06:44
  • @pst The title is related to the error (the line of code where I clearly state the error, in a comment). I knew the problem is related to the math class (pow(x,y)) & indeed it turned out to be so. – InspiringProgramming Nov 04 '12 at 15:09
  • You didn't state *what the error was*. Hiding important information in a comment in the code isn't useful either. – user207421 Nov 04 '12 at 22:01
  • @EJP Ok, sry, didn't know that ... I'm new here! – InspiringProgramming Nov 05 '12 at 16:58

3 Answers3

4

Change pow to Math.pow. The Math class is a static class, and you need to get the method from the class itself.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
Brandon
  • 1,336
  • 3
  • 10
  • 38
1

Use Math. as prefix to your function pow because pow(double a, double b) is an static method from java.lang.Math class:

public void SqRt(double x, double y, double z) {
    double sqrt;
    sqrt = Math.pow(y, 2) - (4 * x * z); // This generates an error
    System.out.println(sqrt);
}

Also I think you may want to return sqrt from this method so change the return type from void to double and add a return statement in the end as below:

public dobule SqRt(double x, double y, double z) {
    double sqrt;
    sqrt = Math.pow(y, 2) - (4 * x * z); // This generates an error
    System.out.println(sqrt);
        return sqrt;
}
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
1

Math.pow is a relative expensive operation to use and also longer to type in many case so I would avoid it if you can. It's much simpler (and faster for you and the computer) use x * x

public static Set<Double> solve(double a, double b, double c) {
    Set<Double> solutions = new TreeSet<Double>();
    double discriminant = b * b - 4 * a * c;
    solutions.add((-b - Math.sqrt(discriminant)) / (2 * a));
    solutions.add((-b + Math.sqrt(discriminant)) / (2 * a));
    solutions.remove(Double.NaN);
    return solutions;
}

public static void main(String... args) {
    System.out.println(solve(1, 3, 2));
    System.out.println(solve(1, 0, -1));
    System.out.println(solve(1, 4, 4));
    System.out.println(solve(1, 0, 1));
}

prints

[-2.0, -1.0]
[-1.0, 1.0]
[-2.0]
[]

Note that you can have none, one or two real solutions.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130