• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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

 
rian bron
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Paul Clapham
Sheriff
Posts: 24380
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
fred rosenberger
lowercase baba
Posts: 12738
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Bartender
Posts: 5775
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
 
Paul Clapham
Sheriff
Posts: 24380
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 64179
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you were right guys, the problem was with the brackets,
the right solution is :


thank you all for help
 
Piet Souris
Master Rancher
Posts: 3189
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rian bron wrote:
the right solution is :


Hmmm, are you sure?    
 
Knute Snortum
Sheriff
Posts: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first b needs to be negative, correct?

https://www.calculatorsoup.com/calculators/algebra/quadratic-formula-calculator.php
 
Always look on the bright side of life. At least this ad is really tiny:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!