/** * Arturo Angeles * Homework 1 * 1/27/2013 */ public class arangele_hwk1 { //contains method public static boolean contains(int [] input, Check c) { //loop that goes into the array and checks for the possibilities for(int i = 0; i < input.length; i++) { boolean result = c.ok(input[i]); System.out.println(result); } return false; } static public interface Check { boolean ok( T item); //item T is ok (negative e.g.) } //Negative numbers method static public class Negative implements Check { //It will say if the number is negative or not. public boolean ok( Integer val) { //System.out.println(val); return val < 0; } } //Prime numbers method static public class Prime implements Check{ //boolean value will return whether the integer is prime or not public boolean ok(Integer val){ //System.out.println(val); if (val%2==0) return false; for(int i=3;i*i<=val;i+=2) { if(val%i==0) return false; } return true; } } //PerfectSquare method static public class PerfectSquare implements Check { //Boolean value will return if the number is not a perfect square public boolean ok(Integer val){ //System.out.println(val); double number = (double) val; return Math.sqrt(number)==0; } } public static void main(String[] args) { int [] input = {100,37,49}; boolean result1 = contains(input, new Prime()); //System.out.println(result1); boolean result2 = contains(input, new Negative()); //System.out.println(result2); boolean result3 = contains(input, new PerfectSquare()); //System.out.println(result3); } }