Question: [Java] CODING ARRAY ASSIGNMENT HELP FIRST PLEASE MAKE SURE THAT THE CODE WORKS WITH THE TESTER CODE BELOW OR I WILL DOWNVOTE We continue writing

[Java] CODING ARRAY ASSIGNMENT HELP

FIRST PLEASE MAKE SURE THAT THE CODE WORKS WITH THE TESTER CODE BELOW OR I WILL DOWNVOTE

We continue writing code for arrays. First, lets define some useful terminology to use with this weeks problems. Based on how the value in some array position i compares to the value in its successor position i+ 1, each position i in the array a is classified as an upstep if a[i] < a[i + 1], a downstep if a[i] > a[i + 1], and aplateau if a[i] == a[i + 1]. The last position of the array has no such classification, since there is no successor element that we could compare its value with.

Armed with these definitions, create a class ArrayShapeProblems and write the following methods in it, after which you should again use the JUnit test class TestArrayShapeProblems.java to try them out.

Start the code with boolean isSawtooth(int[] a) that checks whether the parameter array a has a sawtooth shape, meaning that it has no plateaus, and that each upstep is immediately followed by a downstep and vice versa. (Hint: it is much easier to determine whether the given array is not sawtooth, and then negate this answer.)

MAKE SURE IT WORKS WITH TESTER CODE OR I WILL DOWNVOTE

import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.util.*; public class TestArrayShapeProblems { private static final int SEED = 12345; private static final int RUNS = 300; private ArrayShapeProblems map = new ArrayShapeProblems(); 
 @Test public void testIsSawtooth() { Random rng = new Random(SEED); for(int i = 1; i < RUNS; i++) { int[] a = new int[i]; int tmp; for(int j = 0; j < RUNS; j++) { a[0] = rng.nextInt(100000) - 50000; int sign = rng.nextBoolean() ? +1 : -1; for(int l = 1; l < a.length; l++) { a[l] = a[l-1] + sign * (rng.nextInt(10) + 1); sign = -sign; } assertTrue(map.isSawtooth(a)); } for(int l = 1; l < a.length-1; l++) { tmp = a[l]; a[l] = a[l-1]; assertTrue(!map.isSawtooth(a)); a[l] = tmp; tmp = a[l]; a[l] = (a[l-1]+a[l+1])/2; assertTrue(!map.isSawtooth(a)); a[l] = tmp; } } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!