Results 1 to 4 of 4
  1. #1
    AndrewSD is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Exclamation [Help] Polynomial Class in Java

    Ok well I wrote a class that simplifies polynomials, but I always get weird answers. Take a look at the code and then at the bottom of the post.
    Java Code:
    
    package polynomialhelper;
    import javax.swing.*;
    
    /**
     * This class helps with polynomials!
     * @author Andrew
     */
    public class polynomialEngine {
    
        /**
         * Class constructor nothing interesting.
         */
        public polynomialEngine() {
        }
    
        /**
         *
         * @param x the array of x coefficients
         * @param x2 the array of x<sup>2</sup> coefficients
         * @param coeff coefficient at the end
         * @return polynomial in string state.
         */
        public String simplifyPolynomial(float[] x, float[] x2, float coeff) {
            String polynomial = "Error";
            float xs = 0, x2s = 0;
    
            for(int i = 0; i <3; i++) {
                xs = xs + x[i];
                x2s += x2[i];
            }
    
            JOptionPane.showConfirmDialog(null, x2s);
    
            if ( xs != 0 & x2s !=0 ) {
            polynomial = "<html>" + String.valueOf(x2s) + "x<sup>2</sup>+" + String.valueOf(xs) + "x+" + String.valueOf(coeff) + "</html>";
            }
    
     else if (xs != 0 & x2s == 0) {
            polynomial = "<html>" + String.valueOf(xs) + "x" + "+" + String.valueOf(coeff) + "</html>";
            }
    
     else if (xs == 0 & x2s != 0) {
            polynomial = "<html>" + String.valueOf(x2s) + "x<sup>2</sup>+" + String.valueOf(coeff) + "</html>";
            }
    
     else if (xs == 0 & x2s == 0) {
                polynomial = "<html>" + String.valueOf(coeff) + "</html>";
            }
    
     else if (xs != 0 & x2s != 0 & coeff == 0) {
            polynomial = "<html>" + String.valueOf(x2s) + "x<sup>2</sup>+" + String.valueOf(xs) + "x" + "</html>";
            }
    
     else if (xs != 0 & x2s == 0 & coeff == 0) {
            polynomial ="<html>" +  String.valueOf(xs) + "x" + "</html>";
            }
    
     else if (xs == 0 & x2s != 0) {
            polynomial = "<html>" +  String.valueOf(x2s) + "x<sup>2</sup>" + "</html>";
            }
    
    else {
               polynomial = "Error please check data!";
                JOptionPane.showMessageDialog(null, "Error please check the data you enter!", "Polynomial Helper", JOptionPane.ERROR_MESSAGE);
    
    
    
    }
    
    
            return polynomial;
    
        }
    }
    Please note that I am using this class in an other program, it has a limit of three coefficients per-variable and here are some of the test values I passed and here is what I got:

    1st x value: 1
    2nd x value: 2
    3rd x value: 3
    1st x^2 value: 1
    2nd x^2 value: 23rd x^3 value: 3
    Coeff. value : 4

    Answer: 6x^2 + 6x + 4 (totally right)


    1st x value: 1
    2nd x value: 3
    3rd x value: 5
    1st x^2 value: 2
    2nd x^2 value: 4
    3rd x^3 value: 6
    Coeff. value : 7

    Answer: 11x^2 + 9x + 7 (uh, wrong)

    What is wrong with my code?

  2. #2
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Andrew - I ran a test on it using this main method and the exact values you used in your last test which you claim failed:

    Java Code:
    	public static void main(String[] args)	{
    		float[] x = {1,3,5};
    		float[] x2 = {2,4,6};
    		float coeff = 7;
    		String answer = simplifyPolynomial(x, x2, coeff);
    		System.out.println(answer);
    	}
    The answer i got is <html>12.0x<sup>2</sup>+9.0x+7.0</html> which is 12x^2 + 9x + 7. This is correct.

    So what is the issue? Are you sure the parameters you passed to your method are correct?

    Best,
    --user0--

  3. #3
    AndrewSD is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    Oh thank, you thank, you thank you! Thank you for pointing out that the class and methods weren't the problem!

    Thanks,

    Andrew:D

  4. #4
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

Similar Threads

  1. API polynomial java
    By mcherkao in forum Advanced Java
    Replies: 5
    Last Post: 09-10-2010, 02:49 PM
  2. Polynomial Arithmetic Java
    By thisisIT in forum New To Java
    Replies: 3
    Last Post: 03-09-2010, 06:33 PM
  3. Replies: 14
    Last Post: 05-28-2009, 06:44 AM
  4. class.java to class.exe
    By f_the_cook in forum New To Java
    Replies: 11
    Last Post: 07-16-2008, 03:24 PM
  5. Help with polynomial program
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:44 AM

Tags for this Thread

Posting Permissions

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