So im writing a program to add and subtract Polynomials. The Polynomial is comes in as a String (example: 4x^7-2x^5+3x^2+78) and its split up into Terms (example 4x^7) and then the coefficient value is assigned to PolynomialArray[exponent].
This is part one of my assignment so I have an Interface that was given to me below:
public interface PolynomialInterface {
PolynomialInterface add(PolynomialInterface other);
// Effect: Adds value to owner of addPolynomial method.
// Postcondition: Return value = this + value.
PolynomialInterface subtract(PolynomialInterface other);
// Effect: Subtracts value from owner of addPolynomial method.
// Postcondition: Return value = this - value.
void readPolynomial();
// Postcondition: polynomial read.
String toString();
// Postcondition: polynomial converted to string.
}
Heres my code so far:
import java.lang.*;
public class ArrayWithExponentAsIndexPolynomial implements PolynomialInterface {
Integer PolynomialArray[] = new Integer[1000];
CharSequence minus = "-";
CharSequence plusMinus = "+-";
boolean FirstElementPos = true;
public ArrayWithExponentAsIndexPolynomial(String input) {
if (input.charAt(0) == '-') {
input = input.substring(1);
FirstElementPos = false;
}
String inputPolynomial = input.replaceAll("-", "+-");
// input.replace(minus, plusMinus);
System.out.println(inputPolynomial);
String[] splitTerms = inputPolynomial.split("\\+");
// int PolynomialArray[] = new int[100];
for (int i = 0; i <= splitTerms.length - 1; i++) {
System.out.println(splitTerms[i]);
}
String tempTemp = splitTerms[1];
int coef;
int exponent;
String tempExp = null;
for (int i = 0; i < splitTerms.length; i++) {
String tempTerm = splitTerms[i];
System.out.println();
System.out.println("Term we are working with " + tempTerm);
boolean tempPos = true;
if (tempTerm.contains("-")) {
tempTerm = tempTerm.substring(1);
System.out.println("After removing negative from term: "
+ tempTerm);
tempPos = false;
}
int IndexOfexponent = tempTerm.indexOf('^');
if (IndexOfexponent == -1) {
exponent = 1;
// FirstElementPos = true;
} else {
tempExp = tempTerm.substring(IndexOfexponent + 1);
exponent = Integer.parseInt(tempExp);
}
System.out.println("The exp is " + exponent);
// String tempTerm = splitTerms[i];
System.out.println("The term rn is: " + tempTerm);
String tempTermNoCarrot = tempTerm.replaceAll("\\^" + tempExp, "");
String tempCoef = tempTermNoCarrot.replaceAll("x", "");
// String tempCoef = tempTermNoX.replaceAll(tempExp, "");
System.out.println("THe Coeff rn is: " + tempCoef);
coef = Integer.parseInt(tempCoef);
if (tempPos == false || FirstElementPos == false) {
coef = (coef * -1);
}
System.out.println("After everything, Coef is:" + coef
+ " and exp is: " + exponent);
PolynomialArray[exponent] = coef;
}
}
public PolynomialInterface add(PolynomialInterface other) {
String finalOutput=null;
//Integer top = this.PolynomialArray[i];
Integer Sum[] = new Integer[100];
for (int i = 99; i >= 1; i--){
Integer top = this.PolynomialArray[i];
Integer bottom = other.PolynomialArray[i];
Sum[i] = top + bottom;
}
String tempOutput = null;
for (int i = 99; i >= 1; i--) {
if (Sum[i] != null && Sum[i] != 0) {
tempOutput += "+";
int outputCoef = Sum[i];
tempOutput += outputCoef;
tempOutput += "x^";
tempOutput += i;
}
}
String RemoveNull = tempOutput;
tempOutput = RemoveNull.replaceAll("null", "");
if (tempOutput.charAt(0) == '+') {
tempOutput = tempOutput.substring(1);
}
tempOutput = tempOutput.replaceAll("\\+-","-");
finalOutput = tempOutput;
return new ArrayWithExponentAsIndexPolynomial(finalOutput);
}
public PolynomialInterface subtract(PolynomialInterface other) {
return other;
}
public void readPolynomial() {
}
public String toString() {
String output = null;
for (int i = 99; i >= 1; i--) {
if (PolynomialArray[i] != null && PolynomialArray[i] != 0) {
output += "+";
int outputCoef = PolynomialArray[i];
output += outputCoef;
output += "x^";
output += i;
}
}
String outputTemp = output;
output = outputTemp.replaceAll("null", "");
if (output.charAt(0) == '+') {
output = output.substring(1);
}
output = output.replaceAll("\\+-","-");
return output;
}
}
My question is in the add mehthod, how do i refer to the PolynomialArray in the "other" object. When i do other.PolynomialArray[i] it says PolynomialArray cannot be resolved or is not a field sense in the Interface, there exists no such thing. Id there a way to refer to my intended target without changing the interface because in my future project I will need to use this
Sorry if I'm not being clear. This is my first time posting :)
*quick edit. I'm not done with my code so there are a few place holders here and there and some random print statements