Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Recommended Textbook for

Select Healthcare Classification Systems And Databases

Authors: Katherine S. Rowell, Ann Cutrell

1st Edition

0615909760, 978-0615909769

More Books

Students also viewed these Databases questions

Question

1. How did you go about making your selection?

Answered: 1 week ago