Question
PART 2: Completing IntArrays.java --------------------------------- Before working the tasks described below, please look at the following code (in IntArrays#main(String[])) that demonstrates how to (i) construct
PART 2: Completing IntArrays.java --------------------------------- Before working the tasks described below, please look at the following code (in IntArrays#main(String[])) that demonstrates how to (i) construct an array, (ii) find the length of an array, and (iii) access members (i.e., elements) of an array: int[] a = new int[5]; for (int i = 0; i < a.length; i++) a[i] = i; The code above creates an array containing 5 integers, namely 0, 1, 2, 3, and 4. Therefore, the following code outputs "[0, 1, 2, 3, 4]": System.out.println(Arrays.toString(a)); Task 1 (60 points). Complete the "fill(int[] a, int val)" method (using a loop) so that it fills the specified array (argument "a") with the specified value (argument "val"). For example, "fill(b, -1)" needs to set all of the members of "b" to -1. When you finish this task, the following code in IntArrays#main(String[]) will output "[0, 0, 0, 0]" and then "[-1, -1, -1, -1]": int[] b = new int[4]; System.out.println(Arrays.toString(b)); fill(b, -1); System.out.println(Arrays.toString(b)); Your code will also pass the unit test named "test1()" in "UnitTests.java". Task 2 (30 points). Implement the "copyOf(int[] original)" method so that it returns a copy of the specified array (argument "original"). What this method returns must be a new array such that (i) its length is the same as the length of the specified array (argument "original") and (ii) its member at each index is the same as the member at the same index in "original". Consider an array "a" consisting of integers 0, 1, 2, 3, 4. Then, the following code must output "[0, 1, 2, 3, 4]" (since "b" is a copy of "a"): b = copyOf(a); System.out.println(Arrays.toString(b)); Furthermore, consider the following code: fill(b, -1); System.out.println(Arrays.toString(b)); System.out.println(Arrays.toString(a)); Here, "System.out.println(Arrays.toString(b))" must output "[-1, -1, -1, -1, -1]" since all members of "b" are set to -1. On the other hand, "System.out.println(Arrays.toString(a))" will output "[0, 1, 2, 3, 4]" since "a" and "b" are different arrays and "a" has not be modified at all. When you complete this task, your code will also pass the unit test named "test2()" in "UnitTests.java". Task 3 (10 points). Complete the "subarray(int[] original, int startIndex, int endIndex)" method so that it returns a new array that corresponds to a sub-array of the specified array (argument "original"). This sub-array begins at the specified "beginIndex" and extends to the element at index ("code endIndex" - 1) and thus the length of the sub-array is ("endIndex"-"beginIndex"). For example, consider an array "a" consisting of integers 0, 1, 2, 3, 4. "subarray(a, 0, a.length)" must return a sub-array containing the elements at indices 0-4 in "a" (i.e., an array [0, 1, 2, 3, 4]). "subarray(a, 1, a.length-1)" must return a sub-array containing the elements at indices 1-3 in "a" (i.e., an array [1, 2, 3]). When you finish this task, the following code will output "[0, 1, 2, 3, 4]" (i.e., the elements at indices 0-4 in "a"), "[1, 2, 3, 4]" (i.e., the elements at indices 1-4 in "a"), and "[1, 2, 3]" (i.e., the elements at indices 1-3 in "a"): System.out.println(Arrays.toString(subarray(a, 0, a.length))); System.out.println(Arrays.toString(subarray(a, 1, a.length))); System.out.println(Arrays.toString(subarray(a, 1, a.length - 1))); Your code will also pass the unit test named "test3()" in "UnitTests.java".
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