IC211 Spring AY 2020

HW Array Lists and Generics

Name (Last, First): ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________

  1. [30pts] Consider the following program HWa:
    import java.util.*;
    
    public class HWa {
      public static class APair {
        Object x, y;
        public APair(Object o1, Object o2) {
          x = o1;
          y = o2;
        }
        public Object first() { return x; }
        public Object second() { return y; }
      }
    
      public static class BPair<T> {
        T x, y;
        public BPair(T o1, T o2) {
          x = o1;
          y = o2;
        }
        public T first() { return x; }
        public T second() { return y; }
      }
    
      public interface Fooable {
        public String foo();
      }
    
    
      public static class CPair {
        Fooable x, y;
        public CPair(Fooable o1, Fooable o2) {
          x = o1;
          y = o2;
        }
        public Fooable first() { return x; }
        public Fooable second() { return y; }
      }
    
      public static class Bat implements Fooable {
        int val;
        public Bat(int k) { val = k; }
        public String foo() { return "Bat" + val; }
        public int bar() { return val*val; }
      }
    
      public static class Pig implements Fooable {
        String lab;
        public Pig(String s) { lab = s; }
        public String foo() { return "Pig" + lab; }
      }
    
      public static void main(String[] args) {
        APair a = new APair(new Bat(7), new Pig("Q"));
        BPair<Bat> b = new BPair<Bat>(new Bat(8), new Bat(9));
        CPair c = new CPair(new Bat(5), new Pig("Z"));;
      }
    }
    Give the statement(s) we'd have to add to the end of main() in order to:
    1. call foo() on first of a:
      ____________________________________________________

    2. call foo() on first of b:
      ____________________________________________________

    3. call foo() on first of c:
      ____________________________________________________

    1. call bar() on first of a:
      ____________________________________________________

    2. call bar() on first of b:
      ____________________________________________________

    3. call bar() on first of c:
      ____________________________________________________

  2. [30pts] Consider b in main above. Why would it have been wrong to initialize it like this:
    BPair<Bat> b = new BPair<Bat>(new Bat(8), new Pig("Z"));
    How could you define b as a BPair in such a way as to allow one to initialize it with (new Bat(8), new Pig("Z"))?
  3. [40pts] For some, the question "Does Java support multiple inheritance?" does not have a straight forward yes/no answer. Give a thorough and nuanced response to it. Please re-read the lecture notes on Interfaces thoroughly before you start googling for information. I want you to ponder, think and internalize, not copy out an answer you found somewhere else and assume is good because, after all, it's on the internet.
    Write your response in the space below.