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

1. ## 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:
Java 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
Java 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
Java 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

2. Senior Member
Join Date
Feb 2009
Posts
312
Rep Power
9
Take a look at your if statement... specifically this
Java Code:
`(b*=b)`
What is this little bit of code doing?

3. it should be this
(b*b) :o

thanks a lot..:)

4. 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

5. Senior Member
Join Date
Feb 2009
Posts
312
Rep Power
9
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:
Java 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...");
}
}
}

}```

6. 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

7. 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•