public class QueueImpl<E extends Comparable<E>> 

{
    private int count;


    private class Node
    {
        private E info;
        Node next;
    }
    Node front;
    Node rear;
public E peekMaximum()
    {
         Node temp = new Node();
         temp = front;
         Node max = new Node();
         max = temp;
         while(temp!=null)
         {
            System.out.println(temp.info);
            //if(temp.info>max)
                if((temp).compareTo(max))>0)                   // getting error here
            //if(((Integer)(temp.info) - (Integer)(max.info)) >0)  
            {
                max = temp;
            }


            temp=temp.next;
         }

        return max.info;

    }
}

i have implemented my own queue . now i want to find object having maximum value.

how should i compare the two objects?

Edited 4 Years Ago by shanki himanshu

// getting error here

Please post the full text of the error message.
Also post the code you use for testing.

import java.util.*;
import java.io.*;
import java.lang.*;

public class QueueImpl<E extends Comparable<E>> 

{
    private int count;


    private class Node
    {
        private E info;
        Node next;
    }
    Node front;
    Node rear;

    public QueueImpl()
    {
        count = 0;
    }




    public void enqueue(E e)
    {
        Node x = new Node();
        if(e==null)
        {
            throw new IllegalArgumentException();
        }
        else{
            if(count==0)
        {
            x.info = e;
            front = x;
            rear = x;
        }
        else
        {
            x.info = e;
            rear.next = x;
            x.next = null;
            rear = x;
        }
        count++;}
        //System.out.println(x.info);


    }

    public E peekMaximum()
    {
         Node temp = new Node();
         temp = front;
         Node max = new Node();
         //E max = temp;
         max = temp;
         while(temp!=null)
         {
            //System.out.println(temp.info);
            //if(temp.info>max)
        if(((temp).compareTo(max))>0)
            //if(((Integer)(temp.info) - (Integer)(max.info)) >0)
            {
                max = temp;
            }


            //temp=(Node)temp.next;
            temp= temp.next;
         }
        //Iterator i = q.Iterator();
        return max.info;
         //return null;

    }


    public static void main(String[] args)
    {
        QueueImpl<Integer> q = new QueueImpl<Integer>();

        //ArrayList<Integer> a = new ArrayList();
        q.enqueue(2);
        q.enqueue(1);
        q.enqueue(3);
        q.enqueue(4);
        System.out.println(q.peekMaximum());
        //q.peekMaximum();


    }

}

Edited 4 Years Ago by shanki himanshu

QueueImpl.java:65: cannot find symbol
symbol  : method compareTo(QueueImpl<E>.Node)
location: class QueueImpl<E>.Node
                if(((temp).compareTo(max))>0)
                          ^

Edited 4 Years Ago by shanki himanshu

and there's your problem. you call a method that doesn't exist for that object. have your Node class implement the Comparable interface and override the necessary method (compareTo) and write the implementation you want for it.

i have to define compareTo method itself?
isnt it inbuilt function?

Node is a class that you have created, so there's no way Java can know how you want to compare or sort its instances. Eg for a Person class you may want to compare/sort by family name/given name, for a HousesForSale class it may be on price, etc etc.
That's why you have to define your own method when you create your own class.
(The standard API classes like String do come with their own compareTo methods, of course)

how to check for greater or smaller?
how can i compare the values of temp.info and max.info until null is reached?
i.e. checking for if(temp.info>max.info).

Edited 4 Years Ago by shanki himanshu

The info field in your Node class is defined as type E, and the class definition for QueueImpl restricts E toE extends Comparable<E> You can confirm that yourself by trying to create a QueueImpl for a type that does not support Comparable (eg new QueueImpl<Thread> - the compiler will reject it.
That means that whatever type info is, it will always support the compareTo method
So you can use temp.info.compareTo(max.info)

there is no "thé definition", if you mean how the code should be. each class has it's own implementation, based on how you want to sort your instances.

The Comparable interface API doc defines the compareTo method in terms of its parameter, return value, and necessary logical function (in great detail!). Yes, every class has its own implementation, but I would certainly consider the Comparable interface doc as "defining" the method.

Edited 4 Years Ago by JamesCherrill

i mean what should i write in compareTo function?
if i write this temp.info.compareTo(max.info) then i need to define compareTo function. then still i need to have a check in that function. what is the if condition ?

Like I said: The way your classes are defined the info variables will always be of a class that has an implementation of compareTo. If your info variables are from the standard Java API (eg Integer, like the code you posted earler) then you do not need to write a compareTo method.
You code your call to the method just like I showed. That method returns a value that you can test in an if test. The API doc tells you all about that return value.

public class QueueImpl<E extends Comparable<E>> 
    ...
    private E info;

So info is type E where E is defined as <E extends Comparable<E>> so, by definition, it does have a compareTo method.

@shanki himanshu "it's not working" is not enough info.

This question has already been answered. Start a new discussion instead.