Question: JAVA ARRAY ASSIGNMENT HELP PLEASE This week, it is time to practice arrays so that our methods can process millions of items of data, giving
JAVA ARRAY ASSIGNMENT HELP PLEASE
This week, it is time to practice arrays so that our methods can process millions of items of data, giving our loops and conditions something useful to do. Create a class ArrayProblems and write the following array methods in it. Again, you must use the JUnit test class TestArrayProblems.java to test out your methods.
You have to start the code with "int[] everyOther(int[] a)" that creates and returns an array that contains the elements from the even-numbered positions of the array a, that is, the elements a[0], a[2], a[4],... Make sure to compute the length of the result array exactly right so that there is no extra element in the end. (You need to consider odd and even lengths of a separately. The remainder operator % might come handy here.)
the tester code is =
import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.*; public class TestArrayProblems { private static final int SEED = 12345; private static final int RUNS = 100; private static final int SIZE = 100; private ArrayProblems ap = new ArrayProblems(); @Test public void testEveryOther() { java.util.Random rng = new java.util.Random(SEED); int[] a = new int[RUNS]; for(int i = 0; i < RUNS; i++) { a[i] = rng.nextInt(100000) - 50000; } for(int i = 0; i < RUNS; i++) { int[] b = new int[i]; System.arraycopy(a, 0, b, 0, i); int[] c = ap.everyOther(b); System.arraycopy(a, 0, b, 0, i); assertEquals(c.length, b.length / 2 + b.length % 2); for(int j = 0; j < c.length; j++) { assertEquals(c[j], b[j*2]); } } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
