0

I have to create a class called Polynomials and other called PolynomialTerm. The latter one to copy, add and multiply PolynomialTerm (exponents, coefficients). I also have to do the same for the Polynomial(which is an arrayList of PolynomialTerms).

I am having problems adding the two polynomials.I need to do this using recursion.

3
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by dierobe
0

Show us what you've got for your recursive function and why you're having difficulty.

This is part of my program I already did the copy, I need help with the adding part.

public class Polynomial 
{

    public ArrayList<PolynomialTerm> pol = new ArrayList<PolynomialTerm>();

    public Polynomial()
    {
       ArrayList<PolynomialTerm> poli = new ArrayList<PolynomialTerm>();
    }
    public Polynomial(int[] data)
    {
         ArrayList<PolynomialTerm> rpl = new ArrayList<PolynomialTerm>();

         for (int i = 0; i < (data.length-1); i=i+2)
         {
             PolynomialTerm p = new PolynomialTerm(data[i],data[i+1]);
             rpl.add(p);
         }
        this.pol = rpl;
    }

    public ArrayList<PolynomialTerm> copy()
    {
        ArrayList<PolynomialTerm> duplicate = new  ArrayList<PolynomialTerm>();
        for (PolynomialTerm term: this.getPolynomial())
            duplicate.add(term);
        return duplicate;
    }
    public Polynomial plus(Polynomial other)
    {
        Polynomial plusTotal = new Polynomial();
        ArrayList <PolynomialTerm> c = this.copy();
        if (!(plusTotal.isZero()))
       {
           if (c.get(0).equals(other.))
       }

      return plusTotal;
    }

Edited by Reverend Jim: Fixed formatting

0

this is what I have so far

import java.util.ArrayList;


public class Polynomial 
{

    public ArrayList<PolynomialTerm> pol = new ArrayList<PolynomialTerm>();

    public Polynomial()
    {
       ArrayList<PolynomialTerm> poli = new ArrayList<PolynomialTerm>();
    }
    public Polynomial(int[] data)
    {
         ArrayList<PolynomialTerm> rpl = new ArrayList<PolynomialTerm>();

         for (int i = 0; i < (data.length-1); i=i+2)
         {
             PolynomialTerm p = new PolynomialTerm(data[i],data[i+1]);
             rpl.add(p);
         }
        this.pol = rpl;
    }
    public boolean isZero()
    {
        if (this.getPolynomial().isEmpty())
            return true;
        else 
            return false;
    }

    public ArrayList<PolynomialTerm> copy()
    {
        ArrayList<PolynomialTerm> duplicate = new  ArrayList<PolynomialTerm>();
        for (PolynomialTerm term: this.getPolynomial())
            duplicate.add(term);
        return duplicate;
    }
    public Polynomial plus(Polynomial other)
    {
        Polynomial plusTotal = new Polynomial();
        ArrayList <PolynomialTerm> c = this.copy();
        if (!(plusTotal.isZero()))
       {
           if (c.get(0).equals(other.))
       }

      return plusTotal;
    }
    public Polynomial times(PolynomialTerm that)
    {
       Polynomial mul1Total = new Polynomial();
       ArrayList<PolynomialTerm> thisOne = this.copy();
       if (!(mul1Total.isZero()))
       {
            PolynomialTerm r = (thisOne.remove(0).times(that.copy()));
            mul1Total.;
            sort(thisOne);     
       }

      return mul1Total;
    }
    public Polynomial times(Polynomial that)
    {       Polynomial mul2Total = new Polynomial();
       if (!(mul2Total.isZero()))
       {

       }
       return mul2Total;

    }

  public PolynomialTerm maximum (ArrayList<PolynomialTerm> polynomial)
   {
     if (polynomial.isEmpty())
         return null;
     if (polynomial.size()==1)
         return polynomial.get(0);

     PolynomialTerm uno = polynomial.remove(0);
     PolynomialTerm otro = maximum(polynomial);
     polynomial.add(0,uno);

 //    if (uno.compareTo(((Comparable)otro)>= 0))
         return uno;
    // else
      //   return otro;
   }

    public void sort(ArrayList<PolynomialTerm> polynomial)
    {
        if (polynomial.size()>1)
        {
           PolynomialTerm max = (PolynomialTerm) maximum(polynomial);
           polynomial.remove(max);
            sort(polynomial);
            polynomial.add(max);

        }
    }
     public ArrayList<PolynomialTerm> getPolynomial()
     {
        return this.pol;
     }

}

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.