Question: Create a project for ArrayBasedList in Eclipse (you should download the files from blackboard and import them to your project). ArrayBasedList contains two packages, each

Create a project for ArrayBasedList in Eclipse (you should download the files from blackboard and import them to your project). ArrayBasedList contains two packages, each package contains two files, ArrayBasedList.java and test.java. Some methods are not complete in package v1. You need to implement them.

Implement methods get(), remove(), contains(), toString()

Modify the main() method in class test to test your methods

package v1; import java.util.*; import java.lang.IndexOutOfBoundsException; public class ArrayBasedList { private static int MAX_LIST = 5; private AnyType items[]; private int numItems; public ArrayBasedList(){ items = (AnyType []) new Object[MAX_LIST]; numItems = 0; } /** * Tests if the list contains no element * @return true if the list contains no element */ public boolean isEmpty(){ return (numItems == 0); } /** * @return the number of items in this list */ public int size() {return numItems;} /** * An internal method to expand the size of array items * Allocates a new array, twice as long. * Updates the MAX_LIST */ private void expand() { AnyType newArray[] = (AnyType []) new Object[2 * MAX_LIST]; // Allocate a new array, twice as long. for (int i = 0; i < numItems; i++) // Copy items to the bigger array. newArray[i] = this.items[i]; this.items = newArray; // Replace the too-small array with the new one. MAX_LIST *=2; } /** * Adds an item to this list at the specified position * @param x any object * @param index position to insert x * @throws IndexOutOfBoundsException if index is not * between 0 and size() */ public void add(int index, AnyType x) throws IndexOutOfBoundsException { // No room left in the array? if (size() == MAX_LIST) expand(); if (index < 0 || index > size()) throw new IndexOutOfBoundsException("add at index: "+index); for (int i = numItems-1; i >= index; i--) // Shift items to the right. items[i + 1] = items[i]; items[index] = x; numItems++; } /** * Adds an item to this list, at the end * @param x any object */ public void add(AnyType x) { this.add(size(), x); } /** * Returns the item at position index * @param index the index to search in * @return return the item in index * @throws IndexOutOfBoundsException if index is not * between 0 and size() */ public AnyType get(int index) throws IndexOutOfBoundsException { // Put your code here!! return null; } /** * Removes an item from this list * @param index the index of the object * @return the item that was removed from the list * @throws IndexOutOfBoundsException if the index is out of range */ public AnyType remove(int index) throws NoSuchElementException{ // Put your code here!! return null; } /** * Tests if some item is in this list * @param x any object * @return true if this list contains an item equal to x */ public boolean contains(Object x) { // Put your code here!! return false; } /** * Removes all of the elements from this list. */ public void clear(){ numItems = 0; } /** * Returns the index of first item matching x * in this list, or -1 if not found. * @param x any object * @return the index of first item matching x * or -1 if not found. */ public int indexOf(Object x) { for (int i = 0; i < size(); i++) if (x.equals(items[i])) return i; return -1; } public String toString() { // Put your code here!! return ""; } }
package v1; public class test { public static void main( String [ ] args ) { ArrayBasedList groceryList = new ArrayBasedList(); groceryList.add(0, "milk"); groceryList.add(0, "bread"); groceryList.add(0, "cheese"); groceryList.add(0, "fruit"); System.out.println("The size of list is: "+groceryList.size()); groceryList.remove(1); System.out.println("List was updated, the size is: "+groceryList.size()); System.out.println("index 1 is: "+groceryList.get(1)); System.out.println("The whole list is: "+ groceryList); // the output should look like this: The whole list is: [ fruit bread milk ] System.out.println("index of value 5 is: "+groceryList.indexOf(5)); System.out.println("The grocery list contains 'milk'? "+groceryList.contains("milk")); //ArrayBasedList list = new ArrayBasedList(); //this is illegal! primitive types are not allowed ArrayBasedList list = new ArrayBasedList(); //this is legal! } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!