package eecs2030.test2; import java.util.ArrayList; import java.util.List; /** * Test 2 version F. * * @author EECS2030E Fall 2016 * */ public class Test2F { /** * The size of the list required by the method Test2F.min2 */ public static final int MIN2_LIST_SIZE = 2; private Test2F() { // empty by design } /** * Returns true if value is strictly greater than * min and strictly less 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 greater than * min and strictly less than max, and * false otherwise * @pre. min is less than or equal to max */ public static boolean isBetween(int min, int max, int value) { return value > min && value < max; } /** * Given a list containing exactly 2 integers, returns the smaller of the * two integers. The list t is not modified by this method. * For example: * *
     * t            Test2F.min2(t)
     * ---------------------------
     * [-5, 9]      -5
     * [3, 3]        3
     * [12, 6]       6
     * 
* * @pre. t is not null * * @param t * a list containing exactly 2 integers * @return * the minimum of the two values in t * @throws IllegalArgumentException * if the list does not contain exactly 2 integers */ public static int min2(List t) { if (t.size() != 2) { throw new IllegalArgumentException("list size != 2"); } int t0 = t.get(0); int t1 = t.get(1); int min = Math.min(t0, t1); return min; } /** * Counts the number of elements in t that are greater than * value. The list t is not modified by this method. * For example, if value == 3 then: * *
     * t                      Test2F.countGreaterThan(t, 3)
     * ----------------------------------------------------
     * []                     0
     * [-5]                   0
     * [3, 4]                 1
     * [12, 6, 8, 2, 2, 2]    3
     * 
* * @param t * a list * @param value * a value * @return number of elements in t that are greater than value * @pre. t is not null */ public static int countGreaterThan(List t, int value) { int count = 0; for (int i : t) { if (i > value) { count++; } } return count; } /** * Returns the int value equal to the value obtained by removing * n digits from the the end of aNumber. The * method will never remove all of the digits of aNumber. If * n is greater than the number of digits in * aNumber then the first digit of aNumber is * returned. For example: * *
     * aNumber    n     Test2F.removeDigits(aNumber, n)
     * ------------------------------------------------ 
     *  1         0      1 
     *  2         1      2
     *  87        1      8 
     *  100353    3      100 
     * -9325256   5     -93
     * -9325256   999   -9
     * 
* * @param aNumber * a number * @param n * the number of digits to remove from the end of aNumber * @return the number equal to removing n digits from the end of aNumber, or * the first digit of aNumber if n is greater than or equal to the * number of digits in aNumber */ public static int removeDigits(int aNumber, int n) { for (int i = 0; i < n; i++) { int remaining = aNumber / 10; if (remaining == 0) { return aNumber; } else { aNumber /= 10; } } return aNumber; } public static void main(String[] args) { // isBetween int min = 1; int max = 10; int value = 5; System.out.println( String.format("isBetween(%d, %d, %d) : ", min, max, value) + Test2F.isBetween(min, max, value)); // min2 List t = new ArrayList(); t.add(3); t.add(5); System.out.println( String.format("min2(%s) : %s", t.toString(), Test2F.min2(t))); // countGreaterThan t.clear(); t.add(4); t.add(5); t.add(6); t.add(7); t.add(8); value = 5; System.out.println( String.format("countGreaterThan(%s, %d) : %s", t.toString(), value, Test2F.countGreaterThan(t, value))); // removeDigits int aNumber = 100353; int n = 3; System.out.println( String.format("removeDigits(%d, %d) : %d", aNumber, n, Test2F.removeDigits(aNumber, n))); } }