I have two ArrayLists and I want to add the contents of them. I understand a for loop would be the way to implement this but am struggling with how to pull in the and operate on the parameters.
Here is the code for my project so far:
The first part of the task required me to evaluate a polynominal using a given value. With the addition of them I need to print the string without evaluating the polynominal that is created.
The purpose of the second part of the task is to add two of these Polynominal objects and then print the new polynominal.
For example, poly1 = 7 + x + 3x^4 and poly2 = x + x^2.
The eventual result should be a string which contains the polynominal achieved when the two are added, here it would be: 7 + 2x + x^2 + 3x^4
Any help would be appreciated. Sorry if I haven't explained very well.
I am not sure where your problem is, you posted a lot of code and said there are two parts to the assignment but you didn't make it clear what problems you are having. So what specific problem are you having?
Joined: Oct 09, 2012
Apologies for not explaining the problem properly.
Basically I need to add the contents of one arraylist (poly1) to the contents of another (poly2).
I want poly3 to be an arraylist which contains the results of performing an addition of the contents of each index in the other 2 arraylist objects.
So poly3[i] will be equal to poly1[i] + poly2[i].
Then I will run my evaluate function on this newly created arraylist object (poly3).
Okay, can you post the code (or point out what line numbers in your original post) show how you have started working on that issue? I am not sure I see it there (but I might be looking at the wrong spot).
As addition to Steve's post, I would suggest you don't skip steps and move right into implementing further functionalities, before completing previous ones.
Your class design is wrong, for start. Or at least I wouldn't do it this way. The only thing you should keep in it (hence, the only field) should be array of coefficients; field sum is never used in your class (nor it's needed there) and for degree you can declare method that calculates it any time you need it. And, fields really shouldn't be declared as public.
Once you have the class design that should actually be in OO spirit, you should go and implement one method at a time. As I can see from your code, only evaluate(int x) method is working properly (though I would at least name local variable(s) differently); toString() is just outputting coefficients, and not the representation one would expect; as for add(...) method, even the signature is wrong so the code won't compile. On the other hand, that one should be static the way you intent it to work.
The quieter you are, the more you are able to hear.