How to set up an array with related items

235 views Asked by At

I am trying to make a program that will allow me to manipulate multi-variable polynomials. I want to be able to deal with expressions that have more than one variable, and each with its own exponent. For example, one example of a polynomial object will contain information from 5x^2*y^3.

I want to store this polynomial's information in instance variables: an int for the coefficient, a String[] for the variables, and an int[] for the exponents for each variable.

How would I go about linking the two arrays to relate each variable to it's own exponent? I do not want to just have the variable and it's exponent in the same index of different arrays. I would like to know how to have a more guaranteed way that the data will be properly handled.

4

There are 4 answers

0
Levent Divilioglu On BEST ANSWER

First create a POJO class to store polynomial variables in the fields coefficient, variable, exponent.

Then call them from another class;

Here is the POJO class, I call it Element;

public class Element {

    private double coefficient;
    private int exponent;
    private String variable;

    public Element(double coefficient, String variable, int exponent ) {
        this.coefficient = coefficient;
        this.variable = variable;
        this.exponent = exponent;
    }

    public double getCoefficient() {
        return coefficient;
    }

    public void setCoefficient(double coefficient) {
        this.coefficient = coefficient;
    }

    public String getVariable() {
        return variable;
    }

    public void setVariable(String variable) {
        this.variable = variable;
    }

    public int getExponent() {
        return exponent;
    }

    public void setExponent(int exponent) {
        this.exponent = exponent;
    }

}

And the Test Code is as follows;

public class TestPolynomials {

    public static void main(String[] args) {
        Element[] polynomialList = { new Element(5,"x",2), new Element(1,"y",3), new Element(6,"1",0) };

        printPolynomials(polynomialList);

    }

    public static void printPolynomials( Element[] pList ) {
        for( int i = 0; i < pList.length; i++ ) {
            System.out.printf("%2.1f*%s*%d, ", pList[i].getCoefficient(), pList[i].getVariable(), pList[i].getExponent() );
        }
    }

}

The output is as follows;

5.0*x*2
1.0*y*3
6.0*1*0
2
Sh4d0wsPlyr On

Depending on the implementation of your project, the need to create a solid connection between the two might be irrelevant. I will provide an example, because there is an easy way to reference both arrays and get the matching data.

int[] coefficients = ...
String[] exponents = ...

And for referencing, lets say you want the second polynomial... All you need to do is reference the same place in both arrays to get the matching value. Just be sure to update the arrays in tandum, so they do not fall out of sync. I might recommend a specific method for updating them.

//Do some action
coefficients[1].action
exponents[1].action
0
Hughzi On

Why not create a Polynomial class:

Public class Polynomial {
int coefficient;
String variables [];
int exponents [];
public Polynomial (int coeff, String [] var, int [] expo) {...}

then just have an array of polynomial objects..

Polynomial polys [] = new Polynomial[200];

then all of your data is kept in the necessary object and easily access or maintained. if you need a dynamics storage solution use ArrayList instead.

0
Davide Lorenzo MARINO On

Use a single array with a new class like the following

public class Element {
    private String variable;
    private int coefficient;
    private int exponent;
    ...
}

Then you can create an array like the following:

public Element[] elements;

Please note that you have never talk about the operators... probably you have to add something more to that structure to know the operator between elements.