Question: To complete the assignment, remove the calls to fail() in TestArray.java and follow along with comments to complete the test methods. You will not need

To complete the assignment, remove the calls to fail() in TestArray.java and follow along with comments to complete the test methods. You will not need to modify Array.java at all. When complete, upload your copy of TestArray.java.

Array.java

public class Array { private E[] array; /* * Constructs a new instance of Array with a specified array. * @param len the length of the new Array * @throws IllegalArgumentException if the specified length is negative */ public Array(int len) { if (len < 0) { throw new IllegalArgumentException(); } this.array = (E[])new Object[len]; } /* * Returns the element at the specified at the specified position in this array. * @param i index of the element to return * @return the element at the specified position in this array * @throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()) */ public E get(int i) { if (i < 0 || i >= this.array.length) { throw new IndexOutOfBoundsException(); } return this.array[i]; } /* * Returns the element at the specified at the specified position in this array. * @param i index of the element to replace * @param element element to be stored at the specified position * @throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()) */ public void set(int i, E element) { if (i < 0 || i >= this.array.length) { throw new IndexOutOfBoundsException(); } this.array[i] = element; } /* * Returns the number of elements in this array. * @return the number of elements in this array */ public int size() { return this.array.length; } }

TestArray.java

import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class TestArray { @Test void testConstructor() { // Base case: Call the constructor with a valid length. Ensure that the // constructed object is the desired length and that each element is // initially null. // // Edge case: Call the constructor with a length of zero and ensure it // has the desired length. // // Edge case: Check that the constructor throws the documented // exception when call with a negative length. fail("Not yet implemented"); } @Test void testSetThenGet() { // Set up: Construct an Array with a small number of elements. // // Base case: For each index in the array, set it to a value and then // that get returns that new value. You should also verify that it is // only the element at that index was changed. // // Edge case: Check that both get and set throw the documented // exception when passed an illegal index (both too large and // negative). fail("Not yet implemented"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!