Results 21 to 28 of 28
Thread: Can't find symbol
- 09-12-2012, 05:14 PM #21
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
I have been self-teaching 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) } }
- 09-12-2012, 05:28 PM #22
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-12-2012, 05:40 PM #23
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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?
- 09-12-2012, 05:53 PM #24
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Can't find symbol
Please do not ask for code as refusal often offends.
- 09-12-2012, 05:58 PM #25
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
- 09-12-2012, 06:58 PM #26
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-13-2012, 04:49 PM #27
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
Ok, i'll do that, thanks.
Is there anything else which I could have done better in the program?
- 09-13-2012, 05:27 PM #28
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Can't find symbol
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Cannot find symbol
By dest in forum New To JavaReplies: 3Last Post: 04-03-2012, 05:38 PM -
Cannot find symbol
By Eleeist in forum New To JavaReplies: 5Last Post: 01-22-2012, 08:36 PM -
cannot find symbol
By LimblessQuasar in forum New To JavaReplies: 4Last Post: 06-11-2011, 10:55 PM -
cannot find symbol symbol :constructor Error. Please help! =(
By KalEl in forum New To JavaReplies: 9Last Post: 10-18-2008, 08:26 PM -
cannot find symbol symbol : class Item location: package platypos.services.order
By officialhopsof in forum New To JavaReplies: 3Last Post: 05-01-2008, 08:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks