package test2; import java.util.List; public class Utility2A { private Utility2A() { } /** * Returns the last integer in the argument list. * * @param t a list of integers * @return the last integer in t * @pre. t is not empty */ public static Integer last(List t) { return t.get(t.size() - 1); } /** * Computes the sum of the areas of zero or more rectangles. * The width of each rectangle is given by the list widths. * The height of each rectangle is given by the list heights. * For example, consider the two lists: * *
	 * widths   [1, 5, 10]
	 * heights  [2, 3,  4]
	 * 
* *

* The sum of the areas is equal to (1 * 2 + 5 * 3 + 10 * 4). * If a width or height of a rectangle is less than zero then * that area is not included in the sum. * * @param widths a non-null list of rectangle widths * @param heights a non-null list of rectangle heights * @return the sum of the rectangle areas not including negative areas * @throws IllegalArgumentException if widths and heights are lists * of different sizes */ public static int totalArea(List widths, List heights) { if (widths.size() != heights.size()) { throw new IllegalArgumentException(); } int area = 0; for (int i = 0; i < widths.size(); i++) { if (widths.get(i) > 0 && heights.get(i) > 0) { area += widths.get(i) * heights.get(i); } } return area; } /** * Computes the alternating sum of the integers in the argument * list. In an alternating sum, every second element is negated * before adding it to the sum. For example, consider the list: * *

	 * [0, 1, 2, 3, 4, 5]
	 * 
* *

* The alternating sum is given by (0 - 1 + 2 - 3 + 4 - 5). * The alternating sum for an empty list is zero. * * * @param t a non-null list of integers * @return the alternating sum of the integers in t */ public static int alternatingSum(List t) { int sum = 0; int multiple = 1; for (Integer i : t) { sum += multiple * i; multiple *= -1; } return sum; } }