• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Polynomial Java Program

 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the compareTo seems fine, I would just like to add that if the coefficients are 0 then the comparision is also 0. There is by the look of it nothing that enforces the exponent to be 0 when the coefficient is 0.

If you are using a decent IDE, then it will suggest to also override the equals and hashCode methods. It is strongly advised to override these methods indeed. For the sake of this assignment, two PT's are equal when either their coeffficients are 0 or when their exponents are equal. So, 7x^2 equals 3x^2.

I will now try to test yor code, to see what the add and the multiply exactly do.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not had time to do some testing, but I isolated the Node class, so that we can have an easier view of its contents. In the Polynomial class that I used as a LinkedList, I have given this role here to the Node class. I came up with this code:


As you see, I added a method Node.add(PolynomialTerm pt) so that it is straightforward to incorporate a new pt, neatly put into its correct place. It is heavily based on recursion, but you see how that simplifies things. It may look impressive, but given the recursive nature of a genuine List, it is standard work.
Now, it culminates in the method Node.add(Node node), that in fact does all the work of adding two LinkedPolynomials. With this Node class, adding two LinkedPolyniomials becomes very easy indeed.

Global Halth Warning: this all is completely untested, so it could be (and probably will be) bugridden.

Yet to add: Node multiply(Node node)

Tomorrow morning I will test your code, but I hope that you like this new Node class.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I jusr ran your latest code (correct me if I'm wrong):


The output I got was:

piet: 5 + 4x^2 + 3x+
syl: 9 + 7x^5 +
Exception in thread "main" java.lang.IllegalArgumentException
at sergemetellus.PolynomialTerm.plus(SergeMetellus.java:127)
at sergemetellus.LinkedPolynomial.plus(SergeMetellus.java:66)
at sergemetellus.SergeMetellus.main(SergeMetellus.java:23)

So you see there are some shortcomings.

First of all: your terms in the Nodes are not sorted. You see that when I print out piet and syl.
Next, in the plus method, you send pairs of terms from the two LinkedPolynomials to the plus method of PolynomialTerm. There is a big likelyhood of these terms having different exponents, and so this error is being thrown.

So, things to do are:

1) override the equals and hashCode methods in your PolynomialTerm class, to be on the safe side
2) try to get these PolynomialTerms into descending order. If you look at what I did in my Node class, you see that I add term for term, comparing as I go. I think that is the easiest way.
3) Do write this method 'add(PolynomialTerm pt) to your LinkedPolynomial class, or to the Node class. As you can see in my Node class example, getting that method to work enormously eases all the other methods.

Let us know how you fare. This assignment is interesting, but certainly not easy.
 
Marshal
Posts: 79061
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that is supposed to represent an implementation of Comparable<PolynomialTerm>, I think no.
What would that method return if you compare 3x² with 4x²?
Read the documentation for Comparable in the first part of this post, and decide how you are going to define a total ordering for ComparableTerms. What are you going to do about comparing 4x² with 2x³?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:(...)I think no.
What would that method return if you compare 3x² with 4x²?
(...)


The compareTo method treats these two as equal. What do you think is wrong with that? Think about that we do not want two terms in a Polynomial that have the same exponent.
 
Campbell Ritchie
Marshal
Posts: 79061
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So 3x² is considered equal to 4x²? If that is the definition of total ordering, then the method is all right.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As it is used now, is that when two PolynomialTerms are comparing to 0 (I. e are to be considered as equal) then the coefficients can be added. It is used when adding two polynomials. The disadvantage is that it might give some problems when defining an equals method for polynomials. Then I would not want 3x^4 to be considered equal to 5x^4. But that is for later. It is like having a Card class, where you have that diamond 7 is unequal to spades 7, however,  when it comes to poker, they are equal ( in value). That is why I , in the beginning of this topic, preferred to have a dedicated Comparator instead of doing it in a Comparable. But the goal for now is getting this plus and multiply of the Polynomial working and the compareTo is suitable.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after speaking with my professor the plus method is supposed to throw an exception but a polynomial exception if the exponents are not equal. for the ordering i will try something like the one in piet's node class and will get back to you. i still do not see why add(Polynomialterm pt) is necessary when i basically have that method in the polynomial term class. like is said before myy code is working just out of order
 
Campbell Ritchie
Marshal
Posts: 79061
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is very easy to create your own Exception class. The hardest part is usually choosing the superclass. Then (99% of the time) you have to implement four constructors and that's it.
 
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What about 0^10 compared to 1^3 ??
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:What about 0^10 compared to 1^3 ??


Or for that matter, 0^10 and 0^100?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic is about polynomials, i.e. things of the form sigma (ai * x^î), for i = n, n-1, ---, 0.
That has nothing to do with 0^3, 1^4 and 0^100.

Edkit: that sounds a bit harsher than I intended.
The objects here are indeed these polynomials, i.e. algebraïc entities, like you use in the Galois-theory, to name an example. So say 3x^2 + 7 is a polynomial. Now, you can imagine a function f(x) = 3x^2 + 7, as we all are so familiar with, and determine the value of f(2) and f(0). But, as said, that is something different. This assignment unfortunately also talks about a 'value' function, but that makes the possibility for a confusion only bigger.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:This topic is about polynomials, i.e. things of the form sigma (ai * x^î), for i = n, n-1, ---, 0.
That has nothing to do with 0^3, 1^4 and 0^100.


I was under the impression that Carey was using that notation as a shortcut for 0x^10 and 1x^3. I was just following suit: I meant 0x^10 and 0x^100. If coefficient is 0, then it doesn't really matter what x or its exponent is. Seems like coefficient == 0 is a special case for compareTo().
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Edkit: that sounds a bit harsher than I intended.


No problem, Piet. I think most of us know you enough to realize you don't mean it to be (most of the time anyway )
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Seems like coefficient == 0 is a special case for compareTo().

Thank you. This was what I was trying to point out to the OP.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know im late  but here is my multiply/times method


my insert method


my multiply method only multiplies one term of one polynomial with all the terms of the other polynomial nut it does not repeat the process with the other terms. my insert method thwos the exception no matter what term i try to insert. i really need help on these last two methods. the assignment is due TONIGHT and i also have the programming final tomorrow morning
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the Exception message text. Including line number etc.. Point out which line in your posted listing is the one the Exception points to.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the exception is line 17
here is the text
Exception in thread "main" java.lang.IllegalArgumentException: Term already exist
at LinkedPolynomial.insert(LinkedPolynomial.java:72)
at PolynomialTester.main(PolynomialTester.java:34)
/home/Surge/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the most recent code for compareTo() ?
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Serge Metellus wrote:


(term:1)-->(term:2)-->(term:4)
and you want to insert term:3, then you advance your current node to (4) but keep track of the previous node, then previous node links to term:3 and term:3 links to term:4.

Not exactly what you have. Can you use Java's LinkedList class?
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no it has to be the custom linked list that i have so far. and what about the times method
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how would i keep track of the previous node if there i did not declare a previous node in the node class and i don't think i am supposed to
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't recommend strongly enough that as you design code that you implement it in stages and get each stage debugged before going ahead.
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is exactly what i have been doing because those are the only two methods i have left to do in this entire program.

Carey Brown wrote:


^ what is that code?
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Serge Metellus wrote:what is that code?


That is an example of how to insert into a linked list.

Sometimes if you take a piece of your project off to the side and get it to work and then reintegrate it back into your project.
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your case "int payload" would be replaced with "PolynomialTerm".
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay but insert has to be in the polynomial class not the Node class
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the top of this page I gave a working version of a Node class. I used recursion, since if you look at the requirements halfway page 1 you see that the professor prefers recursive solutions.

I also showed that it is very wise to put the insert and add method into the Node class. I made a special 'add(PolynomialTerm p)' to the Node class, such that adding two Nodes was just one line of code. It also means that adding two Polynomials is just a matter of this line of code: 'return new Polynomial(this.node.add(other.node)'.

In similar fashion, adding a method 'multiply(PT p)' is even simpler and was left to OP. The code would have been:


and multiplying two Nodes is, analogue to the add method, just one line of code.

On page 1 I pointed out that in OP's PolynomialTerm class, no special attention was paid to the case when the coefficient == 0. Therefore I added that special line to the compareTo method. That line is now gone again.

I've spent a couple of hours to this topic and I have nothing more to add.

@Carey
why a static root? Why a root at all?
 
Serge Metellus
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piet in the requirements it states that the add and insert have to be in the Polynomilal class which i explained earlier. Also, in the requirements it says that comparable is implement on the exponents ONLY meaning it does not matter what the coefficient is. You seem irritated but i tried to explain everything to you and  blatantly ignored me again and again. also when the coefficient is zero the entire term is zero so AGAIN it does not matter
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:@Carey
why a static root? Why a root at all?


This was an example only. Not needed. I edited the code later to pass root (or whatever you want to call it) to insert().
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone tell me how do I create a method that  reads many polynomials?
 
reply
    Bookmark Topic Watch Topic
  • New Topic