Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quadratic Formula

 
Richard Innamorato
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Here is the program

 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Richard Innamorato
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I should have posted the results I am getting.

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,

Richard
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may be able to simplify that formula.
  • Never use pow(x, 2). Use x * x. Probably faster performance and more accurate.
  • Maybe you can permit the formula to accept negative discriminants, because the sqrt() method has a special way of dealing with them.
  • Look very closely at the hasSolution method and check whether you are looking at negative numbers or positive.
  • Don’t use == true or ==false. Simply delete == true throughout.
  •  
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10417
    63
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
  • Don’t use == true or ==false. Simply delete == true throughout.

  • We should call this the "Ritchie Rule".

    Winston
     
    Richard Innamorato
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ritchie,

    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.

    Thanks,

    Richard.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12122
    30
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Sheriff
    Posts: 48921
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If the exercise said to pass booleans for negative discriminants, then my suggestion about NaN won’t help.
    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.
     
    Campbell Ritchie
    Sheriff
    Posts: 48921
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote: . . . We should call this the "Ritchie Rule".

    Winston
    No, that is, “there is no such thing as a 2D array”
     
    Campbell Ritchie
    Sheriff
    Posts: 48921
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see you already have those fields. All those fields ought to have private access. Sorry for missing that.
     
    Richard Innamorato
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fred,

    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.




     
    Richard Innamorato
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ritchie,

    you are right about the discriminant. The extra line computing the rootsquare should be in getSolution method. I have updated both methods. like this:



    and:



    However, I still can't access those variables.
     
    James Boswell
    Bartender
    Posts: 1051
    5
    Chrome Eclipse IDE Hibernate
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Shouldnt there be a call somewhere to the getDiscriminant() method?
     
    Richard Innamorato
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    James,

    there is a method call but it was in the main method.

     
    Richard Innamorato
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    James,

    you were right. I changed the main method to call this method and it worked. I needed to call the method to get the results. I feel like an idiot. Thanks!
     
    James Boswell
    Bartender
    Posts: 1051
    5
    Chrome Eclipse IDE Hibernate
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are my eyes deceiving me? I can't see this call? No call to the getDiscriminant() certainly explains the output you are getting.
     
    James Boswell
    Bartender
    Posts: 1051
    5
    Chrome Eclipse IDE Hibernate
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ricardo

    I would make the getDiscriminant() method private, rename it to setDiscriminant() and call it as the last line of your constructor.

    Glad I could help.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic