programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Knute Snortum
• paul wheaton
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Ron McLeod
• Piet Souris
• Ganesh Patekar
Bartenders:
• Tim Holloway
• Carey Brown
• salvin francis

# I tried to write an easy algorithm but it doesn't work

Ranch Hand
Posts: 117
Hello guys,  i tried to write the formula of Quadratic equation in  java, but for some reason it seems that i wrote it right but the answer is wrong.
the code i wrote :

the algorithm is at lines 20,21
could you please tell me what i did wrong?
Thank you

Sheriff
Posts: 24635
56
Well, first of all your code is very hard to read because you use the accessor methods instead of the instance variables. If I rewrite your code to remove that idea (and to remove the complicated method of squaring a number, and to remove unnecessary brackets) then it looks like this:

When I compare that to the quadratic formula I can see at least two differences, probably three. And that's before considering the possibility of complex numbers as the solution.

rian bron
Ranch Hand
Posts: 117

Paul Clapham wrote:Well, first of all your code is very hard to read because you use the accessor methods instead of the instance variables. If I rewrite your code to remove that idea (and to remove the complicated method of squaring a number, and to remove unnecessary brackets) then it looks like this:

When I compare that to the quadratic formula I can see at least two differences, probably three. And that's before considering the possibility of complex numbers as the solution.

well, your answer is indeed easier to read, but still if i insert a=1,b=2,c=-3 the answer i get according to this code is x1=8,x2=-6 versa the correct answer is x1=-3,x2=1
and i don't get why the answer is incorrect although the algorithm seems to be right

lowercase baba
Posts: 12760
51
first of all, what is the quadratic formula?  from the get go, I think your very first term is wrong.

What I would suggest you do is break it apart.  you can't tell where the math is wrong, because you do it all in one line.  so, i'd suggest breaking it apart.

is "b" correct?

what does "(Math.sqrt(b*b)-4*a*c))" give you, and is it correct? if not, break it apart into smaller pieces.

if you are not getting the right answer, you need to figure out what part is wrong, and focus on that.

Bartender
Posts: 6133
58
• 1
This is suspect. Sqrt of 'b' squared is 'b'.

Paul Clapham
Sheriff
Posts: 24635
56

Carey Brown wrote:This is suspect. Sqrt of 'b' squared is 'b'.

Indeed. That's just one of the errors in the calculation -- the brackets are in the wrong place there.

Marshal
Posts: 65447
248

Paul Clapham wrote:. . . just one of the errors in the calculation . . .

I can see two more errors, I think. Please remind yourself what the formula for a quadratic equation is.
You doubtless already know that you can't directly implement ± in Java®, so you will have to calculate the two solutions separately. Yes, you are calculating them separately. It would make sense mathematically to calculate the determiner first; if it is > 0 you have two real solutions, if it is 0 you have one real solution and if it is < 0 you have two solutions which are complex numbers.

rian bron
Ranch Hand
Posts: 117
you were right guys, the problem was with the brackets,
the right solution is :

thank you all for help

Saloon Keeper
Posts: 3462
149

rian bron wrote:
the right solution is :

Hmmm, are you sure?

Sheriff
Posts: 6186
164
The first b needs to be negative, correct?