SortedStringListReferenceBased For this one I know most of the code, I just need
ID: 3564258 • Letter: S
Question
SortedStringListReferenceBased
For this one I know most of the code, I just need help correcting the add, remove, get, and locate commands in my file SortedStringListReferenceBased.java as I have work and other classes that I have tests for this week. Most of them are already correct. I have already created the other files or were given them by the professor. The addition is supposed to use insertion and remove uses deletion to keep the list sorted as you add or remove items.
My current output displays
0. milk
1.milk
2.pecans
3.rice
numItems is now: 4
adding juice ...
exception: 5 is an invalid index
I know the problem is in the add method, I am just not sure how to fix it, and if there are any other issues I have that need fixing.
Anyway here is the code, note their are 6 java files, however changes can only be made in SortedStringListReferenceBased.java as I have already created or were given the other files and our professor only wants SortedStringListReferenceBased.java to be submitted as he has the other files we were supposed to create or use from a previous assignment.
SortedStringListReferenceBased.java
// ********************************************************
// Reference-based implementation of the ADT Sorted List.
// *********************************************************
/**
* class SortedListReferenceBased
*
* A class that implements the SortedListInterface using a linked list
*
*/
public class SortedStringListReferenceBased implements SortedStringListInterface
{
private Node head;
private int numItems; // number of items in list
public SortedStringListReferenceBased()
// creates an empty list
{
head = new Node();
numItems = 0;
} // end default constructor
public boolean isEmpty()
// Determines whether a list is empty
{
return (numItems == 0);
} // end isEmpty
public int size()
// Returns the number of items that are in a list
{
return numItems;
} // end size
public void removeAll()
// Removes all the items in the list
{
head = null;
numItems = 0;
} // end removeAll
public void add(String item) throws ListException
// Inserts item into its proper position in a sorted list
// Throws an exception if the item connot be placed on the list
{
if (index >= 0 && index <= numItems)
return;
try
{ if(numItems == 0){
Node newNode = new Node(item, head);
head = newNode;
}else{
Node prev = find(index - 1);
Node newNode = new Node(item,prev.getNext());
prev.setNext(newNode);
}
numItems++;
// YOUR CODE WILL BE HERE...
// DEFINE ANY HELPER METHOD(S) AND CALL IT/THEM, IF YOU NEED ANY.
}
catch(Exception e)
{
throw new ListException("Add to List failed: " + e.toString());
}
}
public String get(int index) throws ListIndexOutOfBoundsException
// Retrieves the item at position index of a sorted list, if 0 <= index < size().
// The list is left unchanged by this operation.
// Throws an exception when index is out of range.
{
if (index >= 0 && index < numItems){
Node curr = find(index);
String dataItem = curr.getItem( );
return dataItem;
}else{
throw new ListIndexOutOfBoundsException(index + " is an invalid index");
}
}
public void remove(String item) throws ListException
// Removes the item from a sorted list.
// Throws an exception if the item is not found.
{
if (index >= 0 && index < numItems)
return;
try{
if (index == 0) // special case: delete the first node
{
head = head.getNext();
}
else{
Node prev = find(index - 1);
Node curr = prev.getNext();
prev.setNext(curr.getNext());
}
numItems--;
// YOUR CODE WILL BE HERE...
// REQUIREMENT: USE "locateIndex(String item)" method.
}
catch(Exception e)
{
throw new ListException("Remove " + item.toString() + " from List failed: " + e.toString());
}
}
public int locateIndex(String item)
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
{
Node curr = head;
for (int i = 0; i < size(); i++){
if (item.compareTo(curr.getItem())<= 0){
return i;
}//end if
else {
curr = curr.getNext();
}//end else
}//end for
return size()+1;
//TODO
} //end locateIndex()
private Node find(int index) {
// --------------------------------------------------
// Locates a specified node in a linked list.
// Precondition: index is the number of the desired
// node. Assumes that 1 <= index <= numItems+1
// Postcondition: Returns a reference to the desired
// node.
// --------------------------------------------------
Node curr = head;
for (int skip = 1; skip < index; skip++) {
curr = curr.getNext();
} // end for
return curr;
} // end find
} // end SortedListReferenceBased
Node.java
// *********************************************************************
// ---------Node Object--------
public class Node
// Assume the Node Object is String type
{
private String item;
private Node next;
public Node()
{//create a node without item or reference
item = null;
next = null;
}// end constructor
public Node(String newItem)
// create a node without reference
{
item = newItem;
next = null;
} // end constructor
public Node(String newItem, Node nextNode)
// create a node with reference to another node
{
item = newItem;
next = nextNode;
} // end constructor
public void setItem(String newItem) // store item in the node
{
item = newItem;
} // end setItem
public String getItem() // return item in the node
{
return item;
} // end getItem
public void setNext(Node nextNode) // reference to another node
{
next = nextNode;
} // end setNext
public Node getNext() // return the reference of the node
{
return next;
} // end getNext
} // end class Node
// =================================================================
SortedStringListInterface.java
// ********************************************************
// Interface SortedListInterface for the ADT SortedList.
// *********************************************************
public interface SortedStringListInterface
{
public boolean isEmpty();
// Determines whether a sorted list is empty
public int size();
// Returns the number of items that are in a sorted list
public void add(String item) throws ListException;
// Inserts item into it's proper position in a sorted list
// Throws an exception if the item connot be placed on the list
public String get(int index) throws ListIndexOutOfBoundsException;
// Retrieves the item at position index of a sorted list, if 0 <= index < size().
// The list is left unchanged by this operation.
// Throws an exception when index is out of range.
public void remove(String item) throws ListException;
// Removes the item from a sorted list.
// Throws an exception if the item is not found.
public int locateIndex(String item);
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
public void removeAll();
// Removes all the items in the list
} // end SortedStringListInterface
SortedStringListDriver.java
/**
* class SortedListDriver
*
* A driver class to execute methods from SortedStringListReferenceBased.java,
* which creates and a reference-based list and performs methods
* such as adding, deleting, finding a value in a list,
* and returning the index number.
*
*/
public class SortedStringListDriver
{
public static void main(String [] args)
{
SortedStringListReferenceBased myGroceryList = new SortedStringListReferenceBased();
myGroceryList.add("milk");
myGroceryList.add("eggs");
myGroceryList.add("butter");
myGroceryList.add("pecans");
myGroceryList.add("apples");
myGroceryList.add("bread");
myGroceryList.add("chicken");
myGroceryList.add("black beans");
myGroceryList.add("rice");
myGroceryList.add("sausage");
myGroceryList.add("flour");
printList(myGroceryList); //print out original List
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("adding juice ...");
myGroceryList.add ("juice"); //add juice
System.out.println("item 5 is: " + myGroceryList.get(5)); //get the content at index 5
printList(myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("removing juice...");
myGroceryList.remove ("juice");
printList(myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("removing apples...");
myGroceryList.remove ("apples");
printList(myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("removing sausage...");
myGroceryList.remove ("sausage");
printList(myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("removing cheese...");
myGroceryList.remove ("cheese");
printList(myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
}
public static void printList(SortedStringListReferenceBased myList)
{
//method prints a list, numbering the values, e.g, "0. milk" .... "4. juice".... etc.
for (int index = 0; index < myList.size(); index++)
{
System.out.printf("%2d. %s ",index, (String) myList.get(index));
}
}
}
ListIndexOutOfBoundsException.java
/**
* class ListIndexOutOfBoundsException
*
* A class that implements the ListException class
*
*/
public class ListIndexOutOfBoundsException extends IndexOutOfBoundsException
{
public ListIndexOutOfBoundsException(String s)
{
super(s);
} // end constructor
private final static long serialVersionUID = 2006L;
} // end ListIndexOutOfBoundsException
ListException.java
/**
* class ListException
*
* A class that implements the ListException class
*
*/
public class ListException extends RuntimeException
{
public ListException(String s)
{
super(s);
} // end constructor
private final static long serialVersionUID = 2008L;
} // end ListException
This is all the files and the output should look like this.
0. apples
1. black beans
2. bread
3. butter
4. chicken
5. eggs
6. flour
7. milk
8. pecans
9. rice
10. sausage
numItems is now: 11
Adding juice
item 5 is: eggs
0. apples
1. black beans
2. bread
3. butter
4. chicken
5. eggs
6. flour
7. juice
8. milk
9. pecans
10. rice
11. sausage
numItems is now: 12
Explanation / Answer
public class SortedStringListReferenceBased implements SortedStringListInterface
{
private Node head;
private int numItems; // number of items in list
public SortedStringListReferenceBased()
// creates an empty list
{
head = new Node();
numItems = 0;
} // end default constructor
public boolean isEmpty()
// Determines whether a list is empty
{
return (numItems == 0);
} // end isEmpty
public int size()
// Returns the number of items that are in a list
{
return numItems;
} // end size
public void removeAll()
// Removes all the items in the list
{
head = null;
numItems = 0;
} // end removeAll
public void add(String item) throws ListException
// Inserts item into its proper position in a sorted list
// Throws an exception if the item connot be placed on the list
{
if (index >= 0 && index <= numItems)
return;
try
{ if(numItems == 0){
Node newNode = new Node(item, head);
head = newNode;
}else{
Node prev = find(index - 1);
Node newNode = new Node(item,prev.getNext());
prev.setNext(newNode);
}
numItems++;
// YOUR CODE WILL BE HERE...
// DEFINE ANY HELPER METHOD(S) AND CALL IT/THEM, IF YOU NEED ANY.
}
catch(Exception e)
{
throw new ListException("Add to List failed: " + e.toString());
}
}
public String get(int index) throws ListIndexOutOfBoundsException
// Retrieves the item at position index of a sorted list, if 0 <= index < size().
// The list is left unchanged by this operation.
// Throws an exception when index is out of range.
{
if (index >= 0 && index < numItems){
Node curr = find(index);
String dataItem = curr.getItem( );
return dataItem;
}else{
throw new ListIndexOutOfBoundsException(index + " is an invalid index");
}
}
public void remove(String item) throws ListException
// Removes the item from a sorted list.
// Throws an exception if the item is not found.
{
if (index >= 0 && index < numItems)
return;
try{
if (index == 0) // special case: delete the first node
{
head = head.getNext();
}
else{
Node prev = find(index - 1);
Node curr = prev.getNext();
prev.setNext(curr.getNext());
}
numItems--;
// YOUR CODE WILL BE HERE...
// REQUIREMENT: USE "locateIndex(String item)" method.
}
catch(Exception e)
{
throw new ListException("Remove " + item.toString() + " from List failed: " + e.toString());
}
}
public int locateIndex(String item)
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
{
Node curr = head;
for (int i = 0; i < size(); i++){
if (item.compareTo(curr.getItem())<= 0){
return i;
}//end if
else {
curr = curr.getNext();
}//end else
}//end for
return size()+1;
//TODO
} //end locateIndex()
private Node find(int index) {
// --------------------------------------------------
// Locates a specified node in a linked list.
// Precondition: index is the number of the desired
// node. Assumes that 1 <= index <= numItems+1
// Postcondition: Returns a reference to the desired
// node.
// --------------------------------------------------
Node curr = head;
for (int skip = 1; skip < index; skip++) {
curr = curr.getNext();
} // end for
return curr;
} // end find
} // end SortedListReferenceBased
Node.java
// *********************************************************************
// ---------Node Object--------
public class Node
// Assume the Node Object is String type
{
private String item;
private Node next;
public Node()
{//create a node without item or reference
item = null;
next = null;
}// end constructor
public Node(String newItem)
// create a node without reference
{
item = newItem;
next = null;
} // end constructor
public Node(String newItem, Node nextNode)
// create a node with reference to another node
{
item = newItem;
next = nextNode;
} // end constructor
public void setItem(String newItem) // store item in the node
{
item = newItem;
} // end setItem
public String getItem() // return item in the node
{
return item;
} // end getItem
public void setNext(Node nextNode) // reference to another node
{
next = nextNode;
} // end setNext
public Node getNext() // return the reference of the node
{
return next;
} // end getNext
} // end class Node
// =================================================================
SortedStringListInterface.java
// ********************************************************
// Interface SortedListInterface for the ADT SortedList.
// *********************************************************
public interface SortedStringListInterface
{
public boolean isEmpty();
// Determines whether a sorted list is empty
public int size();
// Returns the number of items that are in a sorted list
public void add(String item) throws ListException;
// Inserts item into it's proper position in a sorted list
// Throws an exception if the item connot be placed on the list
public String get(int index) throws ListIndexOutOfBoundsException;
// Retrieves the item at position index of a sorted list, if 0 <= index < size().
// The list is left unchanged by this operation.
// Throws an exception when index is out of range.
public void remove(String item) throws ListException;
// Removes the item from a sorted list.
// Throws an exception if the item is not found.
public int locateIndex(String item);
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
public void removeAll();
// Removes all the items in the list
} // end SortedStringListInterface
SortedStringListDriver.java
/**
* class SortedListDriver
*
* A driver class to execute methods from SortedStringListReferenceBased.java,
* which creates and a reference-based list and performs methods
* such as adding, deleting, finding a value in a list,
* and returning the index number.
*
*/
public class SortedStringListDriver
{
public static void main(String [] args)
{
SortedStringListReferenceBased myGroceryList = new SortedStringListReferenceBased();
myGroceryList.add("milk");
myGroceryList.add("eggs");
myGroceryList.add("butter");
myGroceryList.add("pecans");
myGroceryList.add("apples");
myGroceryList.add("bread");
myGroceryList.add("chicken");
myGroceryList.add("black beans");
myGroceryList.add("rice");
myGroceryList.add("sausage");
myGroceryList.add("flour");
printList(myGroceryList); //print out original List
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("adding juice ...");
myGroceryList.add ("juice"); //add juice
System.out.println("item 5 is: " + myGroceryList.get(5)); //get the content at index 5
printList(myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("removing juice...");
myGroceryList.remove ("juice");
printList(myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("removing apples...");
myGroceryList.remove ("apples");
printList(myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("removing sausage...");
myGroceryList.remove ("sausage");
printList(myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("removing cheese...");
myGroceryList.remove ("cheese");
printList(myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
}
public static void printList(SortedStringListReferenceBased myList)
{
//method prints a list, numbering the values, e.g, "0. milk" .... "4. juice".... etc.
for (int index = 0; index < myList.size(); index++)
{
System.out.printf("%2d. %s ",index, (String) myList.get(index));
}
}
}
ListIndexOutOfBoundsException.java
/**
* class ListIndexOutOfBoundsException
*
* A class that implements the ListException class
*
*/
public class ListIndexOutOfBoundsException extends IndexOutOfBoundsException
{
public ListIndexOutOfBoundsException(String s)
{
super(s);
} // end constructor
private final static long serialVersionUID = 2006L;
} // end ListIndexOutOfBoundsException
ListException.java
/**
* class ListException
*
* A class that implements the ListException class
*
*/
public class ListException extends RuntimeException
{
public ListException(String s)
{
super(s);
} // end constructor
private final static long serialVersionUID = 2008L;
} // end ListException
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.