• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Adding the contents of two ArrayLists?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Jack Fletcher
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic