/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package hwk1; /** * * @author Joseph Rowan */ class Hwk1 { public static boolean contains(T[] input, Check c){ for(int i = 0;i < input.length; i++){ if(c.ok(input[i])) return true; }return false; } static public interface Check{ boolean ok(T item); //item T is ok (negative e.g.) } static public class Prime implements Check{ public boolean ok(Integer val){ for(int i = 2;i < val/2;i++){ if(val%i == 0) return true; }return false; } } static public class PerfectSquare implements Check{ public boolean ok(Integer val){ int root = (int) Math.sqrt(val); return root*root == val; } } static public class Negative implements Check{ public boolean ok(Integer val){ return val < 0; } } /** * @param args the command line arguments */ public static void main(String[] args) { Integer[] input = {100, 37, 49}; boolean result1 = contains(input, new Prime()); boolean result2 = contains(input, new PerfectSquare()); boolean result3 = contains(input, new Negative()); System.out.println("Array contains prime number: "+result1); System.out.println("Array contains perfect square: "+result2); System.out.println("Array contains negative number: "+result3); } }