I'm working on this polynomial program but I have stuck at some points which still don't know what's wrong exactly. So anyway, I have to add 2 polynomials, subtracts, and multiply. So could anyone please correct this code below.
Thank you very much!!
public class Polynomial implements Cloneable {
private int m_degree;
private double[] m_coeff;
public Polynomial() {
m_degree = 0;
m_coeff = new double[5];
}
// Set the x0 coefficient
public Polynomial(double c0) {
this();
m_coeff[0] = c0;
}
// COPY CONSTRUCTOR
public Polynomial(Polynomial source) {
super();
source = new Polynomial();
source.clone();
}
// clone method
public Polynomial clone() {
Polynomial polynomial;
try {
polynomial = (Polynomial) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(
"This class doesn't implement Cloneable ");
}
polynomial.m_coeff = m_coeff.clone();
return polynomial;
}
// getter
private double get(int k) {
return (m_coeff[k]);
}
// setter
public double set(int k, double coeff) {
double oldElement = get(k);
m_coeff[k] = coeff;
return (oldElement);
}
// ADD to coefficient
public void addToCoef(double amount, int k) {
m_coeff[k] = amount;
amount++;
}
// ASSIGN COEFFICIENT
public void assignCoef(double newCoeff, int k) {
m_coeff[k] = newCoeff;
}
// TO REMOVE
public double remove(int k) {
double removeElement = get(k);
System.arraycopy(m_coeff, k - 1, m_coeff, k, degree() - k - 1);
m_coeff[k] = m_coeff[m_degree];
--m_degree;
return (removeElement);
}
// convert to string 3.2x^0 + -2.5x^2 + 6.1x^3
public String toString() {
return (coefficient(m_degree) + "x^" + m_degree + " " + "x^" + nextTerm(m_degree) + " x^ " );
}
// to CLEAR
public void clear() {
for (int i = 0; i <= m_coeff.length; i--) {
m_coeff[i] = 0;
}
}
// To reserve
public void reserve(int degree) {
if (degree <= m_coeff.length) {
throw new IllegalArgumentException("Degree is negative: " + degree);
}
double[] newArray = new double[degree];
System.arraycopy(m_coeff, 0, newArray, 0, degree());
m_coeff = newArray;
}
// DOUBLE COEFFICIENT
public double coefficient(int k) {
return m_coeff[k];
}
// INT DEGREE
public int degree() {
int degree = 1;
if (m_coeff.length > 0) {
for (int i = 0; i < m_coeff.length; i++) {
if (m_coeff[i] != 0)
degree = i;
}
}
return (degree);
}
// NEXT_TERM; calling the next term of k
public int nextTerm(int k) {
for(int i=k;k<m_coeff.length;++i){
if(m_coeff[i]!=0);
}
return -1;
}
// to evaluate
public double eval(double x) {
double p = m_coeff[m_degree];
for (int i = 0; i < degree(); i++)
p = m_coeff[i] + (x * p);
return p;
}
// Arithmetic Operators (ADD, SUBTRACT, MULTIPLY OF TWO POLYNOMIALs
public static Polynomial add(Polynomial p1, Polynomial p2) {
Polynomial sum = new Polynomial();
if(p1.degree()<=p2.degree())
for(int i=0;i<p1.degree();i++){
sum.reserve(i);
sum.addToCoef(p1.coefficient(i), i);
sum.addToCoef(p2.coefficient(i), i);
}
else{
for(int i=0;i < p2.degree();i++){
sum.reserve(i);
sum.addToCoef(p1.coefficient(i), i);
sum.addToCoef(p2.coefficient(i), i);
}
}
return sum;
}
public static Polynomial subtract(Polynomial p1, Polynomial p2) {
Polynomial subtract = new Polynomial();
int i=0;
while(p1.nextTerm(i)!=-1||p2.nextTerm(i)!=-1){
subtract.assignCoef(p1.coefficient(i),i);
subtract.addToCoef((-1)*p2.coefficient(i), i);
i++;
}
return subtract;
}
public static Polynomial multiply(Polynomial p1, Polynomial p2) {
Polynomial multiply = new Polynomial();
for (int i = 0; i <= p1.m_degree; i++)
for (int j = 0; j <= p2.m_degree; j++)
multiply.m_coeff[i + j] += (p1.m_coeff[i] * p2.m_coeff[j]);
multiply.degree();
return multiply;
}
/**don't know how to start
public static Polynomial derivative (Polynomial p1){
return;
}
**/
}