0

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

2 Answers2

0

Integer PolynomialArray[] = new Integer[1000];

This is something which your implementing Class ArrayWithExponentAsIndexPolynomial has added. It's not specified in your interface contract. You are trying to get the PolynomialArray[] from your interface reference. That won't work. You need to cast it like ((ArrayWithExponentAsIndexPolynomial)other).PolynomialArray[i];

Amit.rk3
  • 2,417
  • 2
  • 10
  • 16
0

Simply put, the PolynomialArray field is defined in your ArrayWithExponentAsIndexPolynomial class and unknown to the given PolynomialInterface inferface. In practice, only methods are defined in an interface, not fields (as Amit.rk3 mentioned, static final fields are allowed, though no solution to your problem). A way to access a field of an object indentified solely by an interface, is to define a getSomeField() method in the interface.

I'm not sure if your assignment allows you to add getPolynomialArray() to the given interface, but that would be the simplest solution.

Otherwise, though less elegant, you can cast the given object to your class and access the field directly.

  • **In fact, only methods can be defined in an interface, not fields** - this isn't correct. Interfaces can have fields. It's just that they have to be `pubic static final`. Refer [http://stackoverflow.com/questions/9446893/fields-in-interfaces](http://stackoverflow.com/questions/9446893/fields-in-interfaces). – Amit.rk3 Jun 14 '15 at 21:45
  • @Amit.rk3 thank you, I didn't know that, I'll re-phrase my answer. Though I don't think I'll ever use it, and it's clearly of no help to the OP. – Hummeling Engineering BV Jun 14 '15 at 21:53