Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
  1. #21
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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)
    
    		}
    
    
    }

  2. #22
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Can't find symbol

    Quote Originally Posted by kkid View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #23
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Can't find symbol

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    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?

  4. #24
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Can't find symbol

    Quote Originally Posted by kkid View Post
    How do I print a 2 symbol though?
    I searched online and it said to type \u00B2 but it just comes up with a filled in rectangle.
    Whether that symbol appears is entirely down to the character set and capabilities of wherever the println call is going.
    If it's the command line then it's very unlikely to be a printable character.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #25
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Can't find symbol

    Quote Originally Posted by Tolls View Post
    Whether that symbol appears is entirely down to the character set and capabilities of wherever the println call is going.
    If it's the command line then it's very unlikely to be a printable character.
    oh, ok thanks

  6. #26
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Can't find symbol

    Quote Originally Posted by kkid View Post
    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?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #27
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Can't find symbol

    Ok, i'll do that, thanks.


    Is there anything else which I could have done better in the program?

  8. #28
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Can't find symbol

    Quote Originally Posted by kkid View Post
    Ok, i'll do that, thanks.


    Is there anything else which I could have done better in the program?
    Be able to do complex roots? (the discriminant is less than zero). And to take it all over de edge: why not allow complex coefficients of your quadratic equation?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Cannot find symbol
    By dest in forum New To Java
    Replies: 3
    Last Post: 04-03-2012, 06:38 PM
  2. Cannot find symbol
    By Eleeist in forum New To Java
    Replies: 5
    Last Post: 01-22-2012, 09:36 PM
  3. cannot find symbol
    By LimblessQuasar in forum New To Java
    Replies: 4
    Last Post: 06-11-2011, 11:55 PM
  4. Replies: 9
    Last Post: 10-18-2008, 09:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 09:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •