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

Write a driver program which implements the various operations of the array-base

ID: 3600883 • Letter: W

Question

Write a driver program which implements the various operations of the array-based list ADT these operations should include those specified by the following pseudocode:
+createList()
+isEmpty():boolean {query}
+size():integer {query}
+add(in index:integer, in item:ListItemType)
+remove(in index:integer)
+removeAll()
+get(index):ListItemType {query}
The Java code for the array-based list is uploaded on the photos



Write a driver program which implements the various operations of the array-based list ADT these operations should include those specified by the following pseudocode:
+createList()
+isEmpty():boolean {query}
+size():integer {query}
+add(in index:integer, in item:ListItemType)
+remove(in index:integer)
+removeAll()
+get(index):ListItemType {query}
The Java code for the array-based list is uploaded on the photos



Write a driver program which implements the various operations of the array-based list ADT these operations should include those specified by the following pseudocode:
+createList()
+isEmpty():boolean {query}
+size():integer {query}
+add(in index:integer, in item:ListItemType)
+remove(in index:integer)
+removeAll()
+get(index):ListItemType {query}
The Java code for the array-based list is uploaded on the photos



File: ListInterface java Copyright (c) Pearson Addison-Wesley. All rights reserved. // Interface ListInterface for the ADT list. public interface ListInterface public boolean isEmpty0; Determines whether a list is empty /l Precondition: None Postcondition: Returns true if the list is empty // otherwise returns false / Throws: None public int size0; Determines the length of a list /l Precondition: None // Postcondition: Returns the number of items that are // currently in the list Throws: None public void add(int index, Object item) throws ListlndexOutOfBoundsException, ListException; / Adds an item to the list position index. Precondition: index indicates the position at whiclh // the item should be inserted in the list // Postcondition: If insertion is successful, item is // as position index in the list, and other items arc // renumbered accordingly Throws: ListIndexOutOfBoundsException if index0 or // index> sizeO /Throws: ListException if item cannot be placed on // the list

Explanation / Answer

Given below is the completed classes. Please dont forget to rate the answer if it helped. Thank you.

To indent code in eclipse, select code by pressing Ctrl+A and then Ctrl+i


public class ListException extends Exception {
public ListException() {
}
public ListException(String message){
super(message);
}
}


public class ListIndexOutOfBoundsException extends Exception {
public ListIndexOutOfBoundsException() {
}
public ListIndexOutOfBoundsException(String message){
super(message);
}
}

==================================
public interface ListInterface {
public boolean isEmpty();
public int size();
public void add (int index, Object item) throws ListIndexOutOfBoundsException, ListException;
public void remove (int index) throws ListIndexOutOfBoundsException;
public void removeAll();
public Object get (int index) throws ListIndexOutOfBoundsException;
}



==================================

public class ArrayList implements ListInterface
{
public static final int DEFAULT_CAPACITY = 10;
private Object[] elements;
private int size;

public ArrayList()
{
this(DEFAULT_CAPACITY);
}
  
public ArrayList(int capacity)
{
elements = new Object[capacity];
size = 0;
}

public int size()
{
return size;
}

public boolean isEmpty()
{
return size == 0;
}
  
public void add (int index, Object item) throws ListIndexOutOfBoundsException, ListException
{

if(index < 0 || index > size())
throw new ListIndexOutOfBoundsException("Index out of bounds");

if(size() == elements.length)
throw new ListException("List is full");
//move elements 1 position to right
for (int i = size; i > index; i--)
{
elements[i] = elements[i-1];
}
elements[index] = item;
size++;
}
  
public void remove (int index) throws ListIndexOutOfBoundsException
{
if(index < 0 || index >= size())
throw new ListIndexOutOfBoundsException("Index out of bounds should be in range 0-" + size());
  
  
for (int i = index + 1; i < size; i++)
{
elements[i-1] = elements[i];
}
size--;
}
public void removeAll()
{
size = 0;
}
public Object get (int index) throws ListIndexOutOfBoundsException
{
if(index < 0 || index >= size())
throw new ListIndexOutOfBoundsException("Index out of bounds should be in range 0-" + size());
return elements[index];
}

public String toString()
{
if (size == 0)
{
return "[]";
}
String s = "[";
for (int i = 0; i < size; i++)
{
s+= elements[i];
if (i!= size-1)
{
s+= ", ";
}
}
return s + "]";
}
  
}


==================================


public class ArrayListDriver {
public static void main(String[] args) throws ListIndexOutOfBoundsException, ListException {
ArrayList list = new ArrayList(5);
System.out.println("list.isEmpty() = " + list.isEmpty());
System.out.println("adding 5 elements to list");
for(int i = 0; i < 5; i++)
list.add(i, 5*i);
System.out.println("list.size() = " + list.size());
System.out.println("printing list using get()");
for(int i = 0; i < 5; i++)
System.out.println(list.get(i));
System.out.println("list.isEmpty() = " + list.isEmpty());
System.out.println("removing item at index 3");
list.remove(3);
System.out.println(list);
System.out.println("removing all elements using removeAll()");
list.removeAll();
System.out.println(list);
//now list is empty... trying to remove(index) or get() should throw exception
System.out.println("Testing if exception is thrown....");
System.out.println("item at index 0 = " + list.get(0));
}
}

==================================

output
======
list.isEmpty() = true
adding 5 elements to list
list.size() = 5
printing list using get()
0
5
10
15
20
list.isEmpty() = false
removing item at index 3
[0, 5, 10, 20]
removing all elements using removeAll()
[]
Testing if exception is thrown....
Exception in thread "main" ListIndexOutOfBoundsException: Index out of bounds should be in range 0-0
at ArrayList.get(ArrayList.java:78)
at ArrayListDriver.main(ArrayListDriver.java:26)

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