Results 21 to 28 of 28
Thread: Can't find symbol
 09122012, 05:14 PM #21Senior Member
 Join Date
 Jul 2012
 Posts
 241
 Rep Power
 3
Re: Can't find symbol
I have been selfteaching myself java using a free guide I found online. This is the first program I have wrote just off the top of my head, looking at additional resources (such as this website) as little as possible.
I think the program is finally complete.
Is it any good or have I been making some bad mistakes and/or following a few bad practices when I am creating my code?
I want to make sure now so that I don't get into the habit of using bad practices.
By judging my current level with Java, would you do anything different to have a program of the same effect or not?
Here is my final code (with example coefficients given in the calculation(x, y, z) method):
Java Code:class QuadraticSolver { static int aValue, bValue, cValue; // This class is available to all methods static double plusAnswer, minusAnswer; // This class is availible to all methods public static void main(String[] args) { calculation(1, 1, 6); //This line is where the coefficients are entered if (aValue <= 0) { // This is to ensure that 0 is not entered as a coefficients for the first term System.out.println("0 is not a valid value for the coefficient of x\u00B2"); return; } else { System.out.println("The quadritic equation entered was:"); System.out.print(aValue + "x" + "\u00B2"); if (bValue >= 0) { // These if statements ensure that + and  are not both displayed for a negative coefficient System.out.print("+" + bValue + "x"); } else { System.out.print(bValue + "x"); } if (cValue >= 0) { System.out.println("+" + cValue); } else { System.out.println(cValue); } } System.out.println(" "); // This line puts a blank line inbetween the two sections of text on the output System.out.println("It's roots are:"); System.out.println(plusAnswer + " and " + minusAnswer); } public static void calculation(int a, int b, int c) { // The start of the calculation method  the variables for the coefficients are parameters aValue = a; // Because the coefficients are needed in both methods, they need to be accessible to both as a static variable (see the top comments). However, they also need to be a parameter so that the values can be entered with the method, by making the value both of these, both actions are fulfilled. bValue = b; cValue = c; plusAnswer = ((b*(1))+Math.sqrt(Math.pow(b,2)(4*a*c)))/(2*a); // This is the quadratic formula in Java syntax (positive) minusAnswer = ((b*(1))(Math.sqrt(Math.pow(b,2)(4*a*c))))/(2*a); // This is the quadratic formula in Java syntax (negative) } }
 09122012, 05:28 PM #22
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,648
 Blog Entries
 7
 Rep Power
 21
Re: Can't find symbol
Ok, you asked for it ;) your algorithm calculates the disriminant (the square root thing) twice; it is not needed, but much more important is that this way of calculating the roots of a quadratic equation is numerically unstable; read the second half of this page for a better way.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 09122012, 05:40 PM #23Senior Member
 Join Date
 Jul 2012
 Posts
 241
 Rep Power
 3
Re: Can't find symbol
By it is calculating it twice, do you mean I should create a "discriminant" variable and insert this in place of the equation in the overall equation so that it is only entered once?
And when you say that the way I used to calculate roots is unstable, do you mean that that equation itself is not ideal for all situations? i.e. completing the square and other methods may be more appropriate?
 09122012, 05:53 PM #24Moderator
 Join Date
 Apr 2009
 Posts
 12,085
 Rep Power
 20
Re: Can't find symbol
Please do not ask for code as refusal often offends.
** This space for rent **
 09122012, 05:58 PM #25Senior Member
 Join Date
 Jul 2012
 Posts
 241
 Rep Power
 3
 09122012, 06:58 PM #26
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,648
 Blog Entries
 7
 Rep Power
 21
Re: Can't find symbol
Yep, stick that entire square root thingy in an auxiliary variable and use it twice. If one of the roots is very close to zero and the other one isn't you'll lose accuracy; while the 'traditional' formula is mathematically correct there are better formulas that are numerically stable; for details read that page.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 09132012, 04:49 PM #27Senior Member
 Join Date
 Jul 2012
 Posts
 241
 Rep Power
 3
Re: Can't find symbol
Ok, i'll do that, thanks.
Is there anything else which I could have done better in the program?
 09132012, 05:27 PM #28
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,648
 Blog Entries
 7
 Rep Power
 21
Similar Threads

Cannot find symbol
By dest in forum New To JavaReplies: 3Last Post: 04032012, 05:38 PM 
Cannot find symbol
By Eleeist in forum New To JavaReplies: 5Last Post: 01222012, 08:36 PM 
cannot find symbol
By LimblessQuasar in forum New To JavaReplies: 4Last Post: 06112011, 10:55 PM 
cannot find symbol symbol :constructor Error. Please help! =(
By KalEl in forum New To JavaReplies: 9Last Post: 10182008, 08:26 PM 
cannot find symbol symbol : class Item location: package platypos.services.order
By officialhopsof in forum New To JavaReplies: 3Last Post: 05012008, 08:30 AM
Bookmarks