Question
Create a class named DynamicArray that will have convenient functionality similar to JavaScript's Array object and Java's ArrayList class. The class allows to store array
Create a class named DynamicArray that will have convenient functionality similar to JavaScript's Array object and Java's ArrayList class. The class allows to store array of integers that can grow and shrink as needed, search for values, remove elements, etc.
You are not allowed to use ArrayList object as well as any methods from java.util.Arrays class.
Please see the list of required features and methods below.
1. private int array[] You MUST store the data internally in a regular partially-filled array of integers. Please DO NOT USE ArrayList. The size of the allocated array is its capacity and will be discussed below.
2. private int size. This variable stores the number of "occupied" elements in the array. Set to 0 in the constructor.
3. Constructor with parameter. The parameter defines the capacity of initial array. Allocates array of given capacity, sets size field to 0. In case the parameter given to constructor is less than 0, IllegalArgumentException is being thrown.
4. No-argument constructor. Allocates array of size 3, sets size field to 0.
5. Copy constructor. The constructor takes an object of type DynamicArray as a parameter and copies it into the object it creates. The constructor throws IllegalArgumentException if the object that was passed to copy from is null.
6. int getSize() returns the number of "occupied" elements in the array.
7. int getCapacity() returns the actual size (length) of the partially-filled array
8. int [] getArray() accessor returns the entire partially-filled array. Make sure you DO NOT return the private array field, make a copy of it.
9. int [] toArray() accessor returns an "occupied" part of the partially-filled array. Make sure you DO NOT return the private array field. Instead, allocate memory for the new array, copy the "occupied" portion of the field into that new object, and return the new array.
10. public void push(int num) adds a new element to the end of the array and increments the size field. If the array is full, you need to increase the capacity of the array:
-Create a new array with the size equal to double the capacity of the original one.
-Copy all the elements from the array field to the new array.
-Add the new element to the end of the new array.
-Use new array as an array field.
-Make sure your method works well when new elements are added to an empty DynamicArray.
11. public int pop() throws RuntimeException removes the last element of the array and returns it. Decrements the size field. If the array is empty a RuntimeException with the message "Array is empty" must be thrown. At this point check the capacity of the array. If the capacity is 4 times larger than the number of occupied elements (size), it is time to shrink the array:
-Create a new array with the size equal to half of the capacity of the original one.
-Copy all the elements from the array field to the new array.
-Use new array as an array field.
12. int get(int index) throws IndexOutOfBoundsException returns element of the array with the requested index. If the index provided is too large or negative, the IndexOutOfBoundsException is thrown with the message "Illegal index".
13. int indexOf(int key) returns the index of the first occurrence of the given number. Returns -1 when the number is not found.
14. void add(int index, int num) throws IndexOutOfBoundsException adds a new element (passed as parameter num) to the location of the array specified by index parameter. If the index is larger than size of the array or less than 0, IndexOutOfBoundsException is thrown. When adding the element into the middle of the array, you'll have to shift all the elements to the right to make room for the new one. If the array is full and there is no room for a new element, the array must be doubled in size. Please follow the steps listed in the push() method description to double the capacity of the array.
15. int remove ( int index) throws IndexOutOfBoundsException removes the element at the specified position in this array. When the element is removed from the middle of the array, all the elements must be shifted to close the gap created by removed element. If the index value passed into the method is more or equal to the size or less than 0 the IndexOutOfBoundsException must be thrown. At this point check the capacity of the array. If the capacity is 4 times larger than the number of occupied elements (size), it is time to shrink the array.
16. boolean isEmpty() returns true if the size of the array is 0.
17. int findMin()throws RuntimeException returns the smallest element in the array. If the array is empty throws RuntimeException
18. int findMax()throws RuntimeException returns the largest element in the array. If the array is empty throws RuntimeException
19. String toString() - returns an array as a string of comma-separated values. Sample: [1, 2, 3, 4]
20. boolean equals(DynamicArray obj) compares two objects (this one and the one passed as parameter) element-by-element and determines if they are exactly the same. The capacity of the two objects is not being compared and can be different.
This is the code that I have so far:
public class DynamicArray {private int array[];private int size;/**Constructor@param capacity - integerthrows an IllegalArgumentExeception if capacity is less than 0**/public DynamicArray (int capacity){if (capacity 0){throw new IllegalArgumentException("Size cannot be less than 0.");array = new int[capacity];}}/**no-arg constructor**/private static final int DEFAULT_CAPACITY = 10;public DynamicArray(){this(DEFAULT_CAPACITY);}/**Copies the array to a new one@param - array [] - integer array**/public DynamicArray(DynamicArray source){if (source == null)throw new IllegalArgumentException("Source array must not be null.");this.array = Arrays.copyOf(source.array, source.array.length);this.size = source.size;}/**getSize returns the size.@return - size**/public int getSize(){return size;}/**@param array[] - integer@return array**/public int[] toArray(){return Arrays.copyOf(array, array.length);}/**@param num - integer**/public void push(int num){growIfNecessary();array[size] = num;size += 1;}/**@return pop - integer**/public int pop(){if (size == 0)throw new NoSuchElementException();int result = array[--size];shrinkIfNecessary();return result;}/**@param index - integer**/public int get(int index) throws IndexOutOfBoundsException{if (index >= size || index 0)throw new IndexOutOfBoundsException("Illegal Index");return array[index];}public int indexOf(int key){for (int i = 0; i if (array[i] == key)return i; // foundreturn -1; // not found}public void add(int index, int num) throws IndexOutOfBoundsException{if (index > size || index 0)throw new IndexOutOfBoundsException("Illegal index");growIfNecessary();System.arraycopy(array, index, array, index + 1, size - index);array[index] = num;size += 1;}public int remove(int index){if (index >= size || index 0)throw new IndexOutOfBoundsException("Illegal Index");int result = array[index];System.arraycopy(array, index + 1, array, index, size - index - 1);size -= 1;shrinkIfNecessary();return result;}}
But this is the code with the tests that I need to pass:
/*** @param args the command line arguments*/public static void main(String[] args) {int total = 0;PrintStream out = System.out;total += testSet01(out);total += testSet02(out);total += testSet03(out);total += testSet04(out);total += testSet05(out);total += testSet06(out);total += testSet07(out);total += testSet08(out);total += testSet09(out);total += testSet10(out);System.out.println(" You earned "+ total + " out of 100 points for the assignment");}/*** Set of unit tests for no-argument constructor, getSize(), getCapacity(), and isEmpty() methods* @param outputStream stream to direct output into* @return number of points earned for this unit. 0 is returned if even one of the tests failed*/public static int testSet01(PrintStream outputStream){int count = 0;int expectedCount = 2;int points = 10;outputStream.println(" ----Test Set 1---- ");DynamicArray a1 = new DynamicArray();// Test #1if(a1.getCapacity()==3 && a1.getSize()==0){outputStream.printf("%-80s%-10s ", "Test Set 01: Test for no-argument constructor, getSize() and getCapacity()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 01: Test for no-argument constructor, getSize() and getCapacity()","FAILED");// Test #2if(a1.isEmpty()){outputStream.printf("%-80s%-10s ", "Test Set 01: Test for isEmpty()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 01: Test Set 01: Test for isEmpty()","FAILED");if(count==expectedCount) return points;else return 0;}/*** Set of unit tests for constructor with parameter and getArray() methods* @param outputStream stream to direct output into* @return number of points earned for this unit. 0 is returned if even one of the tests failed*/public static int testSet02(PrintStream outputStream){int count = 0;int expectedCount = 4;int points = 10;outputStream.println(" ----Test Set 2---- ");int[] empty1 = {0, 0, 0};int[] empty2 = {0, 0, 0, 0, 0};DynamicArray a1 = new DynamicArray();DynamicArray a2 = new DynamicArray(5);// Test #1if(equal(empty1, a1.getArray())){outputStream.printf("%-80s%-10s ", "Test Set 02: First test for getArray()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 02: First test for getArray()","FAILED");// Test #2int[] test1= a1.getArray();test1[0] = 1000;if(!equal(test1, a1.getArray())){outputStream.printf("%-80s%-10s ", "Test Set 02: Test for getArray() deep copy","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 02: Test for getArray() deep copy","FAILED");// Test #3if(equal(empty2, a2.getArray())&& a2.getSize()==0){outputStream.printf("%-80s%-10s ", "Test Set 02: First test for constructor with parameter","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 02: First test for constructor with parameter","FAILED");// Test #4try{DynamicArray a3 = new DynamicArray(-5);outputStream.printf("%-80s%-10s ", "Test Set 02: Test for constructor with parameter & exception","FAILED");}catch(IllegalArgumentException e){outputStream.printf("%-80s%-10s ", "Test Set 02: Test for constructor with parameter & exception","PASSED");count++;}if(count==expectedCount) return points;else return 0;}/*** Set of unit tests for push() method* @param outputStream stream to direct output into* @return number of points earned for this unit. 0 is returned if even one of the tests failed*/public static int testSet03(PrintStream outputStream){int count = 0;int expectedCount = 4;int points = 10;outputStream.println(" ----Test Set 3---- ");int[] test1 = {0, 1, 2, 3, 4};DynamicArray a1 = new DynamicArray(5);addValues(a1, 5);// Test #1if(equal(test1, a1.getArray())&& a1.getCapacity()==5 && a1.getSize()== 5){outputStream.printf("%-80s%-10s ", "Test Set 03: First test for push()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 03: First test for push()","FAILED");// Test #2DynamicArray a2 = new DynamicArray(5);addValues(a2, 6);if(matchPartiallyFilled(a2.getArray(), 6, 10)){outputStream.printf("%-80s%-10s ", "Test Set 03: Second test for push() - doubling capacity once","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 03: Second test for push() - doubling capacity once","FAILED");// Test #3DynamicArray a3 = new DynamicArray(5);addValues(a3, 31);if(matchPartiallyFilled(a3.getArray(), 31, 40)){outputStream.printf("%-80s%-10s ", "Test Set 03: Third test for push() - doubling capacity twice","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 03: Third test for push() - doubling capacity twice","FAILED");// Test #4DynamicArray a4 = new DynamicArray();addValues(a4, 10);if(matchPartiallyFilled(a4.getArray(), 10, 12)){outputStream.printf("%-80s%-10s ", "Test Set 03: Fourth test for push() - starting from \"empty\" array","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 03: Fourth test for push() - starting from \"empty\" array","FAILED");if(count==expectedCount) return points;else return 0;}/*** Set of unit tests for copy constructor method* @param outputStream stream to direct output into* @return number of points earned for this unit. 0 is returned if even one of the tests failed*/public static int testSet04(PrintStream outputStream){int count = 0;int expectedCount = 3;int points = 10;outputStream.println(" ----Test Set 4---- ");// Test #1DynamicArray a1 = new DynamicArray(5);a1.push(5);a1.push(6);DynamicArray copy1 = new DynamicArray(a1);if(equal(a1.getArray(), copy1.getArray())){outputStream.printf("%-80s%-10s ", "Test Set 04: Test 1 for copy constructor","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 04: Test 1 for copy constructor","FAILED");// Test #2a1.push(7);copy1.push(12);if(!equal(a1.getArray(), copy1.getArray())){outputStream.printf("%-80s%-10s ", "Test Set 04: Test 2 for copy constructor - deep copy","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 04: Test 2 for copy constructor - deep copy","FAILED");// Test #3try{DynamicArray a3 = new DynamicArray(null);outputStream.printf("%-80s%-10s ", "Test Set 04: Test 3 for copy constructor - exception","FAILED");}catch(IllegalArgumentException e){outputStream.printf("%-80s%-10s ", "Test Set 04: Test 3 for copy constructor - exception","PASSED");count++;}if(count==expectedCount) return points;else return 0;}/*** Set of unit tests for pop() method* @param outputStream stream to direct output into* @return number of points earned for this unit. 0 is returned if even one of the tests failed*/public static int testSet05(PrintStream outputStream){int count = 0;int expectedCount = 4;int points = 10;outputStream.println(" ----Test Set 5---- ");int[] test1 = {0, 1, 2, 3, 4};DynamicArray a1 = new DynamicArray(40);a1.push(10);a1.push(20);a1.push(30);int k = a1.pop();// Test #1if(a1.getCapacity()==20 && a1.getSize()== 2 && k==30){outputStream.printf("%-80s%-10s ", "Test Set 05: Test 1 for pop()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 05: Test 1 for pop()","FAILED");k = a1.pop();// Test #2if(a1.getCapacity()==10 && a1.getSize()== 1 && k==20){outputStream.printf("%-80s%-10s ", "Test Set 05: Test 2 for pop()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 05: Test 2 for pop()","FAILED");k = a1.pop();// Test #3if(a1.getCapacity()==5 && a1.getSize()== 0 && k==10){outputStream.printf("%-80s%-10s ", "Test Set 05: Test 3 for pop()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 05: Test 3 for pop()","FAILED");// Test #4try{k = a1.pop();outputStream.printf("%-80s%-10s ", "Test Set 05: Test 4 for pop() - exception","FAILED");}catch(RuntimeException e){outputStream.printf("%-80s%-10s ", "Test Set 05: Test 4 for pop() - exception","PASSED");count++;}if(count==expectedCount) return points;else return 0;}/*** Set of unit tests for get(), indexOf(), and toArray() methods* @param outputStream stream to direct output into* @return number of points earned for this unit. 0 is returned if even one of the tests failed*/public static int testSet06(PrintStream outputStream){int count = 0;int expectedCount = 4;int points = 10;outputStream.println(" ----Test Set 6---- ");int[] test1 = {0, 1, 2, 3, 4};DynamicArray a1 = new DynamicArray(20);addValues(a1, 12);// Test #1if(a1.get(0)==0 && a1.get(11)== 11){outputStream.printf("%-80s%-10s ", "Test Set 06: Test 1 for get()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 06: Test 1 for get()","FAILED");// Test #2try{a1.get(13);outputStream.printf("%-80s%-10s ", "Test Set 06: Test 2 for get() - exception","FAILED");}catch(IndexOutOfBoundsException e){outputStream.printf("%-80s%-10s ", "Test Set 06: Test 2 for get() - exception","PASSED");count++;}// Test #3if(a1.indexOf(0) == 0 && a1.indexOf(11) == 11 && a1.indexOf(33)==-1){outputStream.printf("%-80s%-10s ", "Test Set 06: Test 1 for indexOf()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 06: Test 1 for indexOf()","FAILED");int[] test4 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};int[] empty = {};DynamicArray a2 = new DynamicArray(3);DynamicArray a3 = new DynamicArray();// Test #4if(equal(a1.toArray(), test4)&& equal(a2.toArray(), empty) && equal(a3.toArray(), empty)){outputStream.printf("%-80s%-10s ", "Test Set 06: Test 1 for toArray()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 06: Test 1 for toArray()","FAILED");if(count==expectedCount) return points;else return 0;}/*** Set of unit tests for add() method* @param outputStream stream to direct output into* @return number of points earned for this unit. 0 is returned if even one of the tests failed*/public static int testSet07(PrintStream outputStream){int count = 0;int expectedCount = 3;int points = 10;outputStream.println(" ----Test Set 7---- ");// Test #1DynamicArray a1 = new DynamicArray(2);a1.add(0, 5);a1.add(0, 6);a1.add(1, 7);int[] test1 ={6, 7, 5, 0};if(equal(a1.getArray(), test1) && a1.getCapacity()==4){outputStream.printf("%-80s%-10s ", "Test Set 07: Test 1 for add()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 07: Test 1 for add()","FAILED");// Test #2a1.add(2, 2);a1.add(3, 3);int[] test2 = {6, 7, 2, 3, 5, 0, 0, 0};if(equal(a1.getArray(), test2) && a1.getCapacity()==8){outputStream.printf("%-80s%-10s ", "Test Set 07: Test 2 for add()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 07: Test 2 for add()","FAILED");// Test #3try{a1.add(55, 55);outputStream.printf("%-80s%-10s ", "Test Set 07: Test 3 for add() - exception","FAILED");}catch(IndexOutOfBoundsException e){outputStream.printf("%-80s%-10s ", "Test Set 07: Test 3 for add() - exception","PASSED");count++;}if(count==expectedCount) return points;else return 0;}/*** Set of unit tests for remove() method* @param outputStream stream to direct output into* @return number of points earned for this unit. 0 is returned if even one of the tests failed*/public static int testSet08(PrintStream outputStream){int count = 0;int expectedCount = 4;int points = 10;outputStream.println(" ----Test Set 8---- ");DynamicArray a1 = new DynamicArray(40);a1.push(10);a1.push(20);a1.push(30);a1.remove(0);int[] test1 = {20, 30};// Test #1if(a1.getCapacity()==20 && a1.getSize()== 2 && equal(a1.toArray(), test1)){outputStream.printf("%-80s%-10s ", "Test Set 08: Test 1 for remove()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 08: Test 1 for remove()","FAILED");// Test #2a1.remove(1);int[] test2 = {20};if(a1.getCapacity()==10 && a1.getSize()== 1 && equal(a1.toArray(), test2)){outputStream.printf("%-80s%-10s ", "Test Set 08: Test 2 for remove()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 08: Test 2 for remove()","FAILED");// Test #3try{a1.remove(1);outputStream.printf("%-80s%-10s ", "Test Set 08: Test 3 for remove() - exception","FAILED");}catch(IndexOutOfBoundsException e){outputStream.printf("%-80s%-10s ", "Test Set 08: Test 3 for remove() - exception","PASSED");count++;}// Test #4a1.remove(0);int[] test4 = {};if(a1.getCapacity()==5 && a1.getSize()== 0 && equal(a1.toArray(), test4)){outputStream.printf("%-80s%-10s ", "Test Set 08: Test 4 for remove()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 08: Test 4 for remove()","FAILED");if(count==expectedCount) return points;else return 0;}/*** Set of unit tests for findMax() and findMin() methods* @param outputStream stream to direct output into* @return number of points earned for this unit. 0 is returned if even one of the tests failed*/public static int testSet09(PrintStream outputStream){int count = 0;int expectedCount = 4;int points = 10;outputStream.println(" ----Test Set 9---- ");DynamicArray a1 = new DynamicArray(40);a1.push(10);a1.push(20);a1.push(30);int min = a1.findMin();int max = a1.findMax();// Test #1if(min==10 && max==30){outputStream.printf("%-80s%-10s ", "Test Set 09: Test 1 for findMin() and findMax()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 09: Test 1 for findMin() and findMax()","FAILED");// Test #2a1.push(0);min = a1.findMin();if(min==0){outputStream.printf("%-80s%-10s ", "Test Set 09: Test 2 for findMin()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 09: Test 2 for findMin()","FAILED");// Test #3DynamicArray empty = new DynamicArray();try{min = empty.findMin();outputStream.printf("%-80s%-10s ", "Test Set 09: Test 3 for findMin() - exception","FAILED");}catch(RuntimeException e){outputStream.printf("%-80s%-10s ", "Test Set 09: Test 3 for findMin() - exception","PASSED");count++;}// Test #4try{min = empty.findMax();outputStream.printf("%-80s%-10s ", "Test Set 09: Test 4 for findMax() - exception","FAILED");}catch(RuntimeException e){outputStream.printf("%-80s%-10s ", "Test Set 09: Test 4 for findMax() - exception","PASSED");count++;}if(count==expectedCount) return points;else return 0;}/*** Set of unit tests for toString() and equals() methods* @param outputStream stream to direct output into* @return number of points earned for this unit. 0 is returned if even one of the tests failed*/public static int testSet10(PrintStream outputStream){int count = 0;int expectedCount = 4;int points = 10;outputStream.println(" ----Test Set 10---- ");DynamicArray empty1 = new DynamicArray(4);DynamicArray empty2 = new DynamicArray();DynamicArray a1 = new DynamicArray(40);DynamicArray a2 = new DynamicArray(10);a1.push(10);a1.push(20);a1.push(30);a2.push(10);a2.push(20);a2.push(30);// Test #1if(a1.toString().equals("[10, 20, 30]")){outputStream.printf("%-80s%-10s ", "Test Set 10: Test 1 for toString()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 10: Test 1 for toString()","FAILED");// Test #2if(empty1.toString().equals("[ ]")){outputStream.printf("%-80s%-10s ", "Test Set 10: Test 2 for toString() - empty array","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 10: Test 2 for toString() - empty array","FAILED");// Test #3if(empty1.equals(empty2) && a1.equals(a2)){outputStream.printf("%-80s%-10s ", "Test Set 10: Test 3 for equals()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 10: Test 3 for equals()","FAILED");// Test #4empty1.push(100);a1.add(1, 100);if(!empty1.equals(empty2) && !a1.equals(a2)){outputStream.printf("%-80s%-10s ", "Test Set 10: Test 4 for equals()","PASSED");count++;}elseoutputStream.printf("%-80s%-10s ", "Test Set 10: Test 4 for equals()","FAILED");if(count==expectedCount) return points;else return 0;}/*** Compares two arrays of integers* @param one first array to compare* @param two second array to compare* @return returns true if arrays are the same, false if not*/public static boolean equal(int[] one, int[] two){if(one.length!=two.length) return false;for(int i= 0; iif(one[i]!=two[i]) return false;}return true;}/*** Fills ArrayList with a set of consecutive values {0, 1, ..., k]* @param a dynamic array to fill* @param k the upper boundary of a value set*/public static void addValues(DynamicArray a, int k){for(int i = 0; i /*** Matches a pattern of partially filled array. needed for testing* @param one array to analyze* @param size the size of "filled" part* @param capacity the size of "empty" part* @return true if matches*/public static boolean matchPartiallyFilled(int[] one, int size, int capacity){boolean match = true;int i;for(i = 0; i if(i!=one[i]) match = false;}for(int j = i; j if(one[j]!=0) match = false;}return match;}}
Please help me pass these tests:
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