所以我正在编写一个程序来添加和减去多项式。多项式以字符串形式出现(例如:4x ^ 7-2x ^ 5 + 3x ^ 2 + 78)并将其拆分为术语(示例4x ^ 7),然后将系数值分配给PolynomialArray [exponent]。
这是我的任务的第一部分,所以我在下面给了一个接口:
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.
}
到目前为止我的代码:
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;
}
}
我的问题在于添加mehthod,我如何在“其他”对象中引用PolynomialArray。当我做other.PolynomialArray [i]它说PolynomialArray无法解析或者不是接口中的字段感,没有这样的东西。我有办法在不改变界面的情况下引用我的目标,因为在我未来的项目中我将需要使用这个
很抱歉,如果我不清楚的话。这是我第一次发帖:)
*快速编辑。我没有完成我的代码所以这里和那里有一些占位符和一些随机的打印语句
答案 0 :(得分:0)
CreateDirectory "$InstDir"
这是您实施的课程Integer PolynomialArray[] = new Integer[1000];
添加的内容。它没有在您的接口合同中指定。您正尝试从您的界面参考中获取ArrayWithExponentAsIndexPolynomial
。那不行。您需要像PolynomialArray[]
答案 1 :(得分:0)
简单地说,PolynomialArray
字段在您的ArrayWithExponentAsIndexPolynomial
类中定义,并且在给定的PolynomialInterface
界面中是未知的。在实践中,只有方法在接口中定义,而不是字段(如提到的Amit.rk3,允许静态最终字段,但没有解决您的问题)。访问仅由接口标识的对象的字段的方法是在接口中定义getSomeField()
方法。
我不确定您的作业是否允许您将getPolynomialArray()
添加到给定界面,但这将是最简单的解决方案。
否则,虽然不那么优雅,但您可以将给定对象强制转换为您的类并直接访问该字段。