JAVA Generic versions of allOf() and anyOf() In this lab, you will write generic versions of...

80.2K

Verified Solution

Question

Programming

JAVA Generic versions of allOf() and anyOf()

In this lab, you will write generic versions of the allOf() andanyOf() methods you wrote in an earlier lab. Then you will takethose generic versions and create versions that work with apredicate 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 thiswork with arrays of any generic type. If you're stuck, I'veprovided an integer-only implementation of IntArrayUtils.find() anda generic version in ArrayUtils.find() with this lab. Look at thetransformations done to create the generic version of find();you'll need to make those same transformations to anyOf() andallOf(), using the versions above as starting points.

The tests V1 through V14 in the test program provided with thelab (TestArrayUtils.java) will test your code for Part 1. If yourcode passes all but tests V13 and V14, you've made a very commonerror that you need to correct.

It will be best if you ensure that your code passes the first 3graded tests before moving on. Even though you can't see the testcode, the test numbers from the zyBooks tests correspond with theones in TestArrayUtils.java.

NOTE: When working in Develop mode, you'll need to at leastcreate 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 ofallOf() and anyOf() that use a Predicate object. allOf() shouldreturn true if the predicate returns true forevery element of the array, and false otherwise.anyOf() should return true if the predicate returns true forany 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) shouldgive you some idea of the necessary changes needed to create yourpredicate versions of anyOf() and allOf().

The tests P1 through P12 in the test program provided with thelab (TestArrayUtils.java) will test your code for Part 2. Eventhough you can't see the test code, the test numbers from thezyBooks 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() gohere.
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 elementmatches.
   public static 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 allelements.
   public static int find(T[] a,Predicate 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 teststhat fails, you made a really common mistake!
       if (!ArrayUtils.anyOf(saDiff,StringUtils.concat(\"Sixty\", \"Two\"))) System.out.println(\"Test V13failed\");
       if (!ArrayUtils.allOf(saSame,StringUtils.concat(\"Forty\", \"Two\"))) System.out.println(\"Test V14failed\");
   }

   private static void testPredicates() {
      
       Integer[] ia = { 62, 63, 30, 63,29, 9, 57, 72, 33, 87 };

       Predicate gt0 =new GreaterThanN(0);
       Predicate gt50 = newGreaterThanN(50);
       Predicate 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 swDo =new StartsWith(\"Do\");
       Predicate swS = newStartsWith(\"S\");
       Predicate swX = newStartsWith(\"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{

   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{
  
   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 reallyneeded
// 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 elementmatches.
   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 comparesString
   // objects using == instead of .equals().
   public static String concat(String s1, String s2){
       return s1 + s2;
   }
}

Answer & Explanation Solved by verified expert
4.0 Ratings (434 Votes)
Note As the modifications in the code were done only in the ArrayUtilsjava I am including the code that exists in that file ArrayUtilsjava import javautilfunctionPredicate public class ArrayUtils Returns true if any element of a is equal to val and false otherwise public static boolean anyOfT a T val for int i 0 i alength i If the ai is of type String then we use    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students