My code is used to implement a polynomial and turn it into its derivative counterpart for instance 3x^2 + 2x derivative is 6x + 2 but for some reason every time I run my code in the main method it says I'm getting an arrayIndexOutOfBoundException sometimes even type mismatch, is the logic to my code wrong?
public class Der {
private static Der[] polyVal;
private String ope;
private int coe;
private String var;
private int exp;
private String ope2;
private static int deg;
Der(String ope, int coe, String var, int exp, String ope2) {
this.ope = ope;
this.coe = coe;
this.var = var;
this.exp = exp;
this.ope2 = ope2;
}
//Accessor methods
public String getOpe() {return this.ope;}
public int getCoe() {return coe;}
public String getVar() {return var;}
public int getExp() {return exp;}
public String getOpe2() {return this.ope2;}
// mutator methods
public void setOpe(String newOpe) {this.ope = newOpe;}
public void setCoe(int newCoe) {this.coe = newCoe;}
public void setVar(String newVar) {this.var = newVar;}
public void setExp(int newExp) {this.exp = newExp;}
public void setOpe2(String newOpe2) {this.ope2 = newOpe2;}
public static void setEle(int degree) {
deg = degree;
polyVal = new Der[deg+1];
}
/*
public void setValues(Der p) {
int i = 0;
for(i; i < deg; i++) {
}
}
*/
public void addObj(Der newDer) {
int size = polyVal.length-1;
int k = 0;
while(polyVal[k] == null && k < size) {
polyVal[k] = newDer;
k++;
}
}
public static Der[] differentiate(Der[] polynomial) throws IllegalArgumentException {
Der[] diffPoly = polynomial.clone();
for(int i = 0; i < deg+1; i++) {
Der newP = diffPoly[i];
int dCoe = newP.getCoe();
if(dCoe == 0) {dCoe = 1;}
String dVar = newP.getVar();
if(dVar.indexOf(1) != '^') {throw new IllegalArgumentException("Invalid format");}
int dExp = newP.getExp();
if(dExp != 0 || dExp != 1) {
dCoe = dCoe * dExp;
dExp = dExp-1;
newP.setCoe(dCoe);
newP.setExp(dExp);
//newP.setVar(this.var);
//newP.setOpe(this.ope);
//newP.setOpe2(this.ope2);
}else if(dExp == 1) {
dExp = 0;
newP.setExp(dExp);
//newP.setVar(this.var);
//newP.setOpe(this.ope);
//newP.setOpe2(this.ope2);
}else if(dExp == 0 || dVar == "") {
dCoe = 0;
newP.setCoe(dCoe);
//newP.setVar(this.var);
//newP.setOpe(this.ope);
//newP.setOpe2(this.ope2);
}
diffPoly[i] = newP;
if(diffPoly[0].getOpe() == "+") {throw new IllegalArgumentException("Invalid format");}
}
return diffPoly;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("pleas enter the degree of the polynomial");
int degreeVal = sc.nextInt();
//setEle(degreeVal);
System.out.println("Now start adding in the expression in the format" + "- or noting" + "some int" + "some letter with ^" + "some int" + "+ or -");
Der[] polynomial = new Der[degreeVal];
int j = 0;
while(j < degreeVal+1){
Der obj = new Der(sc.next(), sc.nextInt(), sc.next(), sc.nextInt(), sc.next());
polynomial[j] = obj;
j++;
}
differentiate(polynomial);
for(int u = 0; u < degreeVal; u++) {
System.out.println(polynomial[u]);
}
}
}