Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Comparing Generics; Quick Question

  1. #1
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Question Comparing Generics; Quick Question

    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
    Last edited by clydefrog; February 22nd, 2012 at 02:33 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Comparing Generics; Quick Question

    The error pretty much explains it all. You can't use arithmetic operators on Objects. You can use them on Integers or Doubles or Floats, only because of auto-unboxing, but you can't use them on generics that could be a different kind of Object.

    To further explain why, what would happen if I passed in JTable as the generic argument to your Node class? How do you compare two JTables for < or > ?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Comparing Generics; Quick Question

    oh okay, i see. So then how can i go about comparing 2 objects?

    I need to figure out which one is the largest.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Comparing Generics; Quick Question

    Well, you have to come up with rules to determine what "largest" means for different Objects. When is one JTable greater than another JTable?

    Are you sure Node is supposed to be a generic class? I don't see you using it in a generic way, and it's complicating your comparisons. Doesn't make a ton of sense.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    clydefrog (February 22nd, 2012)

  6. #5
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Comparing Generics; Quick Question

    no it doesnt have to be generic, i just though it would be easier. You suggest i change the node to just work with ints?

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Comparing Generics; Quick Question

    Quote Originally Posted by clydefrog View Post
    no it doesnt have to be generic, i just though it would be easier. You suggest i change the node to just work with ints?
    What happened when you tried? Are you supposed to be able to use decimals?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    clydefrog (February 22nd, 2012)

  9. #7
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Comparing Generics; Quick Question

    Quote Originally Posted by KevinWorkman View Post
    What happened when you tried? Are you supposed to be able to use decimals?
    you know i gave it some though, and realized that im only going to be using ints, so i might as well just ditch the generics. It works fine now.

    Thank you for your help regardless.

Similar Threads

  1. Question(s) on Generics/Templates
    By Kumarrrr in forum Java Theory & Questions
    Replies: 4
    Last Post: January 7th, 2012, 10:44 AM
  2. quick question, please HELP!
    By Nemus in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 29th, 2011, 01:23 PM
  3. Quick Question
    By sird00dguyman in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2011, 06:14 PM
  4. Another Quick Question
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 1st, 2011, 07:18 AM
  5. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM