13 Replies - 823 Views - Last Post: 30 March 2012 - 08:48 AM Rate Topic: -----

#1 javaCodeWriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 12

SortedLinkList Java Question

Posted 29 March 2012 - 09:46 AM

Hey guys, I am currently trying to make a sorted link list in java and I am having a lot of trouble when I compile it and the errors I am getting I don't quite understand. Here is my coding I've done so far so if you could help me out I would greatly appreciate it. This is also the assignment so you know what I have to do.

Pseudocode for the ADT Sorted List Operations
+ createSortedList()
// Creates an empty sorted list.
+ sortedIsEmpty(): boolean { query}
// Determines whether a sorted list is empty.
+ sortedSize(): integer { query}
// Returns the number of items that are in a sorted list.
+ sortedAdd( in item: ListItemType)
// Inserts item into its proper sorted position in a sorted list. Throws an exception if the item cannot be placed on the list (list full).
+ sortedRemove( in item: ListItemType)
// Deletes item from a sorted list. Throws an exception if the item is not found.
+ sortedGet( in index: integer)
// Returns the item at position index of a sorted list, if 0 <= index < sortedSize(). The list is left unchanged by this operation. Throws an exception if the index is out of range.
+ locateIndex( in item: ListItemType): integer { query}
// Returns the position where item belongs or exists in a sorted list; item and the list are unchanged.

Some operations— sortedIsEmpty, sortedSize, and sortedGet, for example— are just like those for the ADT list. Insertion and deletion operations, however, are by value, not by position as they are for a list. For example, when you insert an item into a sorted list, you do not specify where in the list the item belongs. Instead, the insertion operation determines the correct position of the item by comparing its value with those of the existing items on the list. A new operation, locateIndex, determines from the value of an item its numerical position within the sorted list. Note that the specifications given in Chapter 4 do not say anything about duplicate entries in the sorted list.
Depending on your application, you might allow duplicates, or you might want to prevent duplicates from entering the list. For example, a sorted list of Social Security numbers probably should disallow dupli-cate entries. In this example, an attempt to insert a Social Security number that already exists in the sorted list would fail.

Write a nonrecursive, reference- based implementation of the ADT sorted list of objects as a Java class SortedListRefBased such that...

b. Duplicates are not allowed, and operations must prevent duplicates from entering the list.

Now here's the coding.

Please help me out!! Thanks

This post has been edited by macosxnerd101: 29 March 2012 - 10:19 AM
Reason for edit:: Removed code since posted later with code tags


Is This A Good Question/Topic? 0
  • +

Replies To: SortedLinkList Java Question

#2 javaCodeWriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 12

Re: SortedLinkList Java Question

Posted 29 March 2012 - 10:01 AM

public class SortedLinkList
 {
 SortedList mySL = new SortedList();
 private int numItems = 0;
 Node head = null;
 
public boolean sortedIsEmpty()
 {
 return numItems == 0;
 }
 
public int sortedSize()
 {
 return numItems;
 }
 
public void sortedAdd(int index, Object item) throws SortedListException
 {
 if (index >= 0; index < numItems + 1; numItems++)
 {
 if(index == 0)
 {
 Node newNode = new Node(item, head);
 head = newNode;
 }
 else
 {
 Node prev = find(index-1);
 Node newNode = new Node(item, prev.next);
 prev.next = newNode;
 }
 
else
 {
 throw new SortedListException("List out of bounds on add.");
 }
 }
 }
 
public void sortedRemove() throws SortedListException
 {
 
}
 
public void sortedGet() throws SortedListException
 {
 
}
 
public int locateIndex() 
{
 return 0; //just so this error doesnt pop up for now
 }
 
}
 


My bad, I forgot to put the code words around my coding.
Was This Post Helpful? 0
  • +
  • -

#3 javaCodeWriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 12

Re: SortedLinkList Java Question

Posted 29 March 2012 - 10:09 AM

Here's an updated version I just did. Still getting a lot of errors...

public class SortedLinkList
{
  SortedList mySL = new SortedList();
  private int numItems = 0;
  Node head = null;
  
  public boolean sortedIsEmpty()
  {
    return numItems == 0;
  }
  
  public int sortedSize()
  {
    return numItems;
  }

  public void sortedAdd(int x, Object newnode) throws SortedListException
  {
    if (head == null)
    {
      head = newnode;
    }
    else if(!head.item.lessThan(x))
    {
      newnode.next = head;
      head = newnode;
    }
     else
     {
       throw new SortedListException();
     }
    }
  
  public void sortedRemove() throws SortedListException
  {
    // Don't know what to put here????
  }
  
  public void sortedGet(int index) throws SortedListException
  {
    if(0 <= index < sortedSize())
    {
      //Don't know what to put here??
    }
    else
    {
      throw new SortedListException();
    }
  }
  
  public int locateIndex() 
  {
    return 0; //just so this error doesnt pop up for now
  }
}

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,146
  • Joined: 27-December 08

Re: SortedLinkList Java Question

Posted 29 March 2012 - 10:19 AM

What specific errors are you encountering? Post them exactly as they appear.
Was This Post Helpful? 0
  • +
  • -

#5 javaCodeWriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 12

Re: SortedLinkList Java Question

Posted 29 March 2012 - 10:30 AM

When I compile these are the exact errors I am getting..

5 errors found:
File: C:\Users\millerk40\Downloads\Untitled_Message\SortedLinkList.java  [line: 3]
Error: cannot find symbol
  symbol:   class SortedList
  location: class SortedLinkList
File: C:\Users\millerk40\Downloads\Untitled_Message\SortedLinkList.java  [line: 3]
Error: cannot find symbol
  symbol:   class SortedList
  location: class SortedLinkList
File: C:\Users\millerk40\Downloads\Untitled_Message\SortedLinkList.java  [line: 5]
Error: cannot find symbol
  symbol:   class Node
  location: class SortedLinkList
File: C:\Users\millerk40\Downloads\Untitled_Message\SortedLinkList.java  [line: 25]
Error: cannot find symbol
  symbol:   variable next
  location: variable newnode of type java.lang.Object
File: C:\Users\millerk40\Downloads\Untitled_Message\SortedLinkList.java  [line: 41]
Error: bad operand types for binary operator '<'
  first type:  boolean
  second type: int

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,146
  • Joined: 27-December 08

Re: SortedLinkList Java Question

Posted 29 March 2012 - 10:34 AM

For your last error, you cannot compare a boolean and int using the < operator. For your other errors, where are those classes?
Was This Post Helpful? 0
  • +
  • -

#7 javaCodeWriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 12

Re: SortedLinkList Java Question

Posted 29 March 2012 - 10:38 AM

How do I fix that boolean and int error?? And what do you mean what are those other classes??
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,146
  • Joined: 27-December 08

Re: SortedLinkList Java Question

Posted 29 March 2012 - 10:42 AM

Make sure to break up your conditions like this rather than writing more mathematical inequalities.
if(index >= 0 && index < sortedSize()){}



Quote

And what do you mean what are those other classes??

Where is SortedList? And where is Node? Where did you define them?
Was This Post Helpful? 0
  • +
  • -

#9 javaCodeWriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 12

Re: SortedLinkList Java Question

Posted 29 March 2012 - 10:56 AM

View Postmacosxnerd101, on 29 March 2012 - 10:42 AM, said:

Make sure to break up your conditions like this rather than writing more mathematical inequalities.
if(index >= 0 && index < sortedSize()){}



Quote

And what do you mean what are those other classes??

Where is SortedList? And where is Node? Where did you define them?


Thank you for that fix. And I don't believe I have those defined.... How do I define Node?? I just need to rename SortedList to my class name. I misspelled it
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,146
  • Joined: 27-December 08

Re: SortedLinkList Java Question

Posted 29 March 2012 - 11:03 AM

You might find my Linked List tutorial helpful here.
Was This Post Helpful? 1
  • +
  • -

#11 javaCodeWriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 12

Re: SortedLinkList Java Question

Posted 29 March 2012 - 11:09 AM

View Postmacosxnerd101, on 29 March 2012 - 11:03 AM, said:

You might find my Linked List tutorial helpful here.


I will have to look more into it when I get back home from school. I'm on my
iPhone doing this now so I'll keep posting of I need more help. Thank you again!!!
Was This Post Helpful? 0
  • +
  • -

#12 javaCodeWriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 12

Re: SortedLinkList Java Question

Posted 29 March 2012 - 02:08 PM

View Postmacosxnerd101, on 29 March 2012 - 11:03 AM, said:

You might find my Linked List tutorial helpful here.


I still am getting errors in my coding :( please help!!!! Here's the code:

public class SortedLinkList
{
  int elem;
  Node int next, previous;
  SortedList mySL = new SortedList();
  private int numItems = 0;
  private Node head = null;
   
  public boolean sortedIsEmpty()
  {
    return numItems == 0;
  }
   
  public int sortedSize()
  {
    return numItems;
  }

 
  public void sortedAdd(int x, int elem) throws SortedListException
  {
    if (x == sortedSize())
    {
      add(elem);
    }
    else if(x == 0)
    {
      Node int head = new Node<E>();
      head.elem = elem;
    }
     else
     {
       throw new SortedListException();
     }
    }
   
  public void sortedRemove() throws SortedListException
  {
    // Don't know what to put here????
  }

  public void sortedGet(int index) throws SortedListException
  {
    if(index >= 0 && index < sortedSize())
    {
      //Don't know what to put here??
    }
    else
    {
      throw new SortedListException();
    }
  }
  public int locateIndex()
  {
    return 0; //just so this error doesnt pop up for now
  }
}

Was This Post Helpful? 0
  • +
  • -

#13 javaCodeWriter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-March 12

Re: SortedLinkList Java Question

Posted 29 March 2012 - 02:56 PM

public class SortedLinkList
{
  int elem;
  Node next, previous;
  SortedLinkList mySL = new SortedLinkList();
  private int numItems = 0;
  private Node head;
   
  public boolean sortedIsEmpty()
  {
    return numItems == 0;
  }
   
  public int sortedSize()
  {
    return numItems;
  }

 
  public void sortedAdd(int x, int elem) throws SortedListException
  {
    if (x == sortedSize())
    {
      add(elem);
    }
    else if(x == 0)
    {
      Node int head = new Node<E>();
      head.elem = elem;
    }
     else
     {
       throw new SortedListException();
     }
    }
   
  public void sortedRemove() throws SortedListException
  {
    // Don't know what to put here????
  }

  public void sortedGet(int index) throws SortedListException
  {
    if(index >= 0 && index < sortedSize())
    {
      //Don't know what to put here??
    }
    else
    {
      throw new SortedListException();
    }
  }
  public int locateIndex()
  {
    return 0; //just so this error doesnt pop up for now
  }
  
    private class Node
   {
    Node next;
    Object data;
  
    public Node(Object _data)
    {
       next = null;
       data = _data;
    }
  
    public Node(Object _data, Node _next)
    {
       next = _next;
       data = _data;
    }
   }
}


More updated...
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,146
  • Joined: 27-December 08

Re: SortedLinkList Java Question

Posted 30 March 2012 - 08:48 AM

Please post the specific errors you are encountering exactly as they appear. Otherwise, we don't know what errors you are encountering.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1