I have looked through multiple other posts on this topic, so I apologize if I am reposting previously discussed things here. Here is one of the test methods my main method calls in my testing class:
public static void test1(){
PolynomialA p = new PolynomialA();
//should return true
System.out.println(p.isEmpty());
if(p.isEmpty()){
p.addTerm(2, 3);
p.addTerm(3, 2);
//should print (2)x^(3) + (3)x^(2)
System.out.println(p.toString());
//should return false
System.out.println(p.isEmpty());
}
}
Now, for whatever reason, add term is not adding the terms, and the Arraylist stays empty. So, ill show you my constructor of PolynomialA:
public class PolynomialA implements Quantity{
private ArrayList<Term> list;
PolynomialA(){
list = new ArrayList<Term>();
}//end constructor
And the addTerm() method where I am trying to add a Term(will show below) to the ArrayList list.
public void addTerm(int c, int e){
Term temp;
for(int i = 0; i < list.size(); i++){
//if a term with this exponent already exists in the polynomial
if(list.get(i).exponent == e){
//just add the passed coefficient to the existing one
temp = new Term((list.get(i).coefficient + c), e);
list.set(i, temp);
}//end if
//else: a term with this exponent does not exist
else{
//add the new term with the passed coeff and expo to the end
temp = new Term(c, e);
list.add(temp);
}//end else
}//end for
}//end addTerm()
The entire Term Class:
public class Term{
int coefficient;
int exponent;
Term(){
coefficient = 0;
exponent = 0;
}
Term(int c, int e){
coefficient = c;
exponent = e;
}
}
So, basically, polynomial is an ArrayList of Term's, which a term has 2 integer values associated with it: coefficient, and exponent. There are many other methods throughout the PolynomialA class, but this is the first one I need working in order for any of the others to create one. For whatever reason, i cannot append a term to the empty ArrayList. I am getting no exceptions thrown or anything, just doesnt add the term to the ArrayList. PLEASE HELP
Also, I apologize if these snippits of code were put up here in a bass-ackwards way.