Create a project for ArrayBasedList in Eclipse (you should download the files from blackboard

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.

a. Implement methods get(), remove(int index), contains(Object x), toString() Hint: First study v0 package.

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

ArrayBasedList.java

package v0;
import java.util.NoSuchElementException;
import java.lang.IndexOutOfBoundsException;

public class ArrayBasedList {
private static int MAX_LIST = 5;
private int items[];
private int numItems;
public ArrayBasedList(){
items = new int[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() {
int newArray[] = new int[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 an integer number to be added to the list * @param index position to insert x * @throws IndexOutOfBoundsException if index is not * between 0 and size() */ public void add(int index, int 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 an integer value
*/
public void add(int 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 int get(int index) throws IndexOutOfBoundsException {
if (index < 0 || index >= numItems)
throw new IndexOutOfBoundsException(“index: “+index);
return this.items[index];
}

/**
* 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 int remove(int index) throws NoSuchElementException{
if (index < 0 || index >= numItems)
throw new NoSuchElementException(“index: “+index);

int item = this.items[index];
for (int i = index+1; i < numItems; i++) // Shift items to the left. items[i - 1] = items[i]; numItems--; return item; } /** * Removes all of the elements from this list. */ public void clear(){ numItems = 0; } } Test.java package v0; public class test { public static void main( String [ ] args ) { ArrayBasedList list = new ArrayBasedList(); list.add(0, 7); list.add(0, 6); list.add(2, 5); list.add(1, 4); list.add(3); list.add(0, 2); list.add(4, 1); System.out.println("element at index 3 is: "+list.get(3)); list.remove(6); System.out.println("element at index 3 is: "+list.get(3)); //int x = list.items[0]; /*illegal*/ } } ArrayBasedList.java 2 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;
}

/**
* Removes the first occurrence of the specified element from this list,
* if it is present. If this list does not contain the element,
* it is unchanged.
* @param x any object that should be removed from the list
* @return true if this list contained the specified element,
* otherwise return false
*/
public boolean remove(AnyType x) {
int index;
for (index = 0; index < size(); index++) if (x.equals(items[index])) break; //the item is not in the list if (index == size()) return false; for (int i = index+1; i < numItems; i++) // Shift items to the left. items[i - 1] = items[i]; numItems--; return true; } /** * 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 ""; } } Test.java 2 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!
}
}

Complete Answer:

Get Instant Help in Homework Asap
Get Instant Help in Homework Asap
Calculate your paper price
Pages (550 words)
Approximate price: -
Open chat
1
Hello 👋
Thank you for choosing our assignment help service!
How can I help you?