Write and test a CopyEvens.java program with a copyEvens() method that returns a
ID: 3930235 • Letter: W
Question
Write and test a CopyEvens.java program with a copyEvens() method that returns a new array containing just the even numbers in its input int array (if any):
------------------------------------------------------
copyEvens should have the following header:
public static int[] copyEvens(int[] a)
------------------------------------------------------
copyEvens should go through the array a passed into the method to determine how many even integers are in a, then create a new array of that size and copy the even ints in a into the new array and return it.
Hint: use a separate counter variable to fill up the new array as you’re going through the a array in a for loop.
------------------------------------------------------
In addition, write a helper method countEvens() with this header:
private static int countEvens(int[] a)
that counts the number of even ints in a and returns that count.
copyEvens should call the countEvens() method to determine the size of its returned array.
Explanation / Answer
Please find the required program along with its output. Please see the comments against each line to understand the step.
---------------------------------------------------
OUTPUT:
Evens array: [2, 6, 2, 8]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.