Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is a java question Add methods get(i) and set(i,e) on top of ListStaticArray and ListDynamicArray. public class ListStaticArray { private final int MAX_CAPACITY =

this is a java question

Add methods get(i) and set(i,e) on top of ListStaticArray and ListDynamicArray.

public class ListStaticArray { private final int MAX_CAPACITY = 10000; private T[] data; private int size; private int capacity; public ListStaticArray() { this.capacity = MAX_CAPACITY; data = (T[])new Object[capacity]; size = 0; } public ListStaticArray(final int capacity) { this.capacity = capacity; data = (T[])new Object[capacity]; size = 0; } /* Operations */ /* Returns the number of elements in the array list */ public int size() { return size; } public boolean isEmpty() { return size == 0; } public int capacity() { return capacity; } public void add(T element) { ensureCapacity(); data[size] = element; size++; } public void add(T element, int i) { ensureCapacity(); for (int k = size-1; k >= i; k--){ data[k+1] = data[k]; } data[i] = element; // assign the element to data[i] size++; } public void add(T[] elements) { for(int k = 0; k < elements.length; k++) { add(elements[k]); } } public T remove(int index) { if(index <= size-1) { T removed = data[index]; for (int k = index; k < size-1; k++) { data[k] = data[k+1]; } size--; return removed; } else { return null;} } public T removeTail() { T removed = data[--size]; return removed; } // static implementation private void ensureCapacity() throws IllegalStateException { if(capacity < size + 1 ) { throw new IllegalStateException ("The list is full"); } } public void printArray() { System.out.print("Array: "); if(size == 0) { System.out.print("EMPTY"); } else { for(int i = 0; i < size; i++) { System.out.printf("%s ", data[i]); } } System.out.print(" "); } public void printState() { System.out.printf("\tCapacity: %d \tSize: %d ", capacity, size); } } 
public class ListDynamicArray { private final int DEFAULT_CAPACITY = 3; private T[] data; private int size; private int capacity; public ListDynamicArray() { this.capacity = DEFAULT_CAPACITY; data = (T[])new Object[capacity]; size = 0; } public ListDynamicArray(final int capacity) { this.capacity = capacity; data = (T[])new Object[capacity]; size = 0; } /* Returns the number of elements in the array list */ public int size() { return size; } public boolean isEmpty() { return size == 0; } public int capacity() { return capacity; } public void add(T element) { ensureCapacity(); data[size] = element; size++; } public void add(T element, int i) { ensureCapacity(); for (int k = size-1; k >= i; k--){ data[k+1] = data[k]; } data[i] = element; // assign the element to data[i] size++; } public void add(T[] elements) { for(int k = 0; k < elements.length; k++) { add(elements[k]); } } public T remove(int index) { if(index <= size-1) { T removed = data[index]; for (int k = index; k < size-1; k++) { data[k] = data[k+1]; } size--; shrinkCapacity(); return removed; } else { return null;} } public T removeTail() { T removed = data[--size]; shrinkCapacity(); return removed; } // dynamic implementation private void ensureCapacity() { if(size == 0) { capacity = DEFAULT_CAPACITY; changeCapacity(); } if(size < capacity) { return; } this.capacity *= 2; // doubling the capacity changeCapacity(); } private void shrinkCapacity() { if(size > (capacity/2)) { return; } this.capacity /=2; changeCapacity(); } private void changeCapacity() { T[] newData = (T[]) new Object[capacity]; for(int i = 0; i < size; i++) { newData[i] = data[i]; } data = newData; } public void printArray() { System.out.print("Array: "); if(size == 0) { System.out.print("EMPTY"); } else { for(int i = 0; i < size; i++) { System.out.printf("%s ", data[i]); } } System.out.print(" "); } public void printState() { System.out.printf("\tCapacity: %d \tSize: %d ", capacity, size); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1. Write down two or three of your greatest strengths.

Answered: 1 week ago