java - Referring to an array in a subclass -
so im writing program add , subtract polynomials. polynomial comes in string (example: 4x^7-2x^5+3x^2+78) , split terms (example 4x^7) , coefficient value assigned polynomialarray[exponent].
this part 1 of assignment have interface given me below:
public interface polynomialinterface { polynomialinterface add(polynomialinterface other); // effect: adds value owner of addpolynomial method. // postcondition: return value = + value. polynomialinterface subtract(polynomialinterface other); // effect: subtracts value owner of addpolynomial method. // postcondition: return value = - value. void readpolynomial(); // postcondition: polynomial read. string tostring(); // postcondition: polynomial converted string. }
heres code 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]; (int = 0; <= splitterms.length - 1; i++) { system.out.println(splitterms[i]); } string temptemp = splitterms[1]; int coef; int exponent; string tempexp = null; (int = 0; < splitterms.length; i++) { string tempterm = splitterms[i]; system.out.println(); system.out.println("term working " + tempterm); boolean temppos = true; if (tempterm.contains("-")) { tempterm = tempterm.substring(1); system.out.println("after removing negative 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 " + 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 + " , exp is: " + exponent); polynomialarray[exponent] = coef; } } public polynomialinterface add(polynomialinterface other) { string finaloutput=null; //integer top = this.polynomialarray[i]; integer sum[] = new integer[100]; (int = 99; >= 1; i--){ integer top = this.polynomialarray[i]; integer bottom = other.polynomialarray[i]; sum[i] = top + bottom; } string tempoutput = null; (int = 99; >= 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; (int = 99; >= 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 in add mehthod, how refer polynomialarray in "other" object. when other.polynomialarray[i] says polynomialarray cannot resolved or not field sense in interface, there exists no such thing. id there way refer intended target without changing interface because in future project need use this
sorry if i'm not being clear. first time posting :)
*quick edit. i'm not done code there few place holders here , there , random print statements
integer polynomialarray[] = new integer[1000];
this implementing class arraywithexponentasindexpolynomial
has added. it's not specified in interface contract. trying polynomialarray[]
interface reference. won't work. need cast ((arraywithexponentasindexpolynomial)other).polynomialarray[i];
Comments
Post a Comment