package eecs2030.test2; import java.util.ArrayList; import java.util.List; public class Test2G { /** * The size of the list required by the method Test2G.negate2 */ public static final int NEGATE2_LIST_SIZE = 2; private Test2G() { // empty by design } /** * Returns true if value is strictly less than min * or strictly greater than max, and false otherwise. * * @param min * a minimum value * @param max * a maximum value * @param value * a value to check * @return true if value is strictly less than min * or strictly greater than max, and false otherwise * @pre. min is greater than or equal to max */ public static boolean isOutside(int min, int max, int value) { return value < min || value > max; } /** * Given a list containing exactly 2 integers, negates the values of the * integers in the list. The list t is not modified by this method. * For example, given a list * *

* [-5, 9] * *

* negate2 modifies the list so that it becomes * *

* [5, -9] * * @pre. t is not null * * @param t * a list containing exactly 2 integers * @throws IllegalArgumentException * if the list does not contain exactly 2 integers */ public static void negate2(List t) { if (t.size() != 2) { throw new IllegalArgumentException("list size != 2"); } int t0 = t.get(0); int t1 = t.get(1); t.set(0, -t0); t.set(1, -t1); } /** * Returns a new list containing the values 0, 1, 2, ..., n. Returns an * empty list if n is less than zero. * * @param n * the upper limit of the interval * @return a new list containing the values 0, 1, 2, ..., n or the empty * list if n is less than zero */ public static List interval(int n) { List t = new ArrayList(); for (int i = 0; i <= n; i++) { t.add(i); } return t; } /** * Returns a run length encoded list representing the numbers in the * the list t. The list t is not modified by this method. * *

* Consider the list containing twelve ones: * *

     * [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
     * 
* *

* A run length encoded version of the list would be: *

* *
     * [12, 1]         (twelve 1s)
     * 
* *

* Similarly, consider the list containing four tens and three fives: * *

     * [10, 10, 10, 10, 5, 5, 5]
     * 
* *

* A run length encoded version of the list would be: *

* *
     * [4, 10, 3, 5]         (four 10s, three 5s)
     * 
* *

* If the list contains few or no repeated values, then the run length * encoded version of the list is longer than the original list. For example, * the run length encoded version of the list: * *

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

* is: * *

     * [1, 1, 1, 2, 1, 3, 1, 4]    (one 1, one 2, one 3, one 4)
     * 
* * @param t a list of integer values * @return a new list equal to the run length encoded version of t * @pre t is not null * @pre t is not empty */ public static List encode(List t) { List rle = new ArrayList(); int count = 1; int prev = t.get(0); if (t.size() > 1) { for (int i = 1; i < t.size(); i++) { int digit = t.get(i); if (digit == prev) { count++; } else { rle.add(count); rle.add(prev); count = 1; prev = digit; } } } rle.add(count); rle.add(prev); return rle; } public static void main(String[] args) { // isOutside int min = 1; int max = 10; int value = 100; System.out.println( String.format("isOutside(%d, %d, %d) : ", min, max, value) + Test2G.isOutside(min, max, value)); // negate2 List t = new ArrayList(); t.add(3); t.add(5); String s = t.toString(); Test2G.negate2(t); System.out.println( String.format("negate2(%s) : %s", s, t.toString())); // interval int n = 9; System.out.println( String.format("interval(%d) : %s", n, Test2G.interval(n))); // encode t.clear(); t.add(10); t.add(10); t.add(10); t.add(10); t.add(5); t.add(5); t.add(5); System.out.println( String.format("encode(%s) : %s", t, Test2G.encode(t))); } }