In Java, a function called allOf to StdIntArray which returns true if all the ar
ID: 3745849 • Letter: I
Question
In Java,
a function called allOf to StdIntArray which returns true if all the array or sub-array elements match the given value, and false otherwise.There should be two versions, one that works with the entire array, and one that works with sub-array defined by a half-open range. The signatures should be
public static boolean allOf(int a[], int value)
public static boolean allOf(int a[], int lo, int hi, int value)
The version that works with the entire array should call the sub-array version.
Explanation / Answer
public static boolean allOf(int a[], int value) { return allOf(a, 0, a.length, value); } public static boolean allOf(int a[], int lo, int hi, int value) { if (lo >= hi) { return true; } else if (a[lo] != value) { return false; } else { return allOf(a, lo+1, hi, value); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.