Hi,
I'm making a linked list implementation of a polynomial. My node, is a generic type with 2 elements; Coefficients and Powers. However, now i need to compare 2 nodes to see which has the largest power, so that it is placed first (canonical polynomial form). Unfortunately, it seems to be having a problem comparing these generics, i'm a little bit lost to be honest but i feel like its a simple solution.
This is the part thats giving me trouble (its within the add method: if (temp.getElement2()> q.getElement2()){
The error states: The operator > is undefined for the argument type(s) java.lang.Object, java.lang.Objec
Here is my code:
class Polynomial { Node head; Polynomial() { head = new Node(null,null); } //add new nodes into the polynomial public void add(int coef, int power) { Node temp = new Node(coef, power); if (head == null) head = temp; else { Node p = null; Node q = head; //insert in front if exponent is higher if (temp.getElement2()> q.getElement2()){ temp.setNext(head); head = temp; } //insert at middle or end else { while (q != null && q.getElement2() > temp.getElement2()){ p = q; q = q.getNext(); } p.setNext(temp); temp.setNext(q); } } } //to string public String toString() { Node current = head.getNext(); String output = ""; while(current != null) { output += "(" +current.Element1.toString() + "," + current.Element2.toString() + ")"; current = current.getNext(); } return output; } //node class class Node<E> { E Element1, Element2; Node<E> next; public Node(E Element1,E Element2) { this.Element1 = Element1; this.Element2 = Element2; next = null; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public E getElement1() { return Element1; } public void setElement1(E element1) { Element1 = element1; } public E getElement2() { return Element2; } public void setElement2(E element2) { Element2 = element2; } } }
Please help, im nearly done with this project
thanks in advance