Here is the program
Ricardo Figueredo wrote:Hi everyone, I have been practicing Java with the Big Java book and one fo the exercises is to create a program that would calculate the solutions to the quadratic formula. I wrote the code but I am having troubles getting the right results. Can you guys take a look at and tell me where the mistake is?
A little more information would be helpful. What results are you getting?
Also, you might want to look at this Thread, which seems to be based on the same exercise.
Here it is:
Thanks for the link. I reviewed the link and it gave me a couple of good ideas on how to modify my code. However, he is using a different implementation. The method I am having the first problem is getDiscriminant(), it should return something other than "0". I can't get the variable "bSquare" to have the value once the calculation is done. bSquare should be 25 when I call it in the printJob() method.
thanks for the pointers, I deleted the ==true statements. I could deal with the negative values of the quadratic equation and displaying them but the exercise was to use the boolean type so it should return false if the discriminant is negative and provide a statement that there is no real solution to this equation.
My main problem right now is accessing the variables inside the method:
Using the printJob. If I can access these variables, then the rest of the program should run find. I added bSquare, result, and discriminant as public variables but it is not working.
Ricardo Figueredo wrote: but it is not working.
Again, this tells us nothing. HOW is it not working? What is it doing? What do you think it should do?
What variables in here do you want to access? What can't you access? Folks here are really trying to help you, but you have to make it easy on them, or they'll get frustrated and walk away.
You cannot access variables inside a method from outside the method.
The discriminant is b² - 4ac, not √(b² - 4ac).
You might do well to make a, b and c instance fields of your class. You can make discriminant a (derived instance) field, too.
the answer to your question was posted in my second post but I will repeat it to make it easier for those trying to help me.
This is the results I am getting:
I am using the printJob method to get this results:
These variables' values are calculated in the getDiscriminant() method:
So once printJob() calls bSquare it should be 25 (5 * 5), result should be 25-(4*1*3) = 13.