Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sorted String List Array Based in java My driver program in Java is compiling co

ID: 3563050 • Letter: S

Question

Sorted String List Array Based in java

My driver program in Java is compiling correctly. However when I run the program from the driver file the output is numItems is now: 0

adding juice ...

Exception in thread "main" ListIndexOutOfBoundsExceiption: 5 is an invalid index

I can only make changes in SortedStringListArrayBased.java for this assignment. I believe the program is having an issue in the add command and I am unsure how to fix it, but the end result when I run the program is supposed to be 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: 11Adding juice

Explanation / Answer

// ********************************************************
// Array-based implementation of the ADT Sorted List.
// *********************************************************
/**
* class SortedStringListArrayBased
*
* A class that implements the SortedListInterface using an array
*
*/
// Student Name:
import java.util.ArrayList;
import java.util.*;
class SortedStringListArrayBased implements SortedStringListInterface
{
    private static final int MAX_LIST = 50;
    private String[] items; // a reference to an array of String objects
    private int numItems; // number of items in list
    public SortedStringListArrayBased()
    // creates an empty list
    {
        items = new String[MAX_LIST];
        numItems = 0;
    }
    // end default constructor
    public boolean isEmpty()
    // Determines whether a list is empty
    {
        //return numItems == 0;
        if (numItems==0)
        return true;
        else
        return false;
    }
    // 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
    {
        items = new String[MAX_LIST];
        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
    {
        try
        {
            if (numItems >= MAX_LIST) {
                System.out.println(numItems + " items cannot be accomodated in the list");
                return;

            }
            int index=locateIndex(item);
            //System.out.println(index);
            if (index >= 0 && index <= numItems) {
                // shift right
                for (int pos = numItems; pos > index; pos--)
                {
                    items[pos] = items[pos-1];
                }
                items[index] = item;
                numItems++;
            }
            else {
                items[0] = item;
                numItems++;
                //System.out.println("Index is not found in the list");
                return;

                // handling Exception
            }
        // 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){
            return items[index];
        }
        else{
            // index out of range
            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.
    {
        try
        {
            int index=locateIndex(item);
            if (index >= 0 && index < numItems) {
                // shift left
                for (int pos = index+1; pos < size( ); pos++)
                {
                    items[pos-1] = items[pos];
                }
                numItems--;
            }
            else {
                // index out of range Exception handling;
            }
        //}
        // 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 (i.e., index) where the item belongs or exists in a sorted list;
    // Otherwise, it returns -1.
    // NOTE: (1) "locateIndex(String item)" does the same operation that "sequentialSearch" does.
    // (2) It is completely implemented; thus, you can just use this code as it is given.
    {
       int i;
        for ( i = 0; i < size(); i++)
        if (items[i].compareTo(item) > 0)
        return i;
        if(i == size()) return i;

        return -1;
    }
    public String toString()
    {
        String st= new String(" ");
        for (int i=0; i < size(); i++)
        st += items[i] + " ";
        return st;
    }
}
// end SortedStringListArrayBased

/**
* class SortedStringListDriver
*
* A driver class to execute methods from SortedStringList.java, which
* creates a sorted 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)
    {
        SortedStringListArrayBased myGroceryList = new SortedStringListArrayBased();
        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"); //remove item at position (i.e., index) 5
        printList(myGroceryList);
        System.out.print("numItems is now: " + myGroceryList.size() + " ");
        System.out.println("removing cheese...");
        myGroceryList.remove ("cheese"); //remove item at position (i.e., index) 5
        printList(myGroceryList);
        System.out.print("numItems is now: " + myGroceryList.size() + " ");
    }
    public static void printList(SortedStringListArrayBased 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));
        }
    }
}
// end of SortedStringListDriver
// *****************************************************************
// Interface SortedStringListInterface for the ADT SortedStringList.
// *****************************************************************
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 ListInterface
/**
* class ListException
*
* A class that implements the ListException class
*
*/
class ListException extends RuntimeException
{
    public ListException(String s)
    {
        super(s);
    }
    // end constructor
}
// end ListException
/**
* class ListIndexOutOfBoundsException
*
* A class that implements the ListException class
*
*/
class ListIndexOutOfBoundsException extends IndexOutOfBoundsException
{
    public ListIndexOutOfBoundsException(String s)
    {
        super(s);
    }
    // end constructor
}
// end ListIndexOutOfBoundsException

//Sample output

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
removing juice...
0. apples
1. black beans
2. bread
3. butter
4. chicken
5. eggs
6. flour
7. juice
8. pecans
9. rice
10. sausage
numItems is now: 11
removing apples...
0. apples
1. bread
2. butter
3. chicken
4. eggs
5. flour
6. juice
7. pecans
8. rice
9. sausage
numItems is now: 10
removing sausage...
0. apples
1. bread
2. butter
3. chicken
4. eggs
5. flour
6. juice
7. pecans
8. rice
9. sausage
numItems is now: 10
removing cheese...
0. apples
1. bread
2. butter
3. eggs
4. flour
5. juice
6. pecans
7. rice
8. sausage
numItems is now: 9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote