/* * I am Thomas Waller and this is my own work * 01/27/2014 * Homework 1 */ class TWWALLER_hwk1 { public static void main(String[] args) { Integer [] input = {100, 37, 49}; boolean result1 = contains(input, new Prime()); boolean result2 = contains(input, new Sqroot()); boolean result3 = contains(input, new Negative()); } /** * @param * @param input * @param c * @return */ public static boolean contains(T[] input, Check c) { //runs through the array for (int i = 0; i < input.length; i++) { if (c.ok(input[i])) { return true; } } return false; } /** * Checks array for prime values */ public static class Prime implements Check { @Override public boolean ok(Integer val) { for (int i = 2; i < val; i++) { if (val % i == 0) { return false; } } return true; } } /** * Checks array for negative values */ public static class Negative implements Check { @Override public boolean ok(Integer val) { if (val < 0) { return true; } return false; } } /** * Checks array for perfect square root values */ public static class Sqroot implements Check { @Override public boolean ok(Integer val) { double sqrt = Math.sqrt(val); if (sqrt * sqrt == val) { return true; } return false; } } /** * * * @param */ static public interface Check { boolean ok(T item); //item T is ok (negative e.g.) } }