• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 28347
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10969
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is suspect. Sqrt of 'b' squared is 'b'.

 
Paul Clapham
Sheriff
Posts: 28347
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80140
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5583
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rian bron wrote:
the right solution is :


Hmmm, are you sure?    
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first b needs to be negative, correct?

https://www.calculatorsoup.com/calculators/algebra/quadratic-formula-calculator.php
 
My previous laptop never exploded like that. Read this tiny ad while I sweep up the shards.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic