I am stuck in the plus and both times in the Polynomial class

public class PolynomialTerm
{
private int exponent = 0;
private int coefficient = 0;

public PolynomialTerm(int exp, int coeff)
{
int[] term = new int [2];

if (exp < 0)
throw new RuntimeException("The exponent is not valid!");
if (coeff == 0)
throw new RuntimeException("The coefficient is not valid!");

this.exponent = exp;
this.coefficient = coeff;
term[0] = exp;
term[1] = coeff;
}

public PolynomialTerm copy()
{
PolynomialTerm r = new PolynomialTerm(this.getExponent(), this.getCoefficient());
return r;
}
public PolynomialTerm plus (PolynomialTerm other)
{
int e = this.getExponent() + other.getExponent();
int c = this.getCoefficient() + other.getCoefficient();
PolynomialTerm total = new PolynomialTerm(e,c);
/*
Polynomial a = this;
Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
for (int i = 0; i <= b.deg; i++) c.coef[i] += b.coef[i];
c.deg = c.degree();
return c;*/

}
public PolynomialTerm times(PolynomialTerm other)
{
int e = this.getExponent() * other.getExponent();
int c = this.getCoefficient() * other.getCoefficient();
PolynomialTerm total = new PolynomialTerm(e,c);
}
public int getExponent()
{
return this.exponent;
}
public int getCoefficient()
{
return this.coefficient;
}
}

***************************************
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]);
}
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())
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);

// 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);

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

}
2
Contributors
1
2
Views
8 Years
Discussion Span

If I understood correctly your problem I think that you lack the basic of Maths.
I assume that a PolynomialTerm is this: aX^b: 4X^3.
If x=2 then the above would be: 4*(2*2*2) = 32

Then the addition can only be done only when the exponents are the same.
You cannot add these: 4X^3 + 4X^5. And when you add them it goes like this:
aX^e + bX^e = (a+b)X^e

For multiplication though it is like this:
aX^z * bX^k = (a*b)X^(z+k)

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.