Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In java, These are the tasks: And these are the part of code for each tasks: task 1: task 2: task 3: Other parts of
In java, These are the tasks:
And these are the part of code for each tasks:
task 1:
task 2:
task 3:
Other parts of the code:
Thank you!
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)); 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. Task 3 (10 points). Complete the "subarray(int[] original, int startIndex, int endlndex)" 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 endlndex" - 1) and thus the length of the sub-array is ("endlndex"-"beginlndex"). 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.printIn(Arrays.toString(subarray(a, 1, a.length))); System.out.printIn(Arrays.toString(subarray(a, 1, a.length - 1))); public static void fill(int[] a, int val) \{ \} public static int[] copyof(int[] original) \{ return null; \} public static int[] subarray(int[] original, int startIndex, int endIndex) \{ return nullStep 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