Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Poynomial Java Program

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have an assignment to create a Polynomial Program here is my PolynomialTerm class'


Here is my Polynomial Class


when i run the code with a tester i created it gives this output for the add method of the polynomial class
9x^3 + 2x^4 + 15x^2
output of multiply method
40x^5 + 16x^7 + 56x^4
output of derivative method
15x^2 + 10x^4 + 14x

can anyone please help me figure out what i am doing wrong
 
Marshal
Posts: 25835
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that's an awful lot of code to write before starting to test things. I assume, since you posted all the code, that those test results are incorrect? If so, why do you think that? Unless I missed it, I don't see any code where you input test data and display the results.
 
Marshal
Posts: 70353
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Paul C.
I suggest you start by formatting all that code. The comments before the methods shou‍ld be documentation comments. All methods shou‍ld be separated by single empty lines. All code shou‍ld be consistently indented; we have some suggestions here. The long lines e.g. line 104 shou‍ld be made into two lines. All the commented‑out code shou‍ld be deleted. You need to do that because people need to read your code. And you are the person who most needs to read it.
Then all methods ten lines or longer shou‍ld be divided into several smaller methods.
The code looks as if you had been guessing, finding things which don't work, and trying something different. So you end up with what looks like randomly‑arranged lines with /* */ around. Don't. If you need to comment code out always use // at the start of the lines.
Use proper names for fields. Not coe/exp. There is no need for such abbreviations.
Use proper capitalisation: you have methods called Plus and multiply. One of those is wrong because of the case of its first letter.

Once you have got some sort of legibility back into that code, you can start thinking about its logic. Why did you design the multiply method in the second class rather than the first class? How are you going to record different terms in the expression? Why are you using Math#pow, which is intended for fractional arguments, when your numbers are all integers? Why have you got all those ifs 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.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i edited the code i am sorry for the original post because i was in rush when posting it
heres is the edited and formatted polynomialterm class


Polynomial class


My test class


here is the output when i run the code
PolynomialTerm{4x^5}
27
PolynomialTerm{20x^4}
9x^3 + 2x^4 + 15x^2
40x^5 + 16x^7 + 56x^4
15x^2 + 10x^4 + 14x
7050

as you can see the output is incorrect
the add method has an extra term in the output that does not exist in the polynomial i gave
the multiply method is multiplying wrong
the derivative method shows the same problem as the add method
the evaluate method evaluates the polynomial incorrectly

I have parallel iterators to represent two polynomials
math.pow was recommended to find the derivative, if you have a different way to do it i would love to know. the ifs in the toString check different types of terms in a polynomial  


 
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4069
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for a start, your Polynomial(int[] data) constructor is incorrect, that is given the way you use it. The constructor gives:

Yet you invoke it with:

With this use your constructor should be:



Then, your add method with its double iterator is pretty complex, and it is incorrect as well. Given your firt four lines of testcode, we get this:
       
gives as output PolynomialTerm{4x^5}

and that is not so strange, since your plus method returns the second argument.

So there is quite some work to do!

My first suggestion is to add a Comparator<PolynomialTerm> to the code. If you have a Polynomial, then sort its terms with this comparator (nice if the terms with highest exponent come first in the list, just like in real maths).

Then, if you want to add two polynomials, sort both lists and then construct a new polynomial, in a way that you would do the merge of two arrays in a mergeSort. So, if you have thee two polynomials,
p1 = (a0, e0), (a1, e1), ...
p2 = (b0, f0), (b1, f1), ...

create a new List, and do:

if (e0 > f0) list.add((a0, e0))
else if (e0 == f0) list.add (a0 + b0, e0)
else ...

et cetera

A recursive routine would be fine for this. 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.
 
Paul Clapham
Marshal
Posts: 25835
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Piet suggests, your PolynomialTerm's Plus method is incorrectly designed. That's because the sum of two polynomial terms (say 2x and x^2) is not necessarily a polynomial term itself (in the example, the sum is 2x + x^2 which is not a polynomial term). 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.e. "plus" rather than "Plus").

Your Derivative method (better name: "derivative") also has a minor problem, namely that the derivative of 42 is not 0 * x^-1.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is extremely inefficient for a Polynomial class, especially when computing the value of the polynomial.
First, there is really  no need for a PolynomialTerm class; an nth order polynomial can be implemented using an array of n+1 values.
Also, note that
can be evaluated as

which can easily be extended to n terms via recursion or looping, thus eliminating all of the redundant calls to Math.pow().
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)
 
Paul Clapham
Marshal
Posts: 25835
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Fred's design makes the "add" and "derivative" methods much, much easier to write. It probably makes the "multiply" method easier as well.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have made many of the changes that al of you have suggested

Fred Kleinschmidt wrote:First, there is really  no need for a PolynomialTerm class; an nth order polynomial can be implemented using an array of n+1 values.


The polynomialterm class is a requirement for the  assignment.



 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:

My first suggestion is to add a Comparator<PolynomialTerm> to the code. If you have a Polynomial, then sort its terms with this comparator (nice if the terms with highest exponent come first in the list, just like in real maths).

Then, if you want to add two polynomials, sort both lists and then construct a new polynomial, in a way that you would do the merge of two arrays in a mergeSort. So, if you have thee two polynomials,
p1 = (a0, e0), (a1, e1), ...
p2 = (b0, f0), (b1, f1), ...

create a new List, and do:

if (e0 > f0) list.add((a0, e0))
else if (e0 == f0) list.add (a0 + b0, e0)
else ...

et cetera



i have almost no experience with the comparable class i know that the Polynomial term has to implement the comparable class and that it will only be implement on the exponents, but how do i go about doing this?

[quote = Paul Clapham]your PolynomialTerm's Plus method is incorrectly designed. That's because the sum of two polynomial terms (say 2x and x^2) is not necessarily a polynomial term itself (in the example, the sum is 2x + x^2 which is not a polynomial term)
how do i go about fixing this method, i have tried a number of different ways but i can't figure out how to return two terms

i cannot seem to figure out what is wrong with my value/evaluate method
 
Piet Souris
Bartender
Posts: 4069
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

and with this testcode:

I get this output:
*********************************************
*********************************************

As you see, that is not correct.

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.

And about the use of a Comparator (or implementing Comparable): it is handy to get your List of Polynomials sorted according to the exponents. So, a Comparator could be like:



and, having a Polynomial with a list, you can simply say: list.sort(c);

A Polynomial list will then be of the form (say)  3x^4 + 5x^3 + 2x + 3

and when adding two Polynomials, involving merging the lists, you can simply compare the first terms of both lists, to see if the exponents are equal or not.

Well, enough for now, I better shut up.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:
So, since the sum of two PolynomialTerms is not always another Polynomial, lets remove that method there and put it in the Polynomial class.



And about the use of a Comparator (or implementing Comparable): it is handy to get your List of Polynomials sorted according to the exponents. So, a Comparator could be like:



and, having a Polynomial with a list, you can simply say: list.sort(c);

A Polynomial list will then be of the form (say)  3x^4 + 5x^3 + 2x + 3

and when adding two Polynomials, involving merging the lists, you can simply compare the first terms of both lists, to see if the exponents are equal or not.



I cannot remove the plus/add method from the PolynomialTerm class because it is a requirement for the assignment
when implementing the comparator do i create a comparator class or use the comparable class?
also there are two methods that i forgot were part of the assignment
the insert method


and the isZero method which is supposed to return true if the polynomial has no terms
false if not
 
Paul Clapham
Marshal
Posts: 25835
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Serge Metellus wrote:

Paul Clapham wrote:your PolynomialTerm's Plus method is incorrectly designed. That's because the sum of two polynomial terms (say 2x and x^2) is not necessarily a polynomial term itself (in the example, the sum is 2x + x^2 which is not a polynomial term)


how do i go about fixing this method, i have tried a number of different ways but i can't figure out how to return two terms



Well, you can't return two terms. 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?
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Piet Souris
Bartender
Posts: 4069
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Serge,

a question: did you also get the bodies of all these methods? 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?

Going back to the 'insert' method, that I repeat here:

there are a couple of things that look wrong. The first three lines of the while lopp are:

That looks like an infinite loop, but as said, in the next two lines the exception is thrown, so can't be sure here.

Then, I see this line:

That would work if the PolynomialTerm class had redefined the 'equals' method, but that has not been done yet.

It would be much safer and easier to work with an intermediate List<PolynomialTerm> that we shall name 'result' here. Create one at the start of the method. Then look at this example:

Polynomial piet = 2x^4 + 4x^2  + 3x + 5
insert PolynomialTerm 5x.

Now, in our first check, we see that 2x^4 > 5x, so lets put 2x^4 into result.

Next, we compare 4x^2 with our 5x. Again, this term is bigger, and so we add it to 'result' too.
Then we see that 5x is equal (for the sake of comparison) to 3x. And now we can call our famous PolynomialTerm 'plus', since the exponents are equal and so that method works. We add the result of 'plus' to 'result'.
And Then we only need to add all the remaining terms of our original termList to reult, that becomes then 2x^4 + 4x^2 + 8x + 5. And to get immutable Polynomials, we make that as our return value of the insert method.

Then when we want to add a Polynomial p1 to another Polynomial p2, all we have to do is:

Polynomial result = new Polynomial();
for (PolynomilTerm p: p1) result = result.insert(p);

And for the method 'multiply' we can do a similar thing.

Last question for now: why are you working with LinkedLists and unhandy iterators?
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in order ofr you to understand the program better here are the requirements

Specific Requirements
1. Write a customized exception class PolynomialException for use with polynomials.
2. Write a class PolynomialTerm to represent one term of a Polynomial, each with a non-negative integer exponent and a  non-zero integer coefficient:
Instance variables of type int for the exponent and coefficient, and accessors.
A constructor that enforces the constraints described above
Method value( .. ) to evaluate a PolynomialTerm for a given int value of x.
Method plus( .. ) to return the sum of two PolynomialTerms.
Method times( .. ) to return the product of two PolynomialTerms.
Method derivative() to return the derivative of a PolynomialTerm.
Implements Comparable based on exponents only.
3. Write a class Polynomial to represent a Polynomial:
A customized linked list to store PolynomialTerms in descending order. The sole instance variable stores a reference to the first term.
A parameter-less constructor that creates the 0-polynomial (no terms).
A constructor public Polynomial(int[] data).The data parameter is an array of alternating exponents and coefficients; each pair of consecutive ints defines one PolynomialTerm. E.g. [1, 5, 3, 2, 0, -2, 2, -1] for Example 2 above. The terms may be in any order, but always with exponent first then coefficient.
Helper insert(PolynomialTerm term) to insert a new PolynomialTerm; throw an exception if the exponent of the new term matches an existing one.
Method isZero() to return true iff a Polynomial is the zero-polynomial.
Method value( .. ) to evaluate a Polynomial for a given int value of x.
Method plus( .. ) to return the sum of a pair of Polynomials.
Method times( .. ) to return the product of a pair of Polynomials.
Method derivative() to return the derivative of a Polynomial.

Algorithm Notes
1. Adding PolynomialTerms: (e, c1) + (e, c2) = (e, c1 + c2). The exponents must be the same. If the coefficient sum c1 + c2 = 0, the sum of the terms is null.
2. Multiplying PolynomialTerms: (e1, c1) * (e2, c2) = (e1 + e2, c1 * c2).
3. Adding Polynomials: Combines like terms – add terms with the same exponent.
4. Multiplying Polynomials: Let P(x) = p1(x) + P’(x), p1(x) the 1st term, P’(x) the rest.
Then, P(x) * Q(x)  =  p1(x) * Q(x)  +  P’(x) * Q(x) .
5. Consider providing recursive implementations of the Polynomial methods.



 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Specific Requirements
1. Write a customized exception class PolynomialException for use with polynomials.
2. Write a class PolynomialTerm to represent one term of a Polynomial, each with a non-negative integer exponent and a  non-zero integer coefficient:
Instance variables of type int for the exponent and coefficient, and accessors.
A constructor that enforces the constraints described above
Method value( .. ) to evaluate a PolynomialTerm for a given int value of x.
Method plus( .. ) to return the sum of two PolynomialTerms.
Method times( .. ) to return the product of two PolynomialTerms.
Method derivative() to return the derivative of a PolynomialTerm.
Implements Comparable based on exponents only.
3. Write a class Polynomial to represent a Polynomial:
A customized linked list to store PolynomialTerms in descending order. The sole instance variable stores a reference to the first term.
A parameter-less constructor that creates the 0-polynomial (no terms).
A constructor public Polynomial(int[] data).The data parameter is an array of alternating exponents and coefficients; each pair of consecutive ints defines one PolynomialTerm. E.g. [1, 5, 3, 2, 0, -2, 2, -1] for Example 2 above. The terms may be in any order, but always with exponent first then coefficient.
Helper insert(PolynomialTerm term) to insert a new PolynomialTerm; throw an exception if the exponent of the new term matches an existing one.
Method isZero() to return true iff a Polynomial is the zero-polynomial.
Method value( .. ) to evaluate a Polynomial for a given int value of x.
Method plus( .. ) to return the sum of a pair of Polynomials.
Method times( .. ) to return the product of a pair of Polynomials.
Method derivative() to return the derivative of a Polynomial.

Algorithm Notes
1. Adding PolynomialTerms: (e, c1) + (e, c2) = (e, c1 + c2). The exponents must be the same. If the coefficient sum c1 + c2 = 0, the sum of the terms is null.
2. Multiplying PolynomialTerms: (e1, c1) * (e2, c2) = (e1 + e2, c1 * c2).
3. Adding Polynomials: Combines like terms – add terms with the same exponent.
4. Multiplying Polynomials: Let P(x) = p1(x) + P’(x), p1(x) the 1st term, P’(x) the rest.
Then, P(x) * Q(x)  =  p1(x) * Q(x)  +  P’(x) * Q(x) .
5. Consider providing recursive implementations of the Polynomial methods.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please ignore my failed attempt at formatting the requirements into code
 
Paul Clapham
Marshal
Posts: 25835
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see. So most of my comments are now moot because in several cases the specs required you to do something, and you didn't, which made me make incorrect assumptions about the specs.

So after all of the to-and-fro in this thread, what code do you have now, and what questions do you still have?
 
Piet Souris
Bartender
Posts: 4069
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Head up, Serge! The assignment is very clear, and covers indeed many of the things that we have been writing about. But look at what you have achieved so far: although not everything is working 100%, you have come a long way. 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!

Goodnight!
 
Paul Clapham
Marshal
Posts: 25835
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.util.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.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

So after all of the to-and-fro in this thread, what code do you have now, and what questions do you still have?



well i now realize that add method should only be called when the exponents are the same(i will clarify this with my professor tomorrow) but my multiply method is still wrong and i STILL cant figure out what i am doing wrong and i tried to do my insert method like piet suggested but it either loops infinitely or returns the polynomial without the term inserted. in order to do the the iszero method i would have to check the first term in the polynomial and if the coefficient is zero than the polynomial is zero.
 
Paul Clapham
Marshal
Posts: 25835
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for the add method:

1. Adding PolynomialTerms: (e, c1) + (e, c2) = (e, c1 + c2). The exponents must be the same. If the coefficient sum c1 + c2 = 0, the sum of the terms is null.



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.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ill get back to you after i have created the custom linked list but you did not say anything about the multiply method
 
Piet Souris
Bartender
Posts: 4069
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll leave it further up to you and Paul, but I would like to mention this requirement:

3. Write a class Polynomial to represent a Polynomial:
A customized linked list to store PolynomialTerms in descending order. The sole instance variable stores a reference to the first term.


That is a bit strange. At first I would have thought that this Polynomial was meant:



But that woud be two instance variables. 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.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. he ment that the Plynomial class IS the linkedlist class and we are supposed to implement as such but that seems more difficult to me than creating a separate one.

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
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what i have so far for the Polynomial Linkedlist class

when i run the code i get this error
Exception in thread "main" java.lang.NullPointerException
at Polynomial.toString(Polynomial.java:241)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at PolynomialTester.main(PolynomialTester.java:29)
/home/Surge/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
or something similar
 
Piet Souris
Bartender
Posts: 4069
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
For instance, suppose you have the mathematical Polynomial,  4x^3 + 2x +5

Then you would have the Polynomial with head = 4x^3, tail Polynomial with head 2x and tail the Polynomial with head 5 and tail null.

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.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well after you wrote that and that the requirements specified 1 single variable repesenting the first term so i can only have one variable.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you also said you understood what the requirement said regarding a single instance variable
 
Piet Souris
Bartender
Posts: 4069
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Serge Metellus wrote:well after you wrote that and that the requirements specified 1 single variable repesenting the first term so i can only have one variable.


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). Suppose we have the Polynomial p + P, and we add the PT q, forming the new Polynomial p + q + P. Now, this new Polynomial must have a head and a tail. Is the head p? Or q? 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.

2) Suppose we have the Polynomials p + P and q + Q, what would be their sum? Given that p and q are the PT's with highest exponent?

3) and finally the product of two Polynomials p + P and q + Q. That should not give any problems given we have 1) and 2), but beware that either may be the zero Polynomial.

HTH

 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when the polynomial class is the list itself the single variable requirement still stands so i cannot have a a head and tail variable only a head . in the code i posted above the problems seems to be with term.next when i remove the .next i get an ouput even if its incorrect but i think that is because of a tostring method that i created. Couk you show me what the t0string would look like?
 
Piet Souris
Bartender
Posts: 4069
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.term.next...'  which seems wrong. 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. I recommend a recursive solution, for instance

 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i use the same test code as the one from before. i use term.term.next because the first term refers to the node firstterm in the polynomial then the second term is the polynomial term referenced from the node class. Whats is an NPE?
 
Piet Souris
Bartender
Posts: 4069
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed that Node had 'term' as member... but if you look at your code, compared to mine, then admit that this whole Node thing is just wasted energy. So I will continue with my definition of Polynomial, while you make the translation to Nodes.

An NPE is a NullPointerException. Have you tested your code so far? If so, did you spot where this NPE originates?

Now, first thing first: make sure that this PolynomialTerm implements Comparable<PolynomialTerm>. For this, you must r\make a newdefinition of 'equals' and 'hashCode'.
Now, two PT's are equal for the sake of this assignment when either both coefficients are 0 or both exponents are equal. If you define that like this, and you have an IDE, then it will also ask to generate a suitable 'hashCode'  for you. Accet what it has to offer.
Next, have a look at your deravative method, and chek for the coefficient being 0 or the exponent being 0. The add methode is indeed only menat for two PT's when their eponents are eauql, so throw an PolynomialError (yet to be defined) if that is not the case.

Then, I advise to incorporate some additional constructors for your Polynomial class. For instance Polynomial(PT p, Polynomial P) (You must translate that into Nodes).
And I lready wrote about the methosd add(PT p). See how much more easy that is when using recursion? I just noticed that wheere I wrote new Polynomial(p, tail.add(head)) that tail.add(head) has THIS shortcut: ???

Having this method add(PT p), then add(Polynomial p) is just one line of code.

All in all, using Polynomial as I suggested simplifies things quite a lot, and I don't think your professor will object (or else send him to me for some good conversation ;)

And test the code. Use System.out.println's, use singlesteppers, use simple short Polynomials for testing, and make sure that your 'toString' for the PT class works as it should.

 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my current code for the Polynomial(now LinkedPolynomial class)

it works but the terms are printed in opposite order. for instance if i just print out the polynomial with no operations done to it will print in the opposite order that it was inputed but after adding it to another polynomial it will print in the same order when it was inputed. what am i doing wrong?

Also, as i have said multiple times already i CANNOT, i repeat, CANNOT have another instance variable. my professor will not except it he is very strict about the way the assignment should done. For example in another assignment i had help from a tutor and the way the code was written was correct but that was not how he wanted it so i got a 76.
 
Piet Souris
Bartender
Posts: 4069
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I must say: your code looks okay at first sight, but to be honest: if I see this code:

then I do not get a good feeling about it, whether that code works or not. And then I am very curious about this 'plus' method in your PT class. Has that changed since the last time you showed the code?

Well, you made it clear we must use this Node class, unfortunately.

Now, first question: can you supply your current PT class? That will allow me (or other people) to run your code and see what works and what not.

Other remark for now: the constructor that uses an int[] as parameter: be aware that that int[] can be unordered, while the specs say that your Nodes should go from high exponent to low. So, that either means that you must be able to sort your Nodes into the correct order, or that you implement what I've been advising: start with a method add(PolynomialTerm p). That allows you with ease to get your Nodes into the correct order. Did you implement the method 'boolean compareTo(Object other)' in your PT class, so that it implements the Comparable<PT> interface?
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the only changes in my PT class were the adding of the exception(i haven't designed the polynomial exception yet) and the compareto method because i am not exactly sure how to implement it

 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would this be correct for the compareTo method
 
I am mighty! And this is a mighty small ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic