/** * Demonstrates the use of basic functional objects, or functors, in a program. * This is my own work. * @date 01-26-2014 * @author Adam Pyle * */ public class hwk1 { /** * Check the given array numbers of the quality specified * @param the datatype this method should expect * @param InputArr the array containing the numbers to be checked * @param type the class representing the specific type of number to check for * @return true if at least one number of the specified type is present, false otherwise */ public static boolean check(T[] InputArr, conditional type) { for(int i=0;i the expected type of the data */ static public interface conditional { /** * Checks the given data and determines whether it is an example of this type of number * @param input the data to be checked * @return true if @input meets the conditions for this type of number, false otherwise */ public boolean Check(T input); } /** * represents a negative number */ static class Negative implements conditional { @Override public boolean Check(Integer input) { if (input<0) { return true; }else{ return false; } } } /** * represents a prime number */ static class Prime implements conditional { @Override public boolean Check(Integer input) { for(int i=2;i<=input/2;i++) { if(input % i == 0) { return false; } } return true; } } /** * represents a perfect square */ static class PerfectSquare implements conditional { @Override public boolean Check(Integer input) { //find the root of input, whatever it is, and discard any decimal places. int root=(int) Math.sqrt(input); //if the integer root squared is equal to the original input, then the number was a perfect square. if(root*root==input) { return true; }else{ return false; } } } /** * simple utility method to cut back on the print statements in main * @param input the array to print */ static void PrintArray(Integer[] input) { for(int i=0;i