So this is what i dit. Declared three classes: Main Term Polynom

So in Polynom i try to call a "plus" method:

i tried this:

public Polynom add(Polynom pol){

    Polynom res = new Polynom("",0);
    Term tox = new Term(0,0);

    for(Term p : Polynom){


            for(Term other : pol.Polynom ){
                if(p.getDeg()==other.getDeg()){
                    tox.setCoef(p.getCoef()+ other.getCoef());
                    tox.setDeg(p.getDeg());
        }
                res.addTerm(tox);
            }


    }
    return res;
}

And I am facing a problem : I reffer to an object this and iam getting into an other object which is a this too. So when i try to add two polynoms I have wrong results.

Is there a good method for adding two Polynoms ?

private ArrayList<Term> Polynom = new ArrayList<Term>();
private String name;
private double number;

And the Term is :

public class Term {
private int deg;
private double coef;
}

You probably have to create a new Term instance for each "degree" that matches. That's why you always have one (or none) in your result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.