In: Computer Science
JAVA Generic versions of allOf() and anyOf()
In this lab, you will write generic versions of the allOf() and anyOf() methods you wrote in an earlier lab. Then you will take those generic versions and create versions that work with a predicate rather than direct comparison of values.
Part 1 - Create generic versions of allOf() and anyOf()
Recall that the integer-only versions of allOf() and anyOf() looked like this:
// Returns true if any element of 'a' is equal to 'val' // and false otherwise. public static boolean anyOf(int[] a, int val) { for (int i = 0; i < a.length; i++) if (a[i] == val) return true; return false; } // Returns true if every element of 'a' is equal to 'val' // and false otherwise. public static boolean allOf(int[] a, int val) { for (int i = 0; i < a.length; i++) if (a[i] != val) return false; return true; }
You will need to make the necessary transformations to make this work with arrays of any generic type. If you're stuck, I've provided an integer-only implementation of IntArrayUtils.find() and a generic version in ArrayUtils.find() with this lab. Look at the transformations done to create the generic version of find(); you'll need to make those same transformations to anyOf() and allOf(), using the versions above as starting points.
The tests V1 through V14 in the test program provided with the lab (TestArrayUtils.java) will test your code for Part 1. If your code passes all but tests V13 and V14, you've made a very common error that you need to correct.
It will be best if you ensure that your code passes the first 3 graded tests before moving on. Even though you can't see the test code, the test numbers from the zyBooks tests correspond with the ones in TestArrayUtils.java.
NOTE: When working in Develop mode, you'll need to at least create dummy stubs for the methods in Part 2.
Part 2 - Predicate versions of allOf() and anyOf()
For the second part of the assignment, you'll create versions of allOf() and anyOf() that use a Predicate object. allOf() should return true if the predicate returns true for every element of the array, and false otherwise. anyOf() should return true if the predicate returns true for any element of the array, and false otherwise.
If you're stuck, check the implementations of ArrayUtils.find() provided with the lab. The differences between those two versions (one that checks for equality and one that uses a predicate) should give you some idea of the necessary changes needed to create your predicate versions of anyOf() and allOf().
The tests P1 through P12 in the test program provided with the lab (TestArrayUtils.java) will test your code for Part 2. Even though you can't see the test code, the test numbers from the zyBooks tests correspond with the ones in TestArrayUtils.java.
ArrayUtils.java
import java.util.function.Predicate;
public class ArrayUtils {
// Your new versions of anyOf() and allOf() go
here.
CODE IS ONLY NEEDED HERE
CODE IS ONLY NEEDED HERE
CODE IS ONLY NEEDED HERE
CODE IS ONLY NEEDED HERE
// The versions of the find() algorithm provided below
// should help you determine the changes you need to make
// to your code to create the new versions of anyOf()
// and all().
// Returns the index of the first element of 'a'
that
// is equal to 'val'. Returns -1 if no element
matches.
public static <T> int find(T[] a, T val) {
for (int i = 0; i < a.length;
i++)
if
(a[i].equals(val))
return i;
return -1;
}
// Returns the index of the first element of 'a'
for
// which the given predicate returns true.
Returns
// -1 if the predicate is false for all
elements.
public static <T> int find(T[] a,
Predicate<T> pred) {
for (int i = 0; i < a.length;
i++)
if
(pred.test(a[i]))
return i;
return -1;
}
}
TestArrayUtils.java
import java.util.function.Predicate;
public class TestArrayUtils {
private static void testValues() {
Integer[] iaDiff = { 62, 63, 30,
63, 29, 9, 57, 72, 33, 87 };
Integer[] iaSame = { 42, 42, 42
};
if (!ArrayUtils.anyOf(iaDiff, 62))
System.out.println("Test V1 failed");
if (!ArrayUtils.anyOf(iaDiff, 87))
System.out.println("Test V2 failed");
if ( ArrayUtils.anyOf(iaDiff, 42))
System.out.println("Test V3 failed");
if (!ArrayUtils.allOf(iaSame, 42))
System.out.println("Test V4 failed");
if ( ArrayUtils.allOf(iaSame, 24))
System.out.println("Test V5 failed");
if ( ArrayUtils.allOf(iaDiff, 30))
System.out.println("Test V6 failed");
String[] saDiff = { "SixtyTwo",
"Thirty", "TwentyNine", "FiftySeven", "ThirtyThree" };
String[] saSame = { "FortyTwo",
"FortyTwo", "FortyTwo" };
if (!ArrayUtils.anyOf(saDiff,
"SixtyTwo")) System.out.println("Test V7 failed");
if (!ArrayUtils.anyOf(saDiff,
"ThirtyThree")) System.out.println("Test V8 failed");
if ( ArrayUtils.anyOf(saDiff,
"FortyTwo")) System.out.println("Test V9 failed");
if (!ArrayUtils.allOf(saSame,
"FortyTwo")) System.out.println("Test V10 failed");
if ( ArrayUtils.allOf(saSame,
"TwentyFour")) System.out.println("Test V11 failed");
if ( ArrayUtils.allOf(saDiff,
"Thirty")) System.out.println("Test V12 failed");
// If these are the only tests
that fails, you made a really common mistake!
if (!ArrayUtils.anyOf(saDiff,
StringUtils.concat("Sixty", "Two"))) System.out.println("Test V13
failed");
if (!ArrayUtils.allOf(saSame,
StringUtils.concat("Forty", "Two"))) System.out.println("Test V14
failed");
}
private static void testPredicates() {
Integer[] ia = { 62, 63, 30, 63,
29, 9, 57, 72, 33, 87 };
Predicate<Integer> gt0 =
new GreaterThanN(0);
Predicate<Integer> gt50 = new
GreaterThanN(50);
Predicate<Integer> gt100 =
new GreaterThanN(100);
if (!ArrayUtils.anyOf(ia, gt0 ))
System.out.println("Test P1 failed");
if (!ArrayUtils.anyOf(ia, gt50))
System.out.println("Test P2 failed");
if ( ArrayUtils.anyOf(ia, gt100))
System.out.println("Test P3 failed");
if (!ArrayUtils.allOf(ia, gt0 ))
System.out.println("Test P4 failed");
if ( ArrayUtils.allOf(ia, gt50))
System.out.println("Test P5 failed");
if ( ArrayUtils.allOf(ia, gt100))
System.out.println("Test P6 failed");
String[] dw = { "Bashful", "Doc",
"Dopey", "Grumpy", "Happy", "Sleepy", "Sneezy" };
String[] nm = { "Don", "Donna",
"Dolly", "Dolores", "Dominic", "Doria" };
Predicate<String> swDo =
new StartsWith("Do");
Predicate<String> swS = new
StartsWith("S");
Predicate<String> swX = new
StartsWith("X");
if (!ArrayUtils.anyOf(dw, swDo))
System.out.println("Test P7 failed");
if (!ArrayUtils.anyOf(dw, swS))
System.out.println("Test P8 failed");
if ( ArrayUtils.anyOf(dw, swX))
System.out.println("Test P9 failed");
if (!ArrayUtils.allOf(nm, swDo))
System.out.println("Test P10 failed");
if ( ArrayUtils.allOf(nm, swS))
System.out.println("Test P11 failed");
if ( ArrayUtils.allOf(nm, swX))
System.out.println("Test P12 failed");
}
public static void main(String[] args) {
testValues();
testPredicates();
}
}
GreaterThanN.Java
import java.util.function.Predicate;
public class GreaterThanN implements Predicate<Integer> {
private int n;
public GreaterThanN(int n) {
this.n = n;
}
@Override
public boolean test(Integer val) {
return val > n;
}
}
StartsWith.java
import java.util.function.Predicate;
public class StartsWith implements Predicate<String>
{
public String s;
public StartsWith(String s) {
this.s = s;
}
@Override
public boolean test(String t) {
return t.startsWith(s);
}
}
IntArrayUtils.java
// This is provided for reference only. It isn't really
needed
// for the assignment itself.
public class IntArrayUtils {
// Returns the index of the first element of 'a'
that
// is equal to 'val'. Returns -1 if no element
matches.
public static int find(int[] a, int val) {
for (int i = 0; i < a.length;
i++)
if (a[i] ==
val)
return i;
return -1;
}
// Returns true if any element of 'a' is equal to
'val'
// and false otherwise.
public static boolean anyOf(int[] a, int val) {
for (int i = 0; i < a.length;
i++)
if (a[i] ==
val)
return true;
return false;
}
// Returns true if every element of 'a' is equal to
'val'
// and false otherwise.
public static boolean allOf(int[] a, int val) {
for (int i = 0; i < a.length;
i++)
if (a[i] !=
val)
return false;
return true;
}
}
StringUtils.java
public class StringUtils {
// Concatentate two String objects.
// This is a sneaky of breaking code that compares
String
// objects using == instead of .equals().
public static String concat(String s1, String s2)
{
return s1 + s2;
}
}
Note:
As the modifications in the code were done only in the ArrayUtils.java, I am including the code that exists in that file.
ArrayUtils.java:
import java.util.function.Predicate;
public class ArrayUtils
{
// Returns true if any element of 'a' is equal to 'val'
// and false otherwise.
public static <T> boolean anyOf(T[] a, T val)
{
for (int i = 0; i < a.length; i++)
{
// If the a[i] is of type String, then we use equals method, else we use the equals to operator
if (a[i] instanceof String)
{
if (a[i].equals(val))
{
return true;
}
}
else
{
if (a[i] == val)
{
return true;
}
}
}
return false;
}
public static <T> boolean anyOf(T[] a, Predicate<T> pred)
{
for (int i = 0; i < a.length; i++)
{
if (pred.test(a[i]))
{
return true;
}
}
return false;
}
// Returns true if every element of 'a' is equal to 'val'
// and false otherwise.
public static <T> boolean allOf(T[] a, T val)
{
for (int i = 0; i < a.length; i++)
{
// If the a[i] is of type String, then we use equals method, else we use the not equals to operator
if (a[i] instanceof String)
{
if (!(a[i].equals(val)))
{
return false;
}
}
else
{
if (a[i] != val)
{
return false;
}
}
}
return true;
}
public static <T> boolean allOf(T[] a, Predicate<T> pred)
{
for (int i = 0; i < a.length; i++)
{
if (!(pred.test(a[i])))
{
return false;
}
}
return true;
}
// The versions of the find() algorithm provided below
// should help you determine the changes you need to make
// to your code to create the new versions of anyOf()
// and all().
// Returns the index of the first element of 'a' that
// is equal to 'val'. Returns -1 if no element matches.
public static <T> int find(T[] a, T val)
{
for (int i = 0; i < a.length; i++)
if (a[i].equals(val))
return i;
return -1;
}
// Returns the index of the first element of 'a' for
// which the given predicate returns true. Returns
// -1 if the predicate is false for all elements.
public static <T> int find(T[] a, Predicate<T> pred)
{
for (int i = 0; i < a.length; i++)
if (pred.test(a[i]))
return i;
return -1;
}
}
There is no output attached because after compiling and running all the java files, no output is printed onto the screen and if anything is printed on the screen, it is because one or more of the tests failed.