package test2; import java.util.ArrayList; import java.util.List; public class Utility2D { private Utility2D() { } /** * Returns the double value equal to 1 divided by x. For example, * *
	 * Utility2D.reciprocal(2)  returns 0.5
	 * 
* * @param x a value * @return the quotient 1 divided by x * @throws ArithmeticException if x is equal to zero */ public static double reciprocal(int x) { if (x == 0) { throw new ArithmeticException(); } return 1.0 / x; } /** * Returns a list containing the elements [min, min + 1, min + 2, ..., max] * where min is the smaller value of a and b, and max is the larger value of * a and b. If a and b have the same value then the returned list contains * only one element and that element is equal to a. For example: * *
	 * Utility2D.interval(-3, 2)  returns the list [-3, -2, -1, 0, 1, 2]
	 * 
* * @param a one end of an interval * @param b the other end of an interval * @return the list containing the elements [min, min + 1, min + 2, ..., max] * where min is the smaller value of a and b, and max is the larger * value of a and b */ public static List interval(int a, int b) { int min = Math.min(a, b); int max = Math.max(a, b); List t = new ArrayList(); for (int i = min; i <= max; i++) { t.add(i); } return t; } /** * Returns the zipper of two lists s and t without modifying * s or t. The zipper of * lists s and t is the list formed by alternating elements * from s and t. The sizes of the lists can be different. * Let the size of the shorter list be n; then the returned * list is the zipper of the shorter list and the first * n elements of the longer list, followed by the remaining * elements of the longer list. * *

* For example: * *

     * s            t            Test2I.zipper(s, t)
     * ---------------------------------------------
     * [1]          [2]          [1, 2]
     * [5, 7]       [6, 8]       [5, 6, 7, 8]
     * [1, 1, 1]    [0, 0, 0]    [1, 0, 1, 0, 1, 0]
     * []           [10, 9, 8]   [10, 9, 8]
     * [-5, 7, 2]   [99]         [-5, 99, 7, 2]
     * 
* * @param s a non-null list * @param t a non-null list * @return the zipper of the lists s and t */ public static List zipper(List s, List t) { List z = new ArrayList(); if (s.size() <= t.size()) { for (int i = 0; i < s.size(); i++) { z.add(s.get(i)); z.add(t.get(i)); } z.addAll(t.subList(s.size(), t.size())); } else { for (int i = 0; i < t.size(); i++) { z.add(s.get(i)); z.add(t.get(i)); } z.addAll(s.subList(t.size(), s.size())); } return z; } }