Name: ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________
import java.util.*;
public class HWa
{
public static class APair
{
private 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>
{
private 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
{
private 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
{
private 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
{
private 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"));;
}
} |
|
|
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"))?