[Java]Need help with polynomcomparing method menu

User Tag List

Results 1 to 7 of 7
  1. #1
    Zantas's Avatar Contributor
    Reputation
    258
    Join Date
    Dec 2007
    Posts
    1,114
    Thanks G/R
    0/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Java]Need help with polynomcomparing method

    I have the following code and I need to add a method to it that should be called
    public int compareTo(Polynom p)

    the method should return -1 if polynom1 is smaller than polynom2, if they are ewual it should return 0, and 1 if polynom1 is larger than polynom2.

    Now my issue is that I've no idea how to do thisbecause primarily, I haven't programmed for a while and as a beginner you tend to lose knowledge with focus decreasing, and secondly the branch code was just given to me without any explaination, so I'm quite confused on how to work with it.

    CODE
    Code:
     public class Polynom implements Comparable<Polynom> {
    
        double[] polynom;
        double[] der;
    
        /**
        polynom contains the coefficient of the term i such as the exponent is also i
         * example : 4x^7 +3x^2 is representend by
         *[0,0,3,0,0,0,0,4]
         */
        public Polynom(double[] coeff) {
            polynom = new double[coeff.length];
            for (int i = 0; i < coeff.length; i++) {
                polynom[i] = coeff[i];
            }
            der = new double[coeff.length - 1];
            getDeri();
        }
    
        private double[] getDeri() {
            for (int i = 1; i < polynom.length; i++) {
                der[i - 1] = polynom[i] * i;
            }
    
            return der;
        }
    
        public double[] getDerivative() {
            return der;
        }
    
    
        public double calculatePolynom(double x) {
    
            double summa = 0;
            for (int i = 0; i < polynom.length; i++) {
                summa = summa + polynom[i] * (Math.pow(x, i));
            }
            return summa;
        }
    
        public double calculateDerivative(double x) {
    
            double summa = 0;
            for (int i = 0; i < der.length; i++) {
                summa = summa + der[i] * (Math.pow(x, i));
            }
            return summa;
        }
    
        public String toString() {
    
            String p = "" + polynom[0];
            if (polynom.length > 1) {
                p = p + " + " + polynom[1] + "x";
            }
            int i = 2;
            while (i < polynom.length) {
                if (polynom[i] != 0) {
                    p = p + " + " + polynom[i] + "x^" + i;
                }
                i++;
            }
            return p;
        }   
    }
    https://i45.tinypic.com/157df7r.jpg


    [Java]Need help with polynomcomparing method
  2. #2
    Jotox's Avatar Contributor
    Reputation
    250
    Join Date
    Mar 2008
    Posts
    282
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey so I did this real quick, didn't test it or anything, but the logic is pretty simple:

    Polynomial A is greater than polynomial B if and only if, for the term of highest degree with unequal coefficients between the two polynomials, A's coefficient for this term is greater than B's.

    I.e:
    2x^2 + 3x + 1 > 2x^2 + 2x + 1;
    3x^6 + 1 > 3x^5 + 1;
    -7x^2 < x + 5;

    Here's the code:
    Code:
     public class Polynom implements Comparable<Polynom> {
    
        double[] polynom;
        double[] der;
    
        /**
        polynom contains the coefficient of the term i such as the exponent is also i
         * example : 4x^7 +3x^2 is representend by
         *[0,0,3,0,0,0,0,4]
         */
        public Polynom(double[] coeff) {
            polynom = new double[coeff.length];
            for (int i = 0; i < coeff.length; i++) {
                polynom[i] = coeff[i];
            }
            der = new double[coeff.length - 1];
            getDeri();
        }
    
        private double[] getDeri() {
            for (int i = 1; i < polynom.length; i++) {
                der[i - 1] = polynom[i] * i;
            }
    
            return der;
        }
    
        public double[] getDerivative() {
            return der;
        }
    
    
        public double calculatePolynom(double x) {
    
            double summa = 0;
            for (int i = 0; i < polynom.length; i++) {
                summa = summa + polynom[i] * (Math.pow(x, i));
            }
            return summa;
        }
    
        public double calculateDerivative(double x) {
    
            double summa = 0;
            for (int i = 0; i < der.length; i++) {
                summa = summa + der[i] * (Math.pow(x, i));
            }
            return summa;
        }
    
        public int compareTo( Polynom p ) {
            if( !p ) return 0;
            if( this.polynom == p.polynom )
                return 0;//The two are equal.
    
            double a,b;
            for( int iter = Math.max(polynom.length,p.polynom.length); iter > 0; iter-- )
            {
                a = (iter > polynom.length) ? 0 : polynom[iter-1];
                b = (iter > p.polynom.length) ? 0 : p.polynom[iter-1];
                if( a > b )
                    return 1;
                if( b > a )
                    return -1;
            }
            return 0;//Sanity check.
        }
    
        public String toString() {
    
            String p = "" + polynom[0];
            if (polynom.length > 1) {
                p = p + " + " + polynom[1] + "x";
            }
            int i = 2;
            while (i < polynom.length) {
                if (polynom[i] != 0) {
                    p = p + " + " + polynom[i] + "x^" + i;
                }
                i++;
            }
            return p;
        }   
    }
    (p.s. sorry if it doesn't work, I don't have a java VM installed on my comp atm to test it.)
    Last edited by Jotox; 01-09-2011 at 01:26 PM.

  3. #3
    Zantas's Avatar Contributor
    Reputation
    258
    Join Date
    Dec 2007
    Posts
    1,114
    Thanks G/R
    0/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a = (iter > polynom.length) ? 0 : polynom[iter-1];
    b = (iter > p.polynom.length) ? 0 : p.polynom[iter-1];

    not sure that works in java and what purpose does the "?" have?
    https://i45.tinypic.com/157df7r.jpg


  4. #4
    Jotox's Avatar Contributor
    Reputation
    250
    Join Date
    Mar 2008
    Posts
    282
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    if( !p ) return 0;
    This code returns 0 if p doesn't exist, or isn't instantiated for some reason.


    Code:
    a = (iter > polynom.length) ? 0 : polynom[iter-1];
    b = (iter > p.polynom.length) ? 0 : p.polynom[iter-1];
    What the "?" operator does is, it evaluates the expression before it (In this case, (iter > polynom.length) ) and returns the value before the colon (":") if the expression is true, and the value after the colon if the expression is false.
    it's a simple way to cut down on if/else statements and shorten code.

    So in this case, "a = (iter > polynom.length) ? 0 : polynom[iter-1]" just means a is set to 0 if the iterator is beyond the index of polynom, otherwise it sets a to the value at the proper index.
    Last edited by Jotox; 01-09-2011 at 12:37 PM. Reason: further explanation

  5. #5
    Zantas's Avatar Contributor
    Reputation
    258
    Join Date
    Dec 2007
    Posts
    1,114
    Thanks G/R
    0/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if( !p ) { doesn't seem to a working syntax for java.

    Fixed everything not working with your code so it's just that line that I'm having a problem with now.
    https://i45.tinypic.com/157df7r.jpg


  6. #6
    Jotox's Avatar Contributor
    Reputation
    250
    Join Date
    Mar 2008
    Posts
    282
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it may actually just be a C++ thing, my bad.

  7. #7
    jasejunk's Avatar Contributor
    Reputation
    118
    Join Date
    Apr 2008
    Posts
    640
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is what you want.
    If by smaller/larger you mean how large the array is.

    Code:
     class Polynom implements Comparable {
    
        private double[] polynom;
        private double[] der;
    
        /**
        polynom contains the coefficient of the term i such as the exponent is also i
         * example : 4x^7 +3x^2 is representend by
         *[0,0,3,0,0,0,0,4]
         */
        public Polynom(double[] coeff) {
            polynom = new double[coeff.length];
            for (int i = 0; i < coeff.length; i++) {
                polynom[i] = coeff[i];
            }
            der = new double[coeff.length - 1];
            getDeri();
        }
    
        private double[] getDeri() {
            for (int i = 1; i < polynom.length; i++) {
                der[i - 1] = polynom[i] * i;
            }
    
            return der;
        }
    
        public double[] getDerivative() {
            return der;
        }
    
    
        public double calculatePolynom(double x) {
            double summa = 0;
            for (int i = 0; i < polynom.length; i++) {
                summa = summa + polynom[i] * (Math.pow(x, i));
            }
            return summa;
        }
    
        public double calculateDerivative(double x) {
    
            double summa = 0;
            for (int i = 0; i < der.length; i++) {
                summa = summa + der[i] * (Math.pow(x, i));
            }
            return summa;
        }
    
            public String derToString() {
            String p = "0";
            if(der.length == 0) {
                p = "" + der[0];
            }
            if (der.length >= 1) {
                if(der[1] != 0) {
                    p = "" + der[1];
                }
            }
            int i = 2;
            int z = 0;
            while (i < der.length) {
                if (der[i] != 0 && z > 0) {
                    p = "" + p + " + " + der[i] + "x^" + i;
                    z++;
                }
                else if(der[i] != 0 && z == 0) {
                    p = "" + p + " + " + der[i] + "x^" + i;
                    z++;
                }
                i++;
            }
            return p;
        }
        
        public String toString() {
            String p = "0";
            if(polynom.length == 0) {
                p = "" + polynom[0];
            }
            if (polynom.length >= 1) {
                if(polynom[1] != 0) {
                    p = "" + polynom[1];
                }
            }
            int i = 2;
            int z = 0;
            while (i < polynom.length) {
                if (polynom[i] != 0 && z > 0) {
                    p = "" + p + " + " + polynom[i] + "x^" + i;
                    z++;
                }
                else if(polynom[i] != 0 && z == 0) {
                    p = "" + p + " + " + polynom[i] + "x^" + i;
                    z++;
                }
                i++;
            }
            return p;
        }   
        
        public int compareTo(Object x) {
            double test = 0;
            int i = polynom.length;
            Polynom y = (Polynom) x;
            int z = y.polynom.length;
            //int q = 0;
            if(i > z) {
                return 1;
            }
            else if(i < z) {
                return -1;
            }
            return 0;
            /*while(test==0)
            {
                test = polynom[i] - y.polynom[i];
                i--;
            }
            if(test>0)
            {
                return 1;
            }
            else if(test<0)
            {
                return -1;
            }
            else
            {
                return 0;
            }*/
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: 07-10-2012, 03:49 PM
  2. Need help with payment method
    By Utlimatesurvival in forum Community Chat
    Replies: 5
    Last Post: 12-14-2009, 10:27 AM
  3. Need help with payment method
    By Utlimatesurvival in forum Community Chat
    Replies: 1
    Last Post: 12-13-2009, 07:55 AM
  4. Need help with payment method
    By Utlimatesurvival in forum World of Warcraft Guides
    Replies: 1
    Last Post: 12-13-2009, 07:55 AM
  5. need help with shammy talents
    By jason in forum World of Warcraft General
    Replies: 5
    Last Post: 07-19-2006, 02:02 AM
All times are GMT -5. The time now is 03:56 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search