# add an If Else statement and......uh????

• 03-04-2010, 04:17 PM
sonny
add an If Else statement and......uh????
Write a ConsoleProgram that accepts values for a, b, and c, and then calculates the two quadratic equation solutions.
If the quantity under the square root sign is negative, the equation has no real solutions, and your program should display a message to that effect.

here is my code:
Code:

```public void run(){         println("Enter coefficients for the quadratic equation: ");         int a = readInt(" Input a: ");         int b = readInt(" Input b: ");         int c = readInt(" Input c: ");         [COLOR="SeaGreen"]//if ((b*=b)-(4*a*c)<=0){                 //println(" The formula has no real solutions");         //}else{[/COLOR]                 println(" The first solution is: "+ quadraticPlus(a,b,c));                 println(" The second solution is: "+ quadraticMinus(a,b,c));                 [COLOR="SeaGreen"] // } [/COLOR]         }         private double quadraticMinus(int a, int b, int c) {                 double minusX = (-b - Math.sqrt((b*=b)-(4*a*c)))/2*a;                 return minusX;         }         private double quadraticPlus(int a, int b, int c) {                 double plusX = (-b + Math.sqrt((b*=b)-(4*a*c)))/2*a;                 return plusX;         } }```
i wrote this without the If Else statement (in green) to make sure the methods worked, which they did
and heres my output
Code:

``` Input a: 1  Input b: -5  Input c: 6  The first solution is: 3.0  The second solution is: 2.0```
this output is correct.

but, when i add in the If Else statement i get this output
Code:

```Input a: 1  Input b: -5  Input c: 6  The first solution is: -0.24234932786873742  The second solution is: -24.757650672131263```
and i dont understand why it has changed. :confused:
if the method is passed the same values why should it return different results
• 03-04-2010, 04:24 PM
StormyWaters
Take a look at your if statement... specifically this
Code:

`(b*=b)`
What is this little bit of code doing?
• 03-04-2010, 04:32 PM
sonny
it should be this
(b*b) :o

thanks a lot..:)
• 03-04-2010, 04:41 PM
sonny
just one other thing
when i put b*=b into a boolean test does it actually alter the variable int b, that is passed to the methods

or should i say can you alter an integer value inside a boolean test.

i thought that a boolean could only be true or false

in the OP example it appears to alter the value of int b otherwise why would the result be different
• 03-04-2010, 06:35 PM
StormyWaters
That is correct, you can change an object's value inside a boolean test.

Just because its altered doesn't mean that the statement doesn't return a true or false.

Just to prove it run a SSCC example as such:
Code:

```public class Test {                 public static void main(String[] args){                 int i = 0;                 while (i < 100) {                         i = i + 1;                         System.out.println("1st Print: " + i);                         if (i++ % 2 == 0) {                                 System.out.println("You'll never see me...");                         }                 }         }         }```
• 03-04-2010, 07:35 PM
JosAH
Just one other thing: if b*b-4*a*c == 0 there is one real root (-b/(2*a)). Only if that term is less than zero there are two complex roots; otherwise (that term is greater than zero) there are two real roots.

kind regards,

Jos
• 03-04-2010, 07:57 PM
sonny
i see :D

i had thought that the stuff in the boolean parenths was simply a test it hadn't occurred to me the program was still in fact a program within the boolean parenths and it seems kinda obvious now you pointed that out.

and thanks Jos for pointing out the 0 bit

i now have (b*b)-(4*a*c)<0) which works great