Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create an Array class using Stacks, Interface, and Generic in Java Fields: myArray: a generic array numElements: an int which specifies how many items have
Create an Array class using Stacks, Interface, and Generic in Java
Fields:
- myArray: a generic array
- numElements: an int which specifies how many items have been added to the MyArray
- defCap: an int initialized to 10, which is the default size of a MyArray
- origCap: an int which specifies the original size of the MyArray
Methods:
- default constructor: Create an array of size 10, initialize origCap to 10 and numElements to 0.
- constructor with one int parameter: Create an array of the size given by the parameter, initialize origCap to the size of the array, and initialize numElements to 0.
- int size: Has no parms; returns numElements.
- boolean add(T item): append item to MyArray so that it is in the location right after what was the last element. Call expand if there is no room for another item, then add the item.
- boolean add(int loc, T item): add item to MyArray in location loc. Items from location loc to the end of the MyArray are shifted up to make room for item. For example, if the MyArray contains "yes" "no" "red" "green", add(2, "blue") will return "yes" "no" "blue" "red" "green". add can be used to add in any existing MyArray location or to the end of the MyArray. There cannot be any null items within the array, so if the array contains three elements you can add an element in loc three (the fourth element) but you can't add in any loc higher than that. Throw IndexOutOfBoundsException if loc is invalid. Call expand if there is no room for another item, then add the item.
- void expand(): expand is a private method, called by add to allocate a new larger array. The expand method creates a new array, copies the array values into the new array, and saves this new array in the myArray field. The size of the new array is the size of the existing array plus origCap.
- T remove(int loc): remove the element in position loc and return the element that was removed. Later items are moved down to fill in position loc, and the last position is set to null. For example, if the array contains "yes" "no" "blue" "red" "green", remove(2) will change the array to "yes" "no" "red" "green" null and return "blue". Decrement numElements. Throw IndexOutOfBoundsException if the subscript is invalid.
- T remove(T value): remove the first occurrence of value and return true. If value is not in the array it throws NoSuchElementException. Later items are moved down to fill in position loc, and null is put in the last position. Subtract one from numElements. For example, if the array contains "yes" "no" "blue" "red" "green", remove("blue") will change the array to "yes" "no" "red" "green" null and return "blue".
- T get(int): The parm is a subscript; get returns the array element at that subscript. For example, get(7) will return the 8th element in the array. Throws IndexOutOfBoundsException if the subscript is invalid. Use numElements as the upper bound, not the size of the array.
- void set(int loc, T item): set stores item in the array at location loc. For example, set(7, 44) will store 44 as the 8th element in the array. If loc does not contain an array element, throw ArrayIndexOutOfBoundsException.
- int indexOf(T item): search the array for item and return the subscript where item is found in the array. If item is not in the array return -1. If item is in the array more than once, return the subscript of the first occurrence.
- String toString(): return a String which contains the array elements separated by newlines.
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