0

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;
}
2
Contributors
1
Reply
5
Views
3 Years
Discussion Span
Last Post by JeffGrigg
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.