[Java] CODING ARRAY ASSIGNMENT HELP FIRST PLEASE MAKE SURE THAT THE CODE WORKS W
ID: 3825407 • Letter: #
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 code for arrays. First, let’s define some useful terminology to use with this week’s 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 a plateau 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
Explanation / Answer
//TestArrayShapeProblems.java
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 testCountUpsteps() {
Random rng = new Random(SEED);
for(int i = 1; i < RUNS; i++) {
int[] a = new int[i];
int[] b = new int[i];
for(int j = 0; j < RUNS; j++) {
int plateaus = 0;
for(int l = 1; l < a.length; l++) {
a[l] = a[l-1] + rng.nextInt(100) - 50;
b[l] = -a[l];
if(a[l] == a[l-1]) { ++plateaus; }
}
assertTrue(map.countUpsteps(a) + map.countUpsteps(b) + plateaus == i-1);
}
}
}
private static int[] signs = { -1, 0, 1 };
@Test
public void testSameStepShape() {
Random rng = new Random(SEED);
for(int i = 1; i < RUNS; i++) {
int[] a = new int[i];
int[] b = new int[i];
for(int j = 0; j < RUNS; j++) {
for(int l = 1; l < a.length; l++) {
int sign = signs[rng.nextInt(3)];
a[l] = a[l-1] + sign*(rng.nextInt(20)+10);
b[l] = b[l-1] + sign*(rng.nextInt(20)+10);
}
assertTrue(map.sameStepShape(a,b));
for(int l = 1; l < b.length - 1; l++) {
if(b[l] != b[l-1]) {
int tmp = b[l];
b[l] = b[l-1];
b[l-1] = tmp;
assertTrue(!map.sameStepShape(a,b));
tmp = b[l];
b[l] = b[l-1];
b[l-1] = tmp;
}
}
}
}
}
@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;
}
}
}
@Test
public void testIsMountain() {
Random rng = new Random(SEED);
for(int i = 5; i < RUNS; i++) {
int[] a = new int[i];
for(int j = 0; j < RUNS; j++) {
a[0] = rng.nextInt(100000) - 50000;
int k = rng.nextInt(i);
for(int l = 1; l <= k; l++) {
a[l] = a[l-1] + rng.nextInt(10) + 1;
}
for(int l = k + 1; l < a.length; l++) {
a[l] = a[l-1] - rng.nextInt(10) - 1;
}
assertTrue(map.isMountain(a));
for(int l = 0; l < i - 1; l++) {
if(l < k - 1) {
a[l] += 20;
assertFalse(map.isMountain(a));
a[l] -= 40;
assertFalse(l > 0 && map.isMountain(a));
a[l] += 20;
}
else if (l == k) {
a[l] -= 40;
assertFalse(l > 0 && l < a.length - 1 && map.isMountain(a));
a[l] += 40;
}
else if (l > k + 1) {
a[l] += 20;
assertFalse(map.isMountain(a));
a[l] -= 40;
assertFalse(l < a.length - 1 && map.isMountain(a));
a[l] += 20;
}
}
}
}
}
}
=========================================================================
//ArrayShapeProblems
public class ArrayShapeProblems
{
public int countUpsteps(int[] a) {
int result = 0;
for (int x = 0; x < a.length-1; x += 1) {
if (a[x] < a[x+1]) {
result += 1;
}
}
return result;
}
public int countDownsteps(int[] a) {
int result = 0;
for (int x = 0; x < a.length-1; x += 1) {
if (a[x] > a[x+1]) {
result += 1;
}
}
return result;
}
public int countPlateaus(int[] a) {
int result = 0;
for (int x = 0; x < a.length-1; x += 1) {
if (a[x] == a[x+1]) {
result += 1;
}
}
return result;
}
public int isUpStep(int a, int b) {
if ( a < b) {
return 1;
} else {
return 0;
}
}
public int isDownStep(int a, int b) {
if ( a > b) {
return 1;
} else {
return 0;
}
}
public int isPlateau(int a, int b) {
if ( a == b) {
return 1;
} else {
return 0;
}
}
public boolean sameStepShape(int[] a, int[] b) {
boolean result = true;
for (int x = 1; x < a.length; x += 1) {
if (((a[x-1] > a[x]) && (b[x-1] <= b[x]))) {
result = false;
break;
} else if (((a[x-1] < a[x]) && (b[x-1] >= b[x]))) {
result = false;
break;
} else if (((a[x-1] == a[x]) && (b[x-1] != b[x]))) {
result = false;
break;
}
}
return result;
}
public boolean isSawtooth(int[] a) {
boolean result = true;
for (int x = 2; x < a.length; x += 1) {
if (a[x-2] > a[x-1]) {
if (a[x-1] >= a[x]) {
result = false;
break;
}
} else if (a[x-2] < a[x-1]) {
if (a[x-1] <= a[x]) {
result = false;
break;
}
} else {
result = false;
break;
}
}
return result;
}
public boolean isMountain(int[] a) {
boolean result = true;
for (int x = 2; x < a.length; x += 1) {
if (a[x-2] > a[x-1]) {
if (a[x-1] <= a[x]) {
result = false;
break;
}
}
}
return result;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.