Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    next() cannot override next() in my abstract list.

    I get an error when calling next() during my return SalesPerson method.
    Here is the SortedListOfSalesperson:

    Code:
    
    
    public class SortedListOfSalesperson extends AbstractList
    {
      public SortedListOfSalesperson() { super(); }
      public SortedListOfSalesperson(int maxItems) { super(maxItems); }
    
      public boolean contains(Comparable otherItem)
      // Returns true if item is in the list; false otherwise
      {
        SalesPerson item = (SalesPerson) otherItem;
        int index = 0;
        while (index < numItems && ((SalesPerson)listItems[index]).compareTo(item) > 0)
          index++;
        return (index < numItems && ((SalesPerson)listItems[index]).compareTo(item) == 0);
      }
      
      public SalesPerson next() { return (SalesPerson)super.next(); }
      
      public void add(Comparable item)
      // If the list is not full, puts item in its proper place in
      //  the list; otherwise list is unchanged
      // Assumption:  item is not already in the list
      {
        if (!isFull())
        {
          int index = numItems - 1;          // Loop control variable
          while (index >= 0 && 
                (((SalesPerson)item).compareTo((SalesPerson)listItems[index]) < 0))
          { // Find addition point
            listItems[index+1] = listItems[index];
            index--;
          }
          listItems[index+1] = item;        // Add item
          numItems++;                       // Increment number of items
        }
      }
    
      public void remove(Comparable otherItem)
      // Removes item from the list if it is there. Implements "remove if there" 
      // semantics. Maintains ordering of elements
      {
        int index = 0;
        boolean found = false;
        SalesPerson item = (SalesPerson) otherItem;
        while (index < numItems && !found)
        {
          if (((SalesPerson)listItems[index]).compareTo(item) == 0)
            found = true;
          else
            index++;
        }
        if (found)
        {
          for (int count = index; count < numItems-1; count++)
            listItems[count] = listItems[count+1];
          numItems--;
        }
      }
    }
    And my abstract list class:

    Code:
    
    //****************************************************************
    //Class AbstractList provides common methods to create and 
    //maintain a list of Comparable objects. Subclasses must 
    //implement its transformers with appropriate types and semantics
    //****************************************************************
    public abstract class AbstractList
    {
    	protected Comparable[] listItems;     // Array to hold list items
    	protected int numItems;               // Number of items in the list
    	protected int currentPos;             // State variable for iteration
    
    	public AbstractList()
    	// Instantiates an empty list object with room for 100 items
    	{
     		numItems = 0;
     		listItems = new Comparable[100];
     		currentPos = 0;
    	}
    
    	public AbstractList(int maxItems)
    	// Instantiates an empty list object with room for maxItems items
    	{
     		numItems = 0;
     		listItems = new Comparable[maxItems];
     		currentPos = 0;
    	}
    
    	// Returns true if there is no room for another component; false otherwise
    	public boolean isFull() { return (listItems.length == numItems); }
    
    	// Returns true if there are no components in the list; false otherwise
    	public boolean isEmpty() { return (numItems == 0); }
    
    	// Returns the number of components in the list
    	public int size() { return numItems; } 
    
    	// Returns true if item is in the list; false otherwise
    	public abstract boolean contains(Comparable otherItem);
    
    	// Abstract transformers to be implement by subclasses
    	public abstract void add(Comparable item);
    	public abstract void remove(Comparable otherItem);
    
    	// Prepare list for iteration
    	public void resetList() { currentPos = 0; }
    
    	// Returns true if there is another item in the list; false otherwise
    	public boolean hasNext() { return (currentPos < numItems); }
    
    	public Comparable next()
    	// Returns the item at the currentPos position; resets current
    	//  position to first item after the last item is returned
    	// Assumption:  no transformers have been invoked since last call
    	{
     		Comparable next = listItems[currentPos];
     		currentPos++;
     		return next;
    	}
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Can't replicate the problem. What is the error?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •