/* * This is my own work: Yulin Chen * 1/26/2014 * A contains methos that include prime/perfectsuqare/negative */ class y_chen6_hwk1 { public static boolean contains(T[] input, Check chk) { //a variable that increment everytime when there is an item in array qualify int sign = 0; for (int i = 0; i < input.length; i++) { if (chk.qualify(input[i])) { sign++; } } //return true if there is at least one itme in array qualify if (sign > 0) { return true; } else { return false; } } //check method interface static public interface Check { //qualify method to see if the item qualify boolean qualify(T element); } //prime method static public class Prime implements Check { @Override public boolean qualify(Integer element) { //check to see if the element is prime or not for (int i = 2; i < element; i++) { if (element % i == 0) { return true; } } return false; } } //perfectsquare method static public class PerfectSquare implements Check { @Override public boolean qualify(Integer element) { //determine weather the element is a prefect square for (int i = 1; i < element; i++) { if (element / i == i && element % i == 0) { return true; } } return false; } } //negative method static public class Negative implements Check { @Override public boolean qualify(Integer element) { //see if the element is negative or not return element < 0; } } //static public class isNotInteger implements public static void main(String[] args) { Object[] 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(result1); System.out.println(result2); System.out.println(result3); } }