Question: Edit the GenericList class so that it implements the MyStorage interface. CANNOT CHANGE THE MyStorage file. All of this is in java. Thank you GenericList
Edit the GenericList class so that it implements the MyStorage interface. CANNOT CHANGE THE MyStorage file. All of this is in java. Thank you
GenericList
import java.util.Arrays; public class GenericList //needed to change with addition to to make ot work for any type { private X[] arr; private int size; private void newArray() { arr = (X[]) new Object[10]; size = 0; } private void expandArray() { X[] arr2; arr2 = (X[]) new Object[(int)(arr.length * 1.2)]; for (int i = 0; i < arr.length; i++) arr2[i] = arr[i]; arr = arr2; } public GenericList() { this.newArray(); }
// size method - returns the size of the list public int size() { return size; }
/* * add method - add one value to the list in the next available position */ public void add(X value) { if (size == arr.length) // Is arr full? Then expand by 20% { this.expandArray(); } arr[size] = value; size++; }
// get method - return the value at the specified location in the list
// param - index into the list for the value to return
public X get(int index) throws ArrayIndexOutOfBoundsException { if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); else return arr[index]; }
public void clear() { this.newArray(); } public void insert(int index, X value) { if(index>=0 && index { if (size == arr.length) // Is arr full? Then expand by 20% { this.expandArray(); } //Open a hole to insert the value for (int i = size; i > index; i--) arr[i] = arr[i - 1]; arr[index] = value; size++; } //throw array here else if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); }
/* * toString - return a string value that represents the list * return - String */ public String toString() { String returnValue = ""; if (size != 0) { returnValue = String.valueOf(arr[0]); for (int i = 1; i < size; i++) returnValue = returnValue + ", " + arr[i]; } return returnValue; } public void display() { for (int i = 0; i < size; i++) System.out.println(i + ": " + arr[i]); if ( arr.length == size) System.out.println("List is full "); else System.out.println("List has " + (arr.length - size) + " spaces left "); } public void set(int index, X value) { { if(index>=0 && index { arr[index] = value; } //throw array here else if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); } }
public void remove(int index) { { //if (index < 0 || index >= this.size) //throw new ArrayIndexOutOfBoundsException(); if((index >=0) && (index < size)) { for (int i = index; i < size() - 1; i++) { arr[i] = arr[i + 1]; } --size; } //throw array here else if (index < 0 || index >= this.size) throw new ArrayIndexOutOfBoundsException(); } } }
public interface MyStorage { /* * size - returns the size of the list * * return - the size of the list as an integer */ public int size();
/* * add - add one value to the list in the next available position * * param - data to add to the list */ public void add(T value);
/* * get - return the value at the specified location in the list * * param - index into the list for the value to return * * returns - value at parameter */ public T get(int index);
/* * clear - empty the list */ public void clear();
/* * toString - return a string value that represents the list * * return - String */ public String toString();
/* * set - set value at indicated index * * param - index to set new value * param - value of new element */ public void set(int index, T value);
/* * swap - swap data between the two index values * * param - index 1 * param - index 2 * * The data at index1 is saved to index2 and the data at index2 * is saved to index1. */ public void swap(int index1, int index2); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
