• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
Ranch Hand
Posts: 117
  • 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

 
Marshal
Posts: 24468
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: 117
  • 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
 
lowercase baba
Posts: 12745
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.
 
Paul Clapham
Marshal
Posts: 24468
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.
 
Marshal
Posts: 64494
225
  • 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: 117
  • 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
 
Saloon Keeper
Posts: 3253
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

rian bron wrote:
the right solution is :


Hmmm, are you sure?    
 
Marshal
Posts: 5988
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
 
You may have just won ten million dollars! Or, maybe a tiny ad.
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!