Question
JAVA // TO DO: add your implementation and JavaDoc public class SmartArray { private static final int DEFAULT_CAPACITY = 2; //default initial capacity / minimum
JAVA
// TO DO: add your implementation and JavaDoc
public class SmartArray
private static final int DEFAULT_CAPACITY = 2; //default initial capacity / minimum capacity private T[] data; //underlying array
// ADD MORE PRIVATE MEMBERS HERE IF NEEDED! @SuppressWarnings("unchecked") public SmartArray(){ //constructor //initial capacity of the array should be DEFAULT_CAPACITY }
@SuppressWarnings("unchecked") public SmartArray(int initialCapacity){ // constructor // set the initial capacity of the smart array as initialCapacity
// throw IllegalArgumentException if initialCapacity is smaller than 1 }
public int size() { //report number of elements in the smart array // O(1) return 0; } public int capacity() { //report max number of elements before the next expansion // O(1)
return 0; } @SuppressWarnings("unchecked") public void add(int index, T value){ // insert value at index, shift elements if needed // double the capacity if no space is available // throw IndexOutOfBoundsException for invalid index // O(N) where N is the number of elements in the array // Note: this method may be used to append items as // well as insert items }
public T get(int index){ // return the item at index // throw IndexOutOfBoundsException for invalid index // O(1)
return null; }
public T replace(int index, T value){ // change item at index to be value // return old item at index // throw IndexOutOfBoundsException for invalid index // O(1) // Note: you cannot add new items with this method
return null; }
@SuppressWarnings("unchecked") public T delete(int index){ // remove and return element at position index // shift elements to remove any gap in the array // throw IndexOutOfBoundsException for invalid index // halve capacity if the number of elements falls below 1/4 of the capacity // capacity should NOT go below DEFAULT_CAPACITY // O(N) where N is the number of elements in the list
return null; }
@SuppressWarnings("unchecked") public boolean ensureCapacity(int newCapacity){ // change the max number of items allowed before next expansion to newCapacity // capacity should not be changed if: // - newCapacity is below DEFAULT_CAPACITY; or // - newCapacity is not large enough to accommodate current number of items // return true if newCapacity gets applied; false otherwise // O(N) where N is the number of elements in the array return false; }
// -------------------------------------------------------- // example testing code... edit this as much as you want! // --------------------------------------------------------
// Not required, update for your own testing public String toString(){ // return string representation of DynamicArray // update if you want to include more information return "SmartArray"; }
// Not required, update for your own testing public static void main (String args[]){
//create a smart array of integers SmartArray
//append some numbers for (int i=0; i<3;i++) nums.add(i,i*2); if (nums.size()==3 && nums.get(2) == 4 && nums.capacity() == 4 ){ System.out.println("Yay 2"); } //create a smart array of strings SmartArray
//delete and shrinking if (msg.delete(1).equals("beautiful") && msg.get(1).equals("world") && msg.size() == 3 && msg.capacity() == 10 ){ System.out.println("Yay 5"); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started