import java.math.BigInteger; import java.lang.*; /** *Method contains takes an array of arbitrary type and returns true if there exists any item * in the array that satisfies a condition. * @author Swarna */ public class s_bonam_hwk1 { public static boolean contains(T[] inputArray, Check C) { boolean X = false; for(T input : inputArray) { if(!X) { X= C.isSatisfied(input.intValue()); } } return X; } public static void main(String[] arg) { System.out.println("Test for BigInteger"); s_bonam_hwk1.TestBigInteger(); System.out.println("Test for Double"); s_bonam_hwk1.TestDouble(); } /*** * Test case for BigInteger */ public static void TestBigInteger() { Integer [] input = new Integer[3]; input[0]=new Integer(3); input[1]=new Integer(-16); input[2]=new Integer(25); boolean result1=contains(input,new Prime()); boolean result2=contains(input,new PerfectSquare()); boolean result3=contains(input,new Negative()); if(result1)System.out.println("The input array has a prime number"); if(result2)System.out.println("The input array has a perfect square"); if(result3)System.out.println("The input array has a negative number"); } /*** * Test case for Double */ public static void TestDouble() { Double [] input = new Double[3]; input[0]=new Double(-3.4); input[1]=new Double(16.5); input[2]=new Double(5.2); boolean result1=contains(input,new Prime()); boolean result2=contains(input,new PerfectSquare()); boolean result3=contains(input,new Negative()); if(result1)System.out.println("The input array has a prime number"); if(result2)System.out.println("The input array has a perfect square"); if(result3)System.out.println("The input array has a negative number"); } } /** *Interface Check allows us to identify Perfect Square, Negative number, and Prime number */ interface Check { public boolean isSatisfied(int a); } /** *class Negative checks if the given input is less than 0 */ class Negative implements Check { public boolean isSatisfied(int a) { return a<0; } } /** * class Prime checks if the given input is a Prime number */ class Prime implements Check { public boolean isSatisfied(int a) { if(a<=1) return false; boolean isPrime=true; for(int i=2;i<=Math.sqrt(a)&&isPrime;i++) if((a%i)==0)isPrime=false; return isPrime; } } /** * class PerfectSquare checks if the given input is a Perfect Square */ class PerfectSquare implements Check { public boolean isSatisfied(int a) { if(a<0) return false; boolean isperfectSqr=false; if(Math.round(Math.sqrt(a))==Math.sqrt(a)) isperfectSqr=true; return isperfectSqr; } }