/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * Jesse Thomas 27 January 2014 CSC330 * * I have abided by the UNCG Academic Integrity Policy */ public class jathoma3_hwk1 { /** * main method that tests contains method and classes. * INPUT: args * OUTPUT: void */ public static void main(String[] args) { Integer [] test = {72, -5, 17, 100}; boolean result1 = contains(test, new PerfectSquare()); boolean result2 = contains(test, new Prime()); boolean result3 = contains(test, new Negative()); System.out.println(result1 + " " + result2 + " " + result3); } /** * Method that uses Check interface and some array and returns a * boolean result. * @param * @param input * @param chk * @return */ static boolean contains(T[] input, Check chk) { boolean result = false; for(int i = 0;!result && i< input.length; i++){ result = chk.check(input[i]); } return result; } /** * class that tests if a number is negative. */ static public class Negative implements Check { @Override public boolean check(Integer input) { return input < 0; } } /** * Prime class that tests if a number is a prime number. */ static public class Prime implements Check { @Override public boolean check(Integer input) { if (input < 2) { return false; } else if (input == 2) { return true; } else if (input % 2 == 0) { return false; } for (int i = 3; i * i <= input; i += 2) { if (input % i == 0) { return false; } } return true; } } /** *Perfect Square class that tests to see if a number is a perfect * square. */ static public class PerfectSquare implements Check { @Override public boolean check(Integer input) { for (int i = 0; i <= input; i++) { if (i * i == input) { return true; } } return false; } } /** * an interface that has the method Check to be implemented in other classes. * @param */ static public interface Check { public boolean check(T input); } }