Why are you using Math pow, which is intended for fractional arguments, when your numbers are all integers? Why have you got all those if s in the toString method? As soon as I saw that, I started to worry. Why have you got parallel Iterators? That worries me just to look at it. What is the algorithm for each of the operations you are executing? When you have got the algorithms worked out, then you will be able to write code that actually works.
Plus li2Term ; result. Times li2Term ; newresult. Plus term1 ; System. Multiply poly2 ; System. Anton Golovin. Hi, Serge, This code calls for breaking the overall code into smaller parts and combining them. Then, the error will show itself easily. Otherwise you should really love polynomials in order to delve into this code for the error. With best regards, Anton. Piet Souris. So there is quite some work to do! Well, so far my firt impressions.
Edit: making a Map of all the terms of your two polynomials, with key the exponent, and then reducing all the values, would be both easier and more elegant, but I don't know if you are familiar with Maps.
There are three kinds of actuaries: those who can count, and those who can't. As Piet suggests, your PolynomialTerm's Plus method is incorrectly designed. It would also be less difficult for other people to read if you followed the Java standard where method names start with a lower-case character i. Fred Kleinschmidt. This code is extremely inefficient for a Polynomial class, especially when computing the value of the polynomial.
There are also easy ways to optimize this for vary large polynomial orders where many of the coefficients are zero, and also for including fractional parts negative powers. And Fred's design makes the "add" and "derivative" methods much, much easier to write. It probably makes the "multiply" method easier as well. The polynomialterm class is a requirement for the assignment. Although Freds remark is very interesting, I think it is better to leave that for now, to come back to it later.
And Paul is correct too: the sum of two PolynomialTerms is usually not another PolynomialTerm, and that is where you go wrong. To illustrate that better, I have added this constructor to your Polynomial class: public Polynomial PolynomialTerm So, since the sum of two PolynomialTerms is not always another Polynomial, lets remove that method there and put it in the Polynomial class.
Now, when adding two polynomials, it seems easiest to simply merge the two lists, but then we could get two terms with equal exponent. There are several ways to correct for this situation, I mentioned two possibilities, but there are other ones as well. Then, indeed, if you have a PolynomialTerm with exponent 0, then the derivative is 0. Well, enough for now, I better shut up.
You can only return one. That's why I said the method was incorrectly designed. Does the specification you were given not say what should happen in the example I gave? If I were the programmer faced with that bad design, my choice would be to throw an unchecked equation if the two polynomial terms had different exponents -- an IllegalArgumentException is what I would choose.
I would do that because I think the designer is expecting that the method is only going to be called when the two exponents are the same. Maybe your specification even says that? Another observation: a Polynomial that has int coefficients is extremely restricted in usefulness.. You can't even correctly determine the zeroes of such a polynomial unless you have the seroes method method to return a real value.
How would you represent 1. I just tried your 'insert' method, but it gave me a 'ConcurrentModificationException' And that is not strange, since you are modifying the termList while its iterator is active. Meanwhile the whole code is getting a bit chaotic now, with methods that can't be right and methods that throw exceptions. Are you sure that the 'plus' method really should return a PolynomialTerm and not a Polynomial?
Save Article. Like Article. Given two polynomials represented by two arrays, write a function that adds given two polynomials. Addition is simpler than multiplication of polynomials. We initialize result as one of the two polynomials, then we traverse the other polynomial and add all terms to the result. The following is implementation of above algorithm. Simple Python 3 program to add two. A utility function to return maximum. Can you do this by hand on paper? What I mean is, are you having trouble with the math, or the programming?
So now loop over the double array that you've read in and compute the coefficients of a derivative polynomial. This is not an answer since the question is not clear , but you might be interested in reading about en. Add a comment. Active Oldest Votes. Tharindu Kumara Tharindu Kumara 4, 2 2 gold badges 24 24 silver badges 44 44 bronze badges.
Sign up or log in Sign up using Google. Sign up using Facebook. It is bedtime here in Holland well, it is middle of the night , but I'm sure that when we go along the requirements, that all we need is a little fine tunung. And the recursion part should be a lot of fun!
One thing I noticed is that you were supposed to write a customized linked list to store the terms of a Polynomial. So if I were the person assigned to grade what you wrote, I wouldn't accept a java. LinkedList, which is what you used. To me that says you have to write your own list class, let's call it PolynomialTermList because it's supposed to be a list of polynomial terms.
That's a whole load of extra work, but on the other hand it frees you up to write customized methods which apply knowledge about how your polynomial term objects work. As for the add method: 1. The specs are silent about what should happen if the exponents aren't the same, but in a proper object-oriented design it's the responsibility of the PolynomialTerm class to deal with that possibility.
Making that a requirement of all code which calls the class's add method isn't a correct thing to do in OOD. As for the insert method: as I just mentioned, it looks like you need to write a customized linked list. That's a bit a lot, probably of a pain but it gives you an opportunity to get rid of your existing insert method and write a new one which works with your customized list.
Still, adding an entry to a sorted linked list isn't a trivial thing to do. I'll leave it further up to you and Paul, but I would like to mention this requirement: 3. That is a bit strange. If you are allowed only one field that points to the first term, does that mean the PolynomialTerm must contain a reference to a successor? Seems very strange, but maybe you can explain. Edit: forget what I wrote, I get it now. So my professor told me that we could create a separate linkedlist class but that is was not necessary and that that was not what he meant in the specifications for the assignment.
While trying it both ways i had some problems when i was designing the separate linked list class i realized that the java api linked list has an iterator that is used to traverse through the list, do i have to create an iterator method or is there another way?
When i tried to use the Polynomial class as a linkedlist class i ran into even more difficulty would i have to have the add methods for the linked list and for polynomials separate or is there a way i could implement them together.
I really need help finishing this assignment. NullPointerException at Polynomial. What problems do you encounter with the Polynomial class from my previous reply? There you have a PolynomialTerm as head and a Polynomial as tail. That is very much a genuine List.
Now think how you would construct such a Polynomial. You might start with an empty Polynomial a Polynomial for which isZero returns true with methods add or plus. Think how you would make sure that the head of a Polynomial is always the PolynomialTerm with the highest exponent. Don't forget to consider a Polynomial with coefficient 0 as special, since the exponent is irrelevant in that case.
Yes, but that was before your reply about that the Polynomial could be the List itself. Indeed I thought that a Polynomial should have something like a Node that you are using now, but doing it as I suggested seems easier. Given that, the problems simply come down to 1 how to construct a genuine Polynomial? Well, we need a Comparator for the PolynomialTerm class or it needs to implement Comparable, see the requirements.
Now, this new Polynomial must have a head and a tail. Is the head p? How to we decide that? And what will the tail become? This sounds like an opportunity for recursion! The problem to solve is how to add the PT p or q to P, so that p or q finds its place in the hierarchy. Given that p and q are the PT's with highest exponent?
That should not give any problems given we have 1 and 2 , but beware that either may be the zero Polynomial. Couk you show me what the t0string would look like? Hmm, then keep working with your Node class. Brings me to the question: how do you test your code? If I look at your method 'add Polynomial p ' I see some suspect things: your nested ' while' loop seems wrong, you use also ' term.
I canot say where your NPE is originating from. Again: have you tested this? My advice is to first concentrate on a method 'Polynomial add PolynomialTerm p. Having that method should make the add Polynomial p much easier.
0コメント