1 Use Holder3 with the typeinfo.pets library to show that a Holder3 that is specified to hold a base type can also hold a derived type.
package job;
import typeinfo.pets.*;
class Holder<T> {
private T a;
Holder(T a) {
this.a = a;
}
void f() {
System.out.println(a);
}
void set(T a) {
this.a = a;
}
}
public class Ja15_1 {
public static void main(String[] args) {
Holder<Pet> h = new Pet("abcd");
h.f();
h.set(new Dog("qwer"));
h.f();
h.set(new Cat("asdf"));
h.f();
}
}
2 Create a holder class that holds three objects of the same type along with the methods to store and fetch those objects and a constructor to initialize all three.
package job;
import java.util.*;
class Holder<T> {
private T a;
private T b;
private T c;
void setA(T a) {
this.a = a;
}
void setB(T b) {
this.b = b;
}
void setC(T c) {
this.c = c;
}
void getA() {
System.out.println(a);
System.out.println(a.getClass().getName());
}
void getB() {
System.out.println(b);
System.out.println(a.getClass().getName());
}
void getC() {
System.out.println(c);
System.out.println(a.getClass().getName());
}
Holder(T a,T b,T c){
this.a=a;
this.b=b;
this.c=c;
}
}
public class Main{
public static void main(String args[]){
Holder a=new Holder(2,4.0d,5.222f);
a.getA();
a.getB();
a.getC();
}
}
output:
2
java.lang.Integer
4.0
java.lang.Integer
5.222
java.lang.Integer
3 Create and test a SixTuple generic.
package job;
import java.util.*;
import net.mindview.util.*;
import typeinfo.pets.*;
import static net.mindview.util.Print.*;
public class SixTuple<A,B,C,D,E,F> extends FiveTuple<A,B,C,D,E> {
public final F sixth;
SixTuple(A a, B b, C c, D d, E e, F f) {
super(a, b, c, d, e);
sixth = f;
}
public String toString() {
return "(" + first + ", " + second + ", " + third + ", " + fourth + ", " + fifth + ", " + sixth + ")";
}
}
public class Main{
public static void main(String[] args) {
SixTuple<Cat, Dog, Pug, Pet, String, Integer> ja = new Ja15_3<Cat, Dog, Pug, Pet, String, Integer>(new Cat(), new Dog(), new Pug(), new Pet(), "das", 56);
System.out.println(ja);
}
}
4 Generify' innerclasses/Sequence.java.
package job;
interface Selector<TT> {
boolean end();
TT current();
void next();
}
public class Main<TT> {
private TT[] items;
private int next = 0;
public Main(TT[] mm) {
items = mm;
}
public void add(TT x) {
if (next < items.length)
items[next++] = x;
}
private class A implements Selector<TT>