Results 1 to 14 of 14

Thread: RefList help!

  1. #1
    StealthRT is offline Member
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    0

    Question RefList help!

    Hey all i am trying to figure out how to get this RefList working for me...

    Implement a method countValue() that counts the number of times an item occurs in a linked list.

    int countValue(RefList f, int item);

    Generate 20 random numbers in the range of 0 to 4, and insert each number at the front of a linked list. Output the list by using a method which you would call writeLinkedList which you would add to the RefList.java program. In a loop, call the method countValue() , and display the number of occurrences of each value from 0 to 4 in the list.
    So far this is what i have:
    Java Code:
        public class PREX6
        {
               public static void main(String[] args)
               {
                  RefList refl1= new RefList();
                  int intRandomNum = 0;
                  int count = 0;
         
                  while (count != 21)
                  {
                        refl1.add(intRandomNum);
                  }
         
                  System.out.println(refl1);
              }
        }
    But there's an error for the "refl1.add(intRandomNum);" since the RefList class does not have a "add" property. How am i supposed to add to a list that has no command to add?

    Here is the RefList code:
    Java Code:
        public class RefList
        {
          protected int numElements;          // number of elements in this list
          protected LLObjectNode currentPos;  // current position for iteration
         
          // set by find method
          protected boolean found;         // true if element found, else false
          protected LLObjectNode location; // node containing element, if found
          protected LLObjectNode previous; // node preceeding location
         
          protected LLObjectNode list;     // first node on the list
         
          public RefList()
          {
            numElements = 0;
            list = null;
            currentPos = null;
          }
        protected void find(Object target)
          // Searches list for an occurence of an element e such that
          // e.equals(target). If successful, sets instance variables
          // found to true, location to node containing e, and previous
          // to the node that links to location. If not successful, sets 
          // found to false.
          {
            boolean moreToSearch;
            location = list;
            found = false;
         
            moreToSearch = (location != null);
            while (moreToSearch && !found) 
            {
              if (location.getInfo().equals(target))  // if they match
               found = true;
              else
              {
                previous = location;
                location = location.getLink();
                moreToSearch = (location != null);
              }
            }
          }
        public int size()
          // Returns the number of elements on this list. 
          {
            return numElements;
          }
         
          public boolean contains (Object element)
          // Returns true if this list contains an element e such that 
          // e.equals(element); otherwise, returns false.
          {
            find(element);
            return found;
          }
        public boolean remove (Object element)
          // Removes an element e from this list such that e.equals(element)
          // and returns true; if no such element exists, returns false.
          {
            find(element);
            if (found)
            {
              if (list == location)     
                list = list.getLink();    // remove first node
              else
                previous.setLink(location.getLink());  // remove node at location
         
              numElements--;
            }
            return found;
          }
         
          public Object get(Object element)
          // Returns an element e from this list such that e.equals(element);
          // if no such element exists, returns null.
          {
            find(element);    
            if (found)
              return location.getInfo();
            else
              return null;
          }
        public String toString()
          // Returns a nicely formatted string that represents this list.
          {
            LLObjectNode currNode = list;
            String listString = "List:\n";
            while (currNode != null)
            {
              listString = listString + "  " + currNode.getInfo() + "\n";
              currNode = currNode.getLink();
            }
            return listString;
          }  
         
          public void reset()
          // Initializes current position for an iteration through this list,
          // to the first element on this list.
          {
            currentPos  = list;
          }
        public Object getNext()
          // Preconditions: The list is not empty
          //                The list has been reset
          //                The list has not been modified since most recent reset
          //
          // Returns the element at the current position on this list.
          // If the current position is the last element, then it advances the value 
          // of the current position to the first element; otherwise, it advances
          // the value of the current position to the next element.
          {
            Object next = currentPos.getInfo();
            if (currentPos.getLink() == null)
              currentPos = list;
            else
              currentPos = currentPos.getLink();
            return next;
          }
        }
    Thanks!
    David

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems to me you need a method, add method, to add random elements to a LinkedList. Is that right?

    So what you have to do is define a method named add() and implement a LinkedList there. You have to think about the LinkedList object scope to use it within other methods.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Where did you get the code for the RefList class?
    How is the list variable given a value?

    Where is the LLObjectNode class defined? How is it related to your problem?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    You should explain your question much more clearly StealthRT. May be you don't know much about this because you found this code somewhere else.

  5. #5
    StealthRT is offline Member
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    0

    Default

    Ok, re-worked code:
    Java Code:
    public class LLObjectNode
    {
      private LLObjectNode link;
      private Object info;
    
      public LLObjectNode(Object info)
      {
        this.info = info;
        link = null;
      }
    
      public void setInfo(Object info)
      // Sets info Object of this LLObjectNode.
      {
        this.info = info;
      }
    
      public Object getInfo()
      // Returns info Object of this LLObjectNode.
      {
        return info;
      }
    
      public void setLink(LLObjectNode link)
      // Sets link of this LLObjectNode.
      {
        this.link = link;
      }
    
      public LLObjectNode getLink()
      // Returns link of this LLObjectNode.
      {
        return link;
      }
    }
    Java Code:
    public class RefUnsortedList extends RefList implements UnsortedListInterface
    {
    
      public RefUnsortedList()
      {
        super();
      }
    
      public void add(Object element)
      // Adds element to this list.
      {
        LLObjectNode newNode = new LLObjectNode(element);
        newNode.setLink(list);
        list = newNode;
        numElements++;
      }
    }
    Java Code:
    public interface UnsortedListInterface extends ListInterface
    {
      void add(Object element);
      // Adds element to this list.
    }
    Java Code:
        import java.util.Random;
        import java.lang.Integer;
         
        public class PREX6_2
        {
          public static void main(String[] args)
          {
            int temp;
            Integer item;
            Random random_numbers = new Random();
            RefUnsortedList myList = new RefUnsortedList();
         
            //Generate a List of 20 random integers from 1-4
            for (int i=0; i<20;i++)
            {
              temp = random_numbers.nextInt(5);
              item = new Integer(temp);
              myList.add(item);
            }
         
            //Now print the list
            myList.reset();
            
            //Now count and output values for 0-4
            for (int i=0;i<=4; i++)
            {
              temp = countValue(myList, i);
              System.out.println(i + ": " + temp);
            }
          }
         
          // Count the number of instalnces of value v in a LinkedList of Integers f
          public static int countValue(RefUnsortedList f, int v)
          {
            int theList = 0;
            int theValue = v;
            int theCount = 0;
         
            // Count from 0 to 20
            for (int a = 0; a < 20; a++)
            {
              // Save current node in theList
              theList = f.getNext().toString();
         
              // Compaire the node to the current passed static value
              if (theList == theValue)
              {
                // add to theCount if it equals
                theCount++;
              }
            }
            // Return theCounts' count back to function
            return (theCount);
          }
        }
    However i am still getting an error:
    Java Code:
        javac -Xlint:none  -d . -g -classpath . PREX6_2.java
         
        PREX6_2.java:43: incompatible types
         
        found   : java.lang.Object
         
        required: int
         
              theList = f.getNext();
         
                                 ^
         
        .\RefUnsortedList.java:11: RefUnsortedList is not abstract and does not override abstract method add(java.lang.Object) in ListInterface
         
        public class RefUnsortedList extends RefList implements UnsortedListInterface
         
               ^
         
        .\RefUnsortedList.java:19: add(java.lang.Object) in RefUnsortedList cannot implement add(java.lang.Object) in ListInterface; attempting to use incompatible return type
         
        found   : void
         
        required: boolean
         
          public void add(Object element)
         
                      ^
         
        .\UnsortedListInterface.java:11: add(java.lang.Object) in UnsortedListInterface clashes with add(java.lang.Object) in ListInterface; attempting to use incompatible return type
         
        found   : void
         
        required: boolean
         
          void add(Object element);
         
               ^
         
        4 errors
    I'm not getting why it has an error on the "theList = f.getNext();" when both are defined as integers... What am i doing wrong there?

    David
    Last edited by StealthRT; 10-27-2008 at 03:06 AM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    PREX6_2.java:43: incompatible types
    found : java.lang.Object
    required: int
    theList = f.getNext();
    What's returned by the getNext() method? The compiler thinks its an Object. Is theList an Object? The compiler thinks its an int. Where it the getNext() method defined? The only one I see is:
    public Object getNext()

    What do you expect to get back from getNext()? Is it a String that needs to be parsed/converted to an int? If so, then cast it to a String and use parseInt().

  7. #7
    StealthRT is offline Member
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    0

    Default

    Hey Norm the public Object getNext() is in the refList.java i first posted.
    Java Code:
    // Save current node in theList
          theList = Integer.parseInt(f.getNext());
    Is that what i needed to do to change it into an int? I get the error:
    Java Code:
    javac -Xlint:none  -d . -g -classpath . PREX6_2.java
    
    PREX6_2.java:43: cannot find symbol
    
    symbol  : method parseInt(java.lang.Object)
    
    location: class java.lang.Integer
    
          theList = Integer.parseInt(f.getNext());
    
                           ^
    David

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Java is very sensitive to types. You must know what type a method returns and what type a method's arguments must be.
    The getNext() method returns an Object.
    The parseInt() method requires a String.
    The compiler sees: parseInt(java.lang.Object) and can NOT find an override of the parseInt method that takes an Object as an arg. So it gives the error message.
    You must convert the type returned by getNext, an Object, to the String type. One way to do this is, if you are sure, is to cast the Object to String by coding (String) in front of the f.getNext() call.
    Later you'll see how to use Generics to test for correct types and do the casting for you.

  9. #9
    StealthRT is offline Member
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    0

    Default

    Awesome Norm! :) That seemed to work but they are integers instead of strings... But of course im still getting those last 3 errors that i do not know what to do with:
    Java Code:
    javac -Xlint:none  -d . -g -classpath . PREX6_2.java
    
    .\RefUnsortedList.java:11: RefUnsortedList is not abstract and does not override abstract method add(java.lang.Object) in ListInterface
    
    public class RefUnsortedList extends RefList implements UnsortedListInterface
    
           ^
    
    .\RefUnsortedList.java:19: add(java.lang.Object) in RefUnsortedList cannot implement add(java.lang.Object) in ListInterface; attempting to use incompatible return type
    
    found   : void
    
    required: boolean
    
      public void add(Object element)
    
                  ^
    
    .\UnsortedListInterface.java:11: add(java.lang.Object) in UnsortedListInterface clashes with add(java.lang.Object) in ListInterface; attempting to use incompatible return type
    
    found   : void
    
    required: boolean
    
      void add(Object element);
    
           ^
    
    3 errors
    And heres the code the errors refer too:
    Java Code:
    public class RefUnsortedList extends RefList implements UnsortedListInterface
    {
    
      public RefUnsortedList()
      {
        super();
      }
    
      public void add(Object element)
      // Adds element to this list.
      {
        LLObjectNode newNode = new LLObjectNode(element);
        newNode.setLink(list);
        list = newNode;
        numElements++;
      }
    }
    Java Code:
    public interface UnsortedListInterface extends ListInterface
    {
      void add(Object element);
      // Adds element to this list.
    }
    Java Code:
    public interface ListInterface
    {
      int size();
      // Returns the number of elements on this list.
    
      boolean contains (Object element);
      // Returns true if this list contains an element e such that 
      // e.equals(element); otherwise, returns false.
        
      boolean remove (Object element);
      // Removes an element e from this list such that e.equals(element)
      // and returns true; if no such element exists, returns false. 
      
      Object get(Object element);
      // Returns an element e from this list such that e.equals(element);
      // if no such element exists, returns null.
      
      String toString();
      // Returns a nicely formatted string that represents this list.
      
      void reset();
      // Initializes current position for an iteration through this list,
      // to the first element on this list.
    
      Object getNext();
      public boolean add(Object obj);
      // Preconditions: The list is not empty
      //                The list has been reset
      //                The list has not been modified since the most recent reset
      //
      // Returns the element at the current position on this list.
      // If the current position is the last element, then it advances the value 
      // of the current position to the first element; otherwise, it advances
      // the value of the current position to the next element.
    }
    David

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    You should now know how to solve the compiler error messages that say:
    found: ...
    required: ...
    See my previous discussion on getNext()

    does not override abstract method add(java.lang.Object) in ListInterface
    There are 2 different definitions for the add() method shown.
    public boolean add(Object obj);
    void add(Object element);
    How do you define it in your code? Why are the two definitions coded differently? One returns void the other boolean.

  11. #11
    StealthRT is offline Member
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    0

    Default

    Norm im not really sure. Thats the code i was given by my teacher to use. I agree though, it seems that the
    Java Code:
    public boolean add(Object obj);
    in the ListInterface.java contrasts with the UnsortedListInterface.java code
    Java Code:
    void add(Object element);
    And the RefUnsortedList.java code
    Java Code:
    public void add(Object element)
    Any ideas?

    David

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Make them all the same. Do you have specifications for what the method is to do? And what its args are to be?

  13. #13
    StealthRT is offline Member
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    0

    Default

    Well from the code the "add" only adds whatever is placed into the list and does not need anything returned from that. So i figure the return would be void... But thats just with me guessing and looking at what the code "should" be doing.

    I set them all to void and it compiled... it seems to be working now..

    David

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by StealthRT View Post
    I set them all to void and it compiled... it seems to be working now..
    As Norm says, you have to make them all as same. Read the compile error message carefully.

Posting Permissions

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