SH A SH A - 20 days ago 8
Java Question

java: sortedList class Comparable error

I have these two classes, one of them is the sortedLinkedList class that has the comparable with a generic type. When I implement them in the main:

SortedListInterface<Apartment> aptList=new SortedLinkedList<Apartment>();


it will show this error:


Test.java:12: error: type argument Apartment is not within bounds of
type-variable T SortedListInterface aptList=new
SortedLinkedList();
^ where T is a type-variable:
T extends Comparable declared in interface SortedListInterface


Apartment class:

import java.lang.Math;
/**A class that holds Apartment informations*/
public class Apartment{

private String id;
private int yearsLeft, numOfViewed=0;
private StackInterface<Applicant> viewed=new LinkedStack<Applicant>();

/** constructor
@para: String i*/
public Apartment(String i){id=i;}
//end constructor

/** set years in its occupancy*/
public void setYears(){
yearsLeft=1+(int)(Math.random()*((3-1)+1));}
//end setYears method

/** decrease the number of years by one*/
public void decreaseYears(){
if(yearsLeft!=0)
--yearsLeft;}
//end decreaseYears method

/** check if the Apartment is vacant
@return true if vacant or false if not*/
public boolean isVecant(){
if(yearsLeft==0)
return true;
return false;}
//end isVecant method

/** add an Applicant to the viewed stack
@para: Applicant a*/
public void addToView(Applicant a){
viewed.push(a);
numOfViewed++;}
//end addToView method

/** get the viewed stack
@return Array of Applicant or null if empty*/
public Applicant[] getViewed(){
if(numOfViewed==0)
return null;
else{
Applicant[] arr=new Applicant[numOfViewed];
StackInterface<Applicant> temp=new LinkedStack<Applicant>();
Applicant entry;
int index=0;
while(!viewed.isEmpty()){
entry=viewed.pop();
temp.push(entry);
arr[index++]=entry;}//end while
while(!temp.isEmpty())
viewed.push(temp.pop());
return arr;}//end else
}//end getViewed method

/** get the ID number for this apartment
@return the apartment's ID
*/
public String getId(){return id;}
//end getId method

}//end Apartment class


SortedLinkedList class:

/**
* A class that implements the ADT sorted list by using a chain of nodes.
* Duplicate entries are allowed.
*
* @author Frank M. Carrano
* @version 2.0
*/
public class SortedLinkedList<T extends Comparable<? super T>>
implements SortedListInterface<T>
{
private Node firstNode; // reference to first node of chain
private int length; // number of entries in sorted list

public SortedLinkedList()
{
firstNode = null;
length = 0;
} // end default constructor

public boolean add(T newEntry)
{
Node newNode = new Node(newEntry);
Node nodeBefore = getNodeBefore(newEntry);

if (isEmpty() || (nodeBefore == null)) // add at beginning
{
newNode.setNextNode(firstNode);
firstNode = newNode;
}
else // add after nodeBefore
{
Node nodeAfter = nodeBefore.getNextNode();
newNode.setNextNode(nodeAfter);
nodeBefore.setNextNode(newNode);
} // end if

length++;
return true;
} // end add

/** Task: Finds the node that is before the node that
* should or does contain a given entry.
* @param anEntry the object to be located
* @return either a reference to the node that is before the node
* that contains or should contain anEntry, or null if
* no prior node exists (that is, if anEntry belongs at
* the beginning of the list) */
private Node getNodeBefore(T anEntry)
{
Node currentNode = firstNode;
Node nodeBefore = null;

while ( (currentNode != null) &&
(anEntry.compareTo(currentNode.getData()) > 0) )
{
nodeBefore = currentNode;
currentNode = currentNode.getNextNode();
} // end while

return nodeBefore;
} // end getNodeBefore


public boolean remove(T anEntry)
{
boolean found = false;

if (length > 0)
{
Node nodeToRemove;
Node nodeBefore = getNodeBefore(anEntry);

if (nodeBefore == null)
nodeToRemove = firstNode;
else
nodeToRemove = nodeBefore.getNextNode();

if ((nodeToRemove != null) && anEntry.equals(nodeToRemove.getData()) )
{
found = true;

if (nodeBefore == null)
firstNode = nodeToRemove.getNextNode();
else
{
Node nodeAfter = nodeToRemove.getNextNode();
nodeBefore.setNextNode(nodeAfter);
} // end if

length--;
} // end if
} // end if

return found;
} // end remove

public int getPosition(T anEntry)
{
int position = 1;
Node currentNode = firstNode;

while ( (currentNode != null) && ( anEntry.compareTo(currentNode.getData()) > 0) )
{
currentNode = currentNode.getNextNode();
position++;
} // end while

if ( (currentNode == null) || anEntry.compareTo(currentNode.getData()) != 0)
position = -position;

return position;
} // end getPosition

// list operations
public T remove(int givenPosition)
{
T result = null; // return value

if ((givenPosition >= 1) && (givenPosition <= length))
{
assert !isEmpty();

if (givenPosition == 1) // case 1: remove first entry
{
result = firstNode.getData(); // save entry to be removed
firstNode = firstNode.getNextNode();
}
else // case 2: givenPosition > 1
{
Node nodeBefore = getNodeAt(givenPosition - 1);
Node nodeToRemove = nodeBefore.getNextNode();
Node nodeAfter = nodeToRemove.getNextNode();
nodeBefore.setNextNode(nodeAfter); // disconnect the node to be removed
result = nodeToRemove.getData(); // save entry to be removed
} // end if

length--;
} // end if

return result; // return removed entry, or
// null if operation fails
} // end remove

public final void clear()
{
firstNode = null;
length = 0;
} // end clear

public T getEntry(int givenPosition)
{
T result = null; // result to return

if ((givenPosition >= 1) && (givenPosition <= length))
{
assert !isEmpty();
result = getNodeAt(givenPosition).getData();
} // end if

return result;
} // end getEntry

// 16.22
public boolean contains(T anEntry)
{
Node currentNode = firstNode;

while ( (currentNode != null) &&
(anEntry.compareTo(currentNode.getData()) > 0) )
{
currentNode = currentNode.getNextNode();
} // end while

return (currentNode != null) &&
anEntry.equals(currentNode.getData());
} // end contains


public int getLength()
{
return length;
} // end getLength

public boolean isEmpty()
{
boolean result;

if (length == 0) // or getLength() == 0
{
assert firstNode == null;
result = true;
}
else
{
assert firstNode != null;
result = false;
} // end if

return result;
} // end isEmpty

public boolean isFull()
{
return false;
} // end isFull

public void display()
{
// iterative
Node currentNode = firstNode;
while (currentNode != null)
{
System.out.println(currentNode.getData());
currentNode = currentNode.getNextNode();
} // end while
} // end display

private Node getNodeAt(int givenPosition)
{
assert !isEmpty() && (1 <= givenPosition) && (givenPosition <= length);
Node currentNode = firstNode;

// traverse the list to locate the desired node
for (int counter = 1; counter < givenPosition; counter++)
currentNode = currentNode.getNextNode();

assert currentNode != null;
return currentNode;
} // end getNodeAt

private class Node
{
private T data; // entry in list
private Node next; // link to next node

private Node(T dataPortion)
{
data = dataPortion;
next = null;
} // end constructor

private Node(T dataPortion, Node nextNode)
{
data = dataPortion;
next = nextNode;
} // end constructor

private T getData()
{
return data;
} // end getData

private void setData(T newData)
{
data = newData;
} // end setData

private Node getNextNode()
{
return next;
} // end getNextNode

private void setNextNode(Node nextNode)
{
next = nextNode;
} // end setNextNode
} // end Node
} // end SortedLinkedList


The idea is I want to sort all Apartments according to their years (int yearsLeft), but I'm new to the Comparable class, so I don't know how to work with it probably.

my question is: How to fix the error? with explanation if possible.

Answer

To be able to use the Apartment with SortedLinkedList, it needs to implement the Comparable interface, for example:

  class Apartment implements Comparable<Apartment> {
    @Override
    public int compareTo(Apartment o) {
      return 0;
    }

    // ... the rest of your code
  }

How do you want to order apartments? You need to implement the compareTo method accordingly. If you want to order apartments by yearsLeft, you could write that as:

@Override
public int compareTo(Apartment o) {
  return Integer.compare(yearsLeft, o.yearsLeft);
}

To order by yearsLeft in reverse order, you could write that as:

@Override
public int compareTo(Apartment o) {
  return -Integer.compare(yearsLeft, o.yearsLeft);
}

You can read more about the Comparable interface in the JavaDoc.

Comments