Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// ******************************************************** // Array-based implementation of the ADT list. // ********************************************************* public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private

image text in transcribed
image text in transcribed
image text in transcribed
// ********************************************************
// Array-based implementation of the ADT list.
// *********************************************************
public class ListArrayBased implements ListInterface {
private static final int MAX_LIST = 50;
private Object[] items; // an array of list items
private int numItems; // number of items in list
public ListArrayBased() {
// YOUR CODE GOES HERE.
} // end default constructor
public boolean isEmpty() {
// YOUR CODE GOES HERE.
} // end isEmpty
public int size() {
// YOUR CODE GOES HERE.
} // end size
public void removeAll() {
// Creates a new array; marks old array for garbage collection.
// YOUR CODE GOES HERE.
} // end removeAll
public void add(int index, Object item)
throws ListIndexOutOfBoundsException {
if (numItems >= MAX_LIST) {
throw new ListException("ListException on add");
} // end if
if (index >= 0 && index
// make room for new element by shifting all items at
// positions >= index toward the end of the
// list (no shift if index == numItems)
for (int pos = numItems-1; pos >= index; pos--) {
// YOUR CODE GOES HERE.
} // end for
// insert new item
// YOUR CODE GOES HERE.
}
else { // index out of range
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on add");
} // end if
} //end add
public Object get(int index)
throws ListIndexOutOfBoundsException {
if (index >= 0 && index
// YOUR CODE GOES HERE.
}
else { // index out of range
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on get");
} // end if
} // end get
public void remove(int index)
throws ListIndexOutOfBoundsException {
if (index >= 0 && index
// delete item by shifting all items at
// positions > index toward the beginning of the list
// (no shift if index == size)
for (int pos = index+1; pos
// YOUR CODE GOES HERE.
} // end for
// YOUR CODE GOES HERE.
}
else { // index out of range
throw new ListIndexOutOfBoundsException(
"ListIndexOutOfBoundsException on remove");
} // end if
} // end remove
} // end ListArrayBased
/**
* class ListException
*
* A class that implements the ListException class
*
*/
public class ListException extends RuntimeException
{
public ListException(String s)
{
super(s);
} // end constructor
} // end ListException
/**
* class ListIndexOutOfBoundsException
*
* A class that implements the ListException class
*
*/
public class ListIndexOutOfBoundsException extends IndexOutOfBoundsException
{
public ListIndexOutOfBoundsException(String s)
{
super(s);
} // end constructor
} // end ListIndexOutOfBoundsException
// ********************************************************
// Interface ListInterface for the ADT list.
// *********************************************************
public interface ListInterface {
public boolean isEmpty();
public int size();
public void add(int index, Object item)
throws ListIndexOutOfBoundsException,
ListException;
public Object get(int index)
throws ListIndexOutOfBoundsException;
public void remove(int index)
throws ListIndexOutOfBoundsException;
public void removeAll();
} // end ListInterface
// ********************************************************
// Array-based implementation of the ADT Sorted List.
// *********************************************************
/**
* class SortredListArrayBased
*
* A class gets inherited from ListArrayBased
*
*/
public class SortedListArrayBased extends ListArrayBased
{
public SortedListArrayBased()
// creates an empty list
{
// TO BE IMPLEMENTED BY YOU
}// end default constructor
public void add(Object 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
{
// TO BE IMPLEMENTED BY YOU
}
catch(Exception e)
{
throw new ListException("Add to List failed: " + e.toString());
}
}
public void remove(Object item) throws ListException
// Removes the item from a sorted list.
// Throws an exception if the item is not found.
{
try
{
// TO BE IMPLEMENTED BY YOU
}
catch(Exception e)
{
throw new ListException("Remove " + item.toString() + " from List failed: " + e.toString());
}
}
public int locateIndexToAdd(Object item)
{
// TO BE IMPLEMENTED BY YOU
}
public int locateIndexToRemove(Object item)
// Returns the position where the item belongs or exists in a sorted list;
// Otherwise, it returns -1.
{
// TO BE IMPLEMENTED BY YOU
}
} // end SortedListArrayBased
myGroceryList.remove ("cheese"); //remove item at position (i.e., index) 5
// printList(myGroceryList);
printList((SortedListArrayBased) myGroceryList);
System.out.print("numItems is now: " + myGroceryList.size() + " ");
System.out.println("main(String[] args) is called...");
}
*/
public static void adding(SortedListArrayBased myList, String str)
{
System.out.println("Adding " + str + " ...");
myList.add(str); //add juice
printList(myList);
System.out.print("numItems is now: " + myList.size() + " ");
}
public static void removing(SortedListArrayBased myList, String str)
{
System.out.println("Removing " + str + " ...");
myList.remove(str);
printList(myList);
System.out.print("numItems is now: " + myList.size() + " ");
}
public static void printList(SortedListArrayBased myList)
{
//method prints a list, numbering the values, e.g, "0. milk" .... "4. juice".... etc.
for (int index = 0; index
{
System.out.printf("%2d. %s ",index, (String) myList.get(index));
}
}
}
Sorted ListArrayBased Topics Throughout this lab, we want to practice abstract data type (ADT). In particular, this lab asks you to implement a Sorted ListttayBased class, which extends Listatay Based class that implements ListInterface NOTE: After we learn Ch.5, you will be asked to implement another SortedListBeference Based class, which will extends "ListReference Based" class that implements "ListInterface". Activities

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions