Question
this needs to be in java language plz!! lab7 public class StringArray { private int capacity; private int size; // consider renaming the array to
this needs to be in java language plz!! lab7
public class StringArray { private int capacity; private int size; // consider renaming the array to tArray private String[] stringArray;
public StringArray() { this(10); }
public StringArray(int initialCapacity) throws IllegalArgumentException { if (initialCapacity
public StringArray(StringArray sa) throws NullPointerException { this(sa.capacity);
if (sa == null) throw new NullPointerException(); this.size = sa.size; for(int i = 0; i
public void add(String s) throws NullPointerException { try { add(size, s); } catch (NullPointerException ex) { throw ex; } catch (IndexOutOfBoundsException ex) { // do nothing, this should never occur } }
public void add(int index, String s) throws NullPointerException, IndexOutOfBoundsException { if (s == null) throw new NullPointerException(); if (index > size) throw new IndexOutOfBoundsException(); if(size == capacity) ensureCapacity(capacity + capacity/2);
for(int i = size; i > index; i--) { stringArray[i] = stringArray[i-1]; } stringArray[index] = s; size++; }
public int capacity() { return capacity; }
public void clear() { for(; size > 0; size--) stringArray[size - 1] = null; }
public boolean contains(String s) throws NullPointerException { return indexOf(s) > -1; }
public void ensureCapacity(int minCapacity) throws CapacityOutOfBoundsException { if (minCapacity
public String get(int index) throws IndexOutOfBoundsException { if(index >= size || index
public int indexOf(String s) throws NullPointerException { if (s == null) throw new NullPointerException(); int found = -1;
for(int i = 0; i
public boolean isEmpty() { return size == 0; }
public String remove(int index) throws IndexOutOfBoundsException { if(index >= size || index
public boolean remove(String s) throws NullPointerException { int index = indexOf(s); remove(index); return index > -1; }
public String set(int index, String s) throws IndexOutOfBoundsException, NullPointerException { if (index size) throw new IndexOutOfBoundsException(); if (s == null) throw new NullPointerException(); String oldString; if(index > capacity) { ensureCapacity(index + 1); oldString = null; } else { oldString = stringArray[index]; } if(index >= size) { size = index + 1; } stringArray[index] = s; return oldString; }
public int size() { return size; }
}
Part 1: Java Generics Using the solution for Lab 6 StringArray posted on Brightspace, rename the StringArray class to GenericArray. Modify the code throughout using Java generics to allow your GenericArray class to store any generic type, rather than String objects
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