package testing;

public class Testing {
	
	/**
	 * Effects: if x==null throw NullPointerException else return the index of
	 * the LAST 0 in x. Return -1 if 0 does not occur in x
	 */
	public static int lastZero(int[] x) {
		// for (int i = x.length - 1; i >=0; i--) {
		for (int i = 0; i < x.length; i++) {
			if (x[i] == 0) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * Effects: if x==null throw NullPointerException else return the number of
	 * elements in x that are either odd or positive (or both)
	 */
	public static int oddOrPos(int[] x) {
		int count = 0;
		for (int i = 0; i < x.length; i++) {
			// if (x[i] % 2 == -1 || x[i] > 0) {
			if (x[i] % 2 == 1 || x[i] > 0) {
				count++;
			}
		}
		return count;
	}

	/**
	 * Effects: If x==null throw NullPointerException else return the index of
	 * the last element in x that equals y. If no such element exists, return -1
	 */
	public static int findLast(int[] x, int y) {
		// for (int i = x.length - 1; i >= 0; i--) {
		for (int i = x.length - 1; i > 0; i--) {
			if (x[i] == y) {
				return i;
			}
		}
		return -1;
	}

	/**
	 * Effects: If x==null throw NullPointerException else return the number of
	 * positive elements in x
	 */
	public static int countPositive(int[] x) {
		int count = 0;
		for (int i = 0; i < x.length; i++) {
			// if (x[i] > 0) {
			if (x[i] >= 0) {
				count++;
			}
		}
		return count;
	}

	private static String printTab(int[] x) {
		String s = "[";
		for (int i = 0; i < x.length; i++) {
			s += x[i];
			if (i < x.length - 1) {
				s += ", ";
			}
		}
		s += "]";
		return s;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] x1 = { 0, 1, 0 };
		System.out.println("lastZero(" + printTab(x1) + ") = "
				+ Testing.lastZero(x1));
		int[] x2 = { -3, -2, 0, 1, 4 };
		System.out.println("oddOrPos(" + printTab(x2) + ") = "
				+ Testing.oddOrPos(x2));
		int[] x3 = { 2, 3, 5 };
		int y = 2;
		System.out.println("findLast(" + printTab(x3) + ", " + y + ") = "
				+ Testing.findLast(x3, y));
		int[] x4 = { -4, 2, 0, 2 };
		System.out.println("countPositive(" + printTab(x4) + ") = "
				+ Testing.countPositive(x4));
	}
}
