compareTo question
I'm trying to use a Comparable interface to compare two integers...
on the public int compareTo(Object o)...the object being passed in is an int...
now when I try to compare it to another int....it doesn't allow it....says it can't find a compareTo function. I'm writing this in a ListNode function for a linked list of ints....basically I'm just not sure how to make the compareTo function work... can anyone help?
List Node
publicclass ListNode{
private Object info;
private ListNode next;
/**
* Constructs an empty node
*/
public ListNode(){
info =null;
next =null;
}
/**
* Constructs a node with an element in the info field
* @param o - element to be assigned to the info field
*/
public ListNode (Object o){
info = o;
next =null;
}
/**
* Constructs a node with an element in an info field
* and a reference to a node in the next field
* @param o - element to be assigned to the info field
* @param p - reference to a node to be assigned to the next field
*/
public ListNode (Object o, ListNode p){
info = o;
next = p;
}
/**
* Assigns an element to the info field of the node
* @param o - element to be assigned to the info field
*/
publicvoid setInfo(Object o){
info = o;
}
/**
* Returns the element in the info field of the node
*/
public Object getInfo(){
return info;
}
/**
* Sets Node p to the next field of the node
* @param p - reference to a node to be assigned to the next field
*/
publicvoid setNext(ListNode p){
next = p;
}
/**
* Returns the next field of the node
*/
public ListNode getNext(){
return next;
}
publicint compareTo(Object o){
}
}
ObjecList
// ObjectList.java
publicclass ObjectList{
private ListNode list;
/**
* Constructs an empty list
*/
public ObjectList(){
list =null;
}
/**
* Returns the first element in the list
*/
public Object getFirst(){
if (list ==null)
returnnull;
else
return list.getInfo();
}
/**
* Returns the last element in the list
*/
public Object getLast(){
if (list ==null)
returnnull;
ListNode p = list;
while (p.getNext() !=null)
p = p.getNext();
return p.getInfo();
}
/**
* Returns the first node in the list
*/
public ListNode getFirstNode(){
return list;
}
/**
* Adds the given element to the beginning of the list
* @param o - the element to be inserted at the beginning of the list
*/
publicvoid addFirst(Object o){
ListNode p =new ListNode(o, list);
list = p;
}
/** Appends the given element to the end of the list
* @param o - the element to be appended to the end of the list
*/
publicvoid addLast(Object o){
ListNode p =new ListNode(o);
if (list ==null)
list = p;
else{
ListNode q = list;
while (q.getNext() !=null)
q = q.getNext();
q.setNext(p);
}
}
/**
* Removes and returns the first element from the list
*/
public Object removeFirst(){
if (list ==null){
System.out.println("removeFirst Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode p = list;
list = p.getNext();
return p.getInfo();
}
/**
* Removes and returns the last element from the list
*/
public Object removeLast(){
if (list ==null){
System.out.println("removeLast Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode p = list;
ListNode q =null;
while (p.getNext() !=null){
q = p;
p = p.getNext();
}
if (q ==null)
list =null;
else
q.setNext(null);
return p.getInfo();
}
/**
* Inserts a node after the node referenced by p
* @param p - reference to node after which the new node will be added
* @param q - reference to node that will be inserted into the list
*/
publicvoid insertAfter(ListNode p, Object o){
if (p ==null){
System.out.println("insertAfter Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode q =new ListNode(o, p.getNext());
p.setNext(q);
}
/**
* Deletes the node after the node referenced by p
* @param p - reference to node after which the node will be deleted
*/
public Object deleteAfter(ListNode p){
if (p ==null || p.getNext() ==null){
System.out.println("deleteAfter Runtime Error: Illegal Operation");
System.exit(1);
}
ListNode q = p.getNext();
p.setNext(q.getNext());
return q.getInfo();
}
/**
* Inserts a node into its correct location within an ordered list
* @param o - The element to be inserted into the list
*/
publicvoid insert(Object o){
ListNode p = list;
ListNode q =null;
while (p !=null && ((Comparable)o).compareTo(p.getInfo()) > 0){
q = p;
p = p.getNext();
}
if (q ==null)
addFirst(o);
else
insertAfter(q, o);
}
/**
* Removes and returns the first occurrence of the specified
* element in the list
* @param o - The object to be removed from the list
*/
public Object remove(Object o){
ListNode p = list;
ListNode q =null;
while (p !=null && ((Comparable)o).compareTo(p.getInfo()) != 0){
q = p;
p = p.getNext();
}
if (p ==null)
returnnull;
elsereturn q ==null ? removeFirst() : deleteAfter(q);
}
/**
* Returns true if the list contains the specified element.
* @param o - The object to search for in the list
*/
publicboolean contains(Object o){
ListNode p = list;
while (p !=null && ((Comparable)o).compareTo(p.getInfo()) != 0)
p = p.getNext();
return p !=null;
}
/**
* Returns true if this list contains no elements
*/
publicboolean isEmpty(){
return list ==null;
}
/**
* Removes all elements from the list
*/
publicvoid clear(){
list =null;
}
/**
* Returns the number of elements in the list
*/
publicint size(){
int count = 0;
ListNode p = list;
while (p !=null){
++count;
p = p.getNext();
}
return count;
}
}
The comparable interface is just public int compareTo(Object o) ...like it should be...

