public class Homework1 { public static void main(String[] args) { Integer[] input = {0}; boolean result1 = contains(input, new Prime()); boolean result2 = contains(input, new PerfectSq()); boolean result3 = contains(input, new Negative()); System.out.println("Result 1: " + result1); System.out.println("Result 2: " + result2); System.out.println("Result 3: " + result3); } /* * Contains method designed to determine whether an array contains a * specified type of number, which is the second parameter for the method. * The first parameter is just an array of arbitrary type. The array is * checked by using a for loop in conjunction with calling the isTrue method * which tells whether the (in this case) number at index i is of the * specified type. If true is never returned from the isTrue invocations, * then the method automatically returns false. */ public static boolean contains(T[] input, Check c) { for (int i = 0; i < input.length; i++) { if (c.isTrue(input[i])) { return true; } } return false; } /* * Interface for all the types of ints that we are interested in. This is * to ensure that every class which extends this interface has the isTrue * method in it. This method is imperative to the success of the program * as a whole. */ public static interface Check { boolean isTrue(T item); } /* * The negative class. The class only contains a default constructor and * the isTrue method. The default constructor instantiates some arbitrarily * chosen negative number (I chose -2). The way the constructor is used * in the program allows us to do it this way, otherwise a randomly generated * negative number might be more appropriate. */ public static class Negative implements Check { public int Negative() { int i = -2; return i; } /* * The isTrue method for the negative class is quite easy; just return * the boolean item<0. If true, the passed parameter is negative, * otherwise its positive. */ @Override public boolean isTrue(Integer item) { return item < 0; } } /* * The prime class. This class contains both the implementation of isTrue * as well as a default constructor. Similar the the negative class, the * default constructor is only used as a specifier for the second parameter * for contains. If the program had other stipulations, a randomly * generated prime number may be of more use. However since this is not * neccessary, I arbitrarily chose 7 as the default constructor's prime. */ public static class Prime implements Check { public int Prime() { int i = 7; return i; } /* * The isTrue method for the Prime class is done by having a while * loop to iterate through the natural numbers starting at 2, and each * time it checks if when item is divided by i if there is any remainder, * and if i equals item. * If so, that means that there are more than 2 factors of that number * and it is not prime. Otherwise, the method * continues checking until i==item. At this point the value of i is * checked with the value of item. If they are the same, that means the * for loop completely went through all possible natural factors and none * divided with no remainder (besides 1 and itself, which is never checked * because it is always true). */ @Override public boolean isTrue(Integer item) { if (item == 1) { return false; } int i = 2; while (item % i != 0 && i != item) { i++; } if (i == item) { return true; } return false; } } /* * The class PerfectSq which, same as the previous two classes, has two methods: * a constructor and an implementation of isTrue. The constructor is designed * to instantiate some arbitrarily chosen perfect square number to be the * specifier as to what isTrue should be looking for. */ public static class PerfectSq implements Check { public int PerfectSq() { int i = 49; return i; } /* * */ @Override public boolean isTrue(Integer item) { for (int i = 1; i < item; i++) { if (item / i == i) { return true; } } return false; } } }