QuestionQuestion

Transcribed TextTranscribed Text

Activity In this assignment, you will modify the MyArrayList class to give it some extra functionality. Step 1. Use Java Generics Modify the MyArrayList class so that it uses Java Generics instead of Strings. That is, it should be possible to create an instance of the class like this: MyArrayList<Integer> list = new MyArrayList<>(); Note that you will need to make changes throughout the class definition. Make sure that all methods accept or return objects of the parameterized type, and not just Strings. Step 2. Implement remove method Implement the remove method so that it takes an object of the parameterized type (from Step 1) as its parameter, and removes the first instance of an equivalent element in the underlying array, i.e. for which the .equals method returns true. Once the object has been removed, the elements following it should be shifted over to fill the empty space, and the method should return true; if the object does not exist in the underlying array, the method should simply return false. For instance, assume the underlying array contained these values in this order: “Cat” “Dog” “Banana” “Aardvark” null null If the method remove(“Banana”) were called, then the array should look like this: “Cat” “Dog” “Aardvark” null null null And the method should return true. Step 3. Shrink array when it is too large Modify the two remove methods (the one we provided and the one from Step 2) so that they shrink the length of the underlying array when, after removing an element, the number of elements in the array is less than or equal to 25% of the array’s length. In this case, the underlying array should be shrunk to half its current length. For instance, assume the underlying array contains these values in this order: “Ant” “Bat” “Cow” null null null null null And assume that “Bat” was removed, either by its index or its value. Then the array should look like this: “Ant” “Cow” null null null null null null Because the number of elements (2) is now less than or equal to 25% of the length of the array (8), the array should be cut in half, and should then look like this: “Ant” “Cow” null null That is, the elements should stay the same, but the underlying array should now have a length of 4 instead of 8. Hint: Review the code that is used to grow the underlying array and think about how you can write similar code to shrink it. Step 4. Implement set(index, value) method Implement the set method so that it takes an integer index and a value of the parameterized type (from Step 1) as its parameters and replaces the element in the underlying array at that index with the new value. The method should then return the old value, i.e. the one that was replaced. For instance, assume the underlying array contained these values in this order: “Cat” “Dog” “Banana” “Aardvark” null null If the method set(1, “Monkey”) were called, then the array should look like this: “Cat” “Monkey” “Banana” “Aardvark” null null And the method should return “Dog” since that is what was replaced. If the index is out of range, then the method should throw an IndexOutOfBoundsException. Step 5. Create constructor to initialize underlying array Create a constructor for this class that takes an array of the parameterized type (from Step 1) as its parameter and initializes the values in the MyArrayList’s underlying array. If the argument to the constructor is null, then the MyArrayList constructor should behave as the default (no-argument) constructor does. Initializing the MyArrayList elements sounds relatively straightforward, but note that it should be possible for the caller of this constructor to modify the elements of the input array argument without changing what’s in the MyArrayList and vice-versa. For instance, consider the following code: String[] data = {“cat”, “dog”, “elephant”}; MyArrayList<String> list = new MyArrayList<>(data); If the caller then invokes data[1] = “bat”; this should not change what’s in list. Likewise, if the caller invokes list.set(0, “mouse”); this should not change what’s in data .

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class MyArrayList<E> {

/*
* Do not change this initial capacity; it is used by our test cases
*/
private static final int INITIAL_CAPACITY = 4;

/*
* These are protected so that test cases can access them.
* Please do not change the visibility of these fields!
*/
protected Object[] data;
protected int size = 0;

public MyArrayList() {
data = new Object[INITIAL_CAPACITY];
}

/*
* Need to implement this in step 5
*/
public MyArrayList(Object[] arr) {
if( arr == null)
data = new Object[INITIAL_CAPACITY];
else {
data = new Object[arr.length];
for(int i = 0 ; i < arr.length ; i++)
data[i] = arr[i];
size = arr.length;
}
}

public Object get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$54.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats