This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Adding the contents of two ArrayLists? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding the contents of two ArrayLists?" Watch "Adding the contents of two ArrayLists?" New topic
Author

Adding the contents of two ArrayLists?

Jack Fletcher
Greenhorn

Joined: Oct 09, 2012
Posts: 13
Hi guys,

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:

Polynominal class:


Test class:


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.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Welcome to the Ranch!

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?


Steve
Jack Fletcher
Greenhorn

Joined: Oct 09, 2012
Posts: 13
Hi Steve,

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).

Thanks!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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).
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Kemal Sokolovic wrote:only evaluate(int x) method is working properly (though I would at least name local variable(s) differently);


But it is not effective - Horner's rule for polynomial evaluation is pretty simple and more effective.

http://rosettacode.org/wiki/Horner%27s_rule_for_polynomial_evaluation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding the contents of two ArrayLists?