• 05-24-2011, 10:07 AM
fusi0n556

so all i really want help is with understanding how i use linked list in polynomials to finish off final static private void add() function, from there i believe i could do the rest, i know some people will say we dont help in plagiarism or doing someones homework is bad, but seriously the best way i learn is to see a familiar example and break it down to see how it works.

this is the polynomial class
Code:

```import java.util.*; /**  * Assignment 2 Polynomial Data Structure, Constructor and method signatures.  *  * @author ()  * @version (1.0)  */ class Polynomial {     final static private int mantissa=52;     final static private double epsilon=Math.pow(2.0,-mantissa);     private double coefficient=0.0;     private int power=0;     private Polynomial successor=null;     public Polynomial(double coefficient, int power)     {         if (Double.isNaN(coefficient)) return;         if (Math.abs(coefficient)<epsilon) return;         if (power<0) return;         this.coefficient=coefficient;         this.power=power;     }     /* conditions     this(X) => sum(coefficient(i)*X^i:for(i=degree;i>=0;i--)) && X^0=1     this.degree()==this.power==highestPower     this.highestPoweredCoefficient==this.coefficient     this.successor!=null links to terms whose powers uniquely decrease     Polynomial(0.0,0)=(coefficient=0.0,power=0,successor=null)==0.0     if (coefficient==NaN) coefficient=0.0     if (abs(coefficient)<epsilon)) coefficient=0.0     if (power<0) power=0     if (this.degree()==0) abs(coefficient(0))>=0.0     if (this.degree()>0) abs(coefficient(i))>=epsilon for all i>=0     */ [U]    final static private void add(Polynomial polynomial, double coefficient, int power)     {         if (polynomial==null) return;         if (Math.abs(coefficient)<epsilon) coefficient=0.0;         if (coefficient==0.0) return;         if (power<0) return;          /* add more */[/U]             }         final public int cardinality()     {         int count=1;         Polynomial traverser=this.successor;         while (traverser!=null)         {             count++;             traverser=traverser.successor;         }         return count;     }         final public Polynomial clone()     {         Polynomial result=new Polynomial(0.0,0);         result.coefficient=this.coefficient;         result.power=this.power;         Polynomial traverserThis=this;         Polynomial traverserResult=result;         while (traverserThis.successor!=null)         {             traverserResult.successor=new Polynomial(0.0,0);             traverserThis=traverserThis.successor;             traverserResult=traverserResult.successor;             traverserResult.coefficient=traverserThis.coefficient;             traverserResult.power=traverserThis.power;         }         return result;     }         final public double coefficient(int power)     {         if (power<0) return 0.0;         Polynomial traverser=this;         do         {             if (traverser.power<power) return 0.0;             if (traverser.power==power) return traverser.coefficient;             traverser=traverser.successor;         }         while (traverser!=null);         return 0.0;     }     final public Polynomial composite(Polynomial that)     {         if (that==null) return null;         /* more to add */     }         final public int degree()     {         return this.power;     }     final public Polynomial differentiate()     {         if (this.power==0) return new Polynomial(0.0,0);         /* more to add */     }         final public Polynomial[] dividedBy(Polynomial that)     {         if (that==null) return null;         if (that.coefficient==0.0) return null;         Polynomial quotient=new Polynomial(0.0,0);         Polynomial remainder=this.clone();         /* more to add */         Polynomial[] result=new Polynomial[2];         result[0]=quotient;         result[1]=remainder;         return result;     }         final public boolean equals(Polynomial that)     {         if (that==null) return false;         if (this.coefficient!=that.coefficient) return false;         if (this.power!=that.power) return false;         if (this.successor==null && that.successor==null) return true;         if (this.successor==null && that.successor!=null) return false;         if (this.successor!=null && that.successor==null) return false;         return this.successor.equals(that.successor);     }         final public double evaluate(double variable)     {         if (Double.isNaN(variable)) variable=0.0;         if (Math.abs(variable)<epsilon) variable=0.0;         double value=0.0;         /* more to add */         return value;     }         final public Polynomial integrate()     {         if (this.coefficient==0.0) return new Polynomial(0.0,0);         Polynomial result=this.clone();         /* more to add */         return result;     }         final public Polynomial minus(Polynomial that)     {         if (that==null) return null;         if (this.equals(that)) return new Polynomial(0.0,0);         Polynomial result=this.clone();         if (that.coefficient==0.0) return result;         Polynomial traverser=that;         do         {             add(result,-traverser.coefficient,traverser.power);             traverser=traverser.successor;         }         while (traverser!=null);         return result;     }         final public Polynomial plus(Polynomial that)     {         if (that==null) return null;         if (this.coefficient==0.0) return that.clone();         Polynomial result=this.clone();         if (that.coefficient==0.0) return result;         Polynomial traverser=that;         do         {             add(result,traverser.coefficient,traverser.power);             traverser=traverser.successor;         }         while (traverser!=null);         return result;     }         final public int powerMax()     {         int max=Integer.MIN_VALUE;         Polynomial traverser=this;         do         {             if (max<traverser.power) max=traverser.power;             traverser=traverser.successor;         }         while (traverser!=null);         return max;     }     final public int powerMin()     {         int min=Integer.MAX_VALUE;         Polynomial traverser=this;         do         {             if (min>traverser.power) min=traverser.power;             traverser=traverser.successor;         }         while (traverser!=null);         return min;     }         final public Polynomial times(Polynomial that)     {         if (that==null) return null;         Polynomial result=new Polynomial(0.0,0);         /* more to add*/         return result;     }         final public String toString()     {         String string=""+this.coefficient+(this.power==0?"":"*X^"+this.power);         Polynomial traverser=this.successor;         while (traverser!=null)         {             string+=             (traverser.coefficient>0.0?"+":"")+traverser.coefficient+             (traverser.power==0?"":"*X^"+traverser.power);             traverser=traverser.successor;         }         return string;     } }```

--------------------------------------------

this is the tester
Code:

```abstract class PolynomialTester { static Polynomial p=null;   static Polynomial p0=new Polynomial(-1.0,0);   static Polynomial q0=new Polynomial(0.0,0);   static Polynomial r0=new Polynomial(1.0,0);   static Polynomial p1=new Polynomial(-1.0,1).                       plus(new Polynomial(1.0,0));   static Polynomial p2=new Polynomial(1.0,2).                       plus(new Polynomial(-1.0,1)).                       plus(new Polynomial(1.0,0)).                       plus(new Polynomial(1.0,2));   static Polynomial p3=new Polynomial(-3.0,3).                       plus(new Polynomial(2.0,2)).                       plus(new Polynomial(-1.0,1)).                       plus(new Polynomial(0.5,0)).                       plus(new Polynomial(0.5,0));   static Polynomial p4=new Polynomial(4.0,4).                       plus(new Polynomial(-2.0,2)).                       plus(new Polynomial(1.0,0));   static Polynomial p5=new Polynomial(-5.0,5).                       plus(new Polynomial(3.0,3)).                       plus(new Polynomial(-1.0,1));   static Polynomial q5=new Polynomial(-0.5,1).                       plus(new Polynomial(-2.5,5)).                       plus(new Polynomial(2.5,5)).                       plus(new Polynomial(-5.0,5)).                       plus(new Polynomial(3.0,3)).                       plus(new Polynomial(-0.5,1));   static Polynomial s0=new Polynomial(-5.0,5).                       plus(new Polynomial(5.0,5)).                       plus(new Polynomial(3.0,3)).                       plus(new Polynomial(-1.0,1)).                       plus(new Polynomial(1.0,1)).                       plus(new Polynomial(-3.0,3));   static Polynomial[] polynomials={p,p0,q0,r0,p1,p2,p3,p4,p5,q5,s0};   static double[] variables={-2.0, -1.0, 0.0, 1.0, 2.0};   static void main()   { for (int i=0; i<polynomials.length; i++)       System.out.println("\npolynomials["+i+"]="+polynomials[i]);     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       System.out.println("\n("+polynomials[i]+").cardinality()="+                         polynomials[i].cardinality());     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       System.out.println("\n("+polynomials[i]+").clone()=\n"+                         polynomials[i].clone());     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       for (int power=polynomials[i].degree()+1; power>-1; power--)         System.out.println("\n("+polynomials[i]+").coeff("+power+")="+                           polynomials[i].coefficient(power));     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       for (int j=0; j<polynomials.length; j++)         System.out.println("\n("+polynomials[i]+").composite("+                           polynomials[j]+")=\n"+                           polynomials[i].composite(polynomials[j]));     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       System.out.println("\n("+polynomials[i]+").degree()="+                         polynomials[i].degree());     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       System.out.println("\n("+polynomials[i]+").differentiate()=\n"+                         polynomials[i].differentiate());     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       for (int j=0; j<polynomials.length; j++)       { Polynomial[] result=polynomials[i].dividedBy(polynomials[j]);         System.out.println("\n("+polynomials[i]+").dividedBy("+                           polynomials[j]+"):"+                           (result==null?" null":                               "\n  quotient="+result[0]+                               "\n  remainder="+result[1]));       }     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       for (int j=0; j<polynomials.length; j++)         System.out.println("\n("+polynomials[i]+").equals("+                           polynomials[j]+")=\n"+                           polynomials[i].equals(polynomials[j]));     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       for (int j=0; j<variables.length; j++)         System.out.println("\n("+polynomials[i]+                           ").evaluate("+variables[j]+")="+                           polynomials[i].evaluate(variables[j]));     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       System.out.println("\n("+polynomials[i]+").integrate()=\n"+                         polynomials[i].integrate());     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       for (int j=0; j<polynomials.length; j++)         System.out.println("\n("+polynomials[i]+").minus("+                           polynomials[j]+")=\n"+                           polynomials[i].minus(polynomials[j]));     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       for (int j=0; j<polynomials.length; j++)         System.out.println("\n("+polynomials[i]+").plus("+                           polynomials[j]+")=\n"+                           polynomials[i].plus(polynomials[j]));     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       System.out.println("\n("+polynomials[i]+").powerMax()="+                         polynomials[i].powerMax());     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       System.out.println("\n("+polynomials[i]+").powerMin()="+                         polynomials[i].powerMin());     }     for (int i=0; i<polynomials.length; i++)     { if (polynomials[i]==null) continue;       for (int j=0; j<polynomials.length; j++)         System.out.println("\n("+polynomials[i]+").times("+                           polynomials[j]+")=\n"+                           polynomials[i].times(polynomials[j]));     }   } } // Math.pow(-0.0,-even) == POSITIVE_INFINITY == +1.0/0.0 // Math.pow(-0.0, -odd) == NEGATIVE_INFINITY == -1.0/0.0 // Math.pow(-0.0,    0) == +1.0 // Math.pow(-0.0,+even) == +0.0 // Math.pow(-0.0, +odd) == -0.0 // Math.pow(+0.0, -int) == POSITIVE_INFINITY == +1.0/0.0 // Math.pow(+0.0,    0) == +1.0 // Math.pow(+0.0, +int) == +0.0 // Math.pow(double, 0) == +1.0 // 0.0 == -0.0```