Name: ______________________________

Choose 5 out of the 7 questions to answer. Cross out the questions you don't want to have graded.

Here are some useful classes, objects, and methods:

.

  1. Consider the following class that keeps track of the sum of all its inputs (by inputs, I mean all values passed to calls to update):

    public class Summer {
        private int sum = 0;
    
        public void update(int x) {
            sum = sum + x;
        }
    
        public int getSum() {
            return sum;
        }
    }
    

    Define a new class that keeps track of the sum of the squares of all its inputs (e.g. with inputs 2,3,4 it should track 4+9+16) that uses an instance member of type Summer.













  2. Working with the class in #1, define a new class that keeps track of the sum of the cubes of all its inputs. Do this by making the new class a subclass of Summer and overriding the update method.













  3. Consider the following new definition for a more general summation-like class:

    interface Transform {
        int calculate(int x);
    }
    
    public class Accumulator {
        private int total = 0;
        private Transform trans;
    
        public Accumulator(Transform t) {
            trans = t;
        }
    
        public void update(int x) {
            total = total + trans.calculate(x);
        }
    
        public int getTotal() {
            return total;
        }
    }
    

    Define a class Abs that implements the Transform interface so that when used with Accumulator, getTotal would return the sum of the absolute values of all inputs. An example of use is shown below:

    Accumulator acc = new Accumulator(new Abs());
    acc.update(3);
    acc.update(-2);
    System.out.println(acc.getTotal());  // prints 5
    













  4. What does the following program print?

    public class Program {
       interface Predicate {
           boolean test(int x);
       }
    
       public static class Mystery implements Predicate {
           public boolean test(int x) {
               return x % 2 == 0;
           }
       }
    
       public static class AllTrue implements Predicate {
           public boolean test(int x) {
               return true;
           }
       }
    
       public static void printMatching(int[] array, Predicate p) {
           for (int x : array) {
               if (p.test(x))
                   System.out.println(x);
           }
       }
    
       public static void main(String[] args) {
           int[] a = {2,5,4,3,1,10};
           printMatching(a, new AllTrue());
           printMatching(a, new Mystery());
       }
    
    }
    













  5. Fill in the method below so that it returns a Map<String, Integer> that maps the element at index i in the first array to the element at index i in the second. For example, with input {"a", "b"} and {3, 4}, the output should be a map of the form {"a" → 3, "b" → 4}. You can assume that both inputs are the same size.

    public static Map<String, Integer> 
    zip(ArrayList<String> keys, ArrayList<Integer> values) {
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
    
  6. Define a class called PrintCounter that implements the following interface:

    interface PrintNum {
        void print(String x);
        int numberPrinted(char c);
    }
    

    It should keep track of the number of each character printed. For example, after calling print("abc") and print("a-b"), numberPrinted('a') should return 2, numberPrinted('-') should return 1, and numberPrinted('z') should return 0.

    The constructor should accept an instance of PrintStream that is used for printing (System.out is an instance of PrintStream).













  7. What does the following program print?

    public class Test {
        public static class Printer {
            public void print(String x) {
                if (x.charAt(0) == 'B')
                    throw new IllegalArgumentException("I don't print B words!");
                System.out.println(x);
            }
        }
    
        public static void test(Printer p, String message) {
            try {
                p.print(message);
            } catch (IllegalArgumentException e) {
                System.out.println("Error");
            }
        }
    
        public static void main(String[] args) {
            Printer out = new Printer();
            test(out, "Antonioni");
            test(out, "Bergman");
            test(out, "Cocteau");
            test(out, "Disney");
        }
    }