3

I am working on a school assignment. I am supposed to implement a class and supply the methods getSolution1 and getSolution2. However, I am having 2 issues with my code that I cannot figure out.

Issue #1 is on this line:

solution1= ((-1*b)/> + Math.sqrt(Math.pow(b,2)-(4*a*c)));

The compiler is telling me: Syntax error on token ">", delete this token. I can't figure out if I am doing something wrong on my syntax.

Issue #2 is on the ouput line:

String quadEquation= "The quadratic equation is "+ a + Math.pow(("x"),2) + " + " + b+"x"+ " + " + c+ " =0";

Under the Math.pow I get an error that says: The Method pow is not applicable for the arguments String

Here is my entire code:

       public class QuadraticEquation

{

  double a;
  double b;
  double c;
  double solution1;
  double solution2;

QuadraticEquation (double a, double b, double c){

     a= this.a;
     b= this.b;
     c= this.c;
}

public boolean hasSolution (){

  if ((Math.pow(b,2))- (4*a*c)<0){

    return false;
  }

  else

  {
    return true;
  }
}

 public double getSolution1 (double a, double b, double c)

{

  if (hasSolution){

      solution1= ((-1*b) + Math.sqrt(Math.pow(b,2)-(4*a*c))) / 2*a;

    return solution1;

  }

}

 public double getSolution2 (double a, double b, double c){

    if (hasSolution){

        solution1= ((-1*b) - Math.sqrt(Math.pow(b,2)-(4*a*c))) / 2*a;
    return solution2;
}

}

public String toString (double a, double b, double c){

    String quadEquation= "The quadratic equation is "+ a + "x^2" + " + " + b+"x"+ " + " + c+ " =0";

    return quadEquation;

  }

}

Since this is a school assignment, I am looking for guidance on solving this problem.

Thank you.

Jose M.
  • 2,242
  • 8
  • 44
  • 66

2 Answers2

8

Your first issue is that you can't use /> together. This is not a proper operation. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

Second issue is because Math.pow requires two numbers. You have a string in there. It would be like trying to get the power of the word apple. You cant do it. You must first convert that string into an int. How to convert a String to an int in Java?

Community
  • 1
  • 1
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
2
solution1= ((-1*b) + Math.sqrt(Math.pow(b,2)-(4*a*c))) / 2*a;

There is no such thing as a /> in Java.

String quadEquation= "The quadratic equation is "+ a + "x^2" + " + " + b+"x"+ " + " + c+ " =0";

Math.pow requires numbers whereas you were passing the string "x". the symbol "^" is generally used to say to the power of , therefore x^2 is x to the power of 2. I do not think there is a simple solution to write superscript in standard output.

Java cannot understand what to return if the equation has no solution

public double getSolution2 (double a, double b, double c){

    if (hasSolution){
        solution1= ((-1*b) - Math.sqrt(Math.pow(b,2)-(4*a*c))) / 2*a;
        return solution2;
    }
    return -1; // or throw an exception.
}

returning -1 will fix it.

Osama Javed
  • 1,432
  • 1
  • 16
  • 21
  • Thank you, when I made the change it got rid of the error. But as I made that change I realized that this line: public double getSolution1 (double a, double b, double c, boolean hasSolution) was incorrect. it would not return a value of double. so I got rid off boolean hasSolution. but now my statement if (hasSolution) is saying that is not initialized. – Jose M. Jul 19 '13 at 20:08
  • @user2559274 i am not sure what you mean by that. returning a double is perfectly reasonable. Why did you think that line was incorrect.Add the new code to the question so that I might be able to answer. Just a couple of things.. if ( hasSolution ) should be enough.. no need for "== true". Furthermore define solution1 and solution2 inside methods instead of the class. you dont even need variables . you can simply do `return ((-1*b)/> - Math.sqrt(Math.pow(b,2)-(4*a*c)));` – Osama Javed Jul 19 '13 at 21:31
  • Thank you, now that I see it, it seems rather obvious. But it eluded me for a couple of days. I appreciate the explanation – Jose M. Jul 21 '13 at 02:25