Question: in Java: Write a new method for the ArrayIntList class named add that accepts an integer index and a value to add as parameters and

in Java:

Write a new method for the ArrayIntList class named add that accepts an integer index and a value to add as parameters and inserts the given value at the given index, shifting subsequent values to the right. For example, if an ArrayIntList variable named list stores the following values:

[3, 19, 42, 7, -3, 4] 

Then the call of list.add(2, 17); should modify the list to store the following sequence of integers after the call:

[3, 19, 17, 42, 7, -3, 4] 

It is your responsibility to make sure that the list's internal array has enough capacity to accommodate the newly added element. You should throw an ArrayIndexOutOfBoundsException if the index is out of range; that is, if it is not between 0 and the list's size, inclusive. (If the list's size is passed as the index, you should add to the end of the list.)

Assume you are adding to the ArrayIntList class with following members:

public class ArrayIntList {
 private int[] elementData;
 private int size;
 
 // construct a new empty list of capacity 10
 public ArrayIntList() { ... }
 
 // add to end of list
 public void add(int value) { ... }
 
 // throw ArrayIndexOutOfBoundsException if index not between min/max inclusive
 private void checkIndex(int index, int min, int max) { ... }
 
 // ensure that elementData.length >= capacity
 private void ensureCapacity(int capacity) { ... }
 ...
 
 // // your code goes here
}

. . .

1

 

2

 

3

 

4

 

5

 

6

 

7

 

8

 

9

 

10

 

Partial class: Write code that will become part of an existing class as described. You do not need to write the complete class, just the portion described in the exercise.

Submit

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!