3. 아래의 결과가 나오도록 프로그래밍을 완성 하시오.
class DDBoxDemo {
public static void main(String[] args) {
DBox<String, Integer> box1 = new DBox<>();
box1.set("Apple", 25);
DBox<String, Integer> box2 = new DBox<>();
box2.set("Orange", 33);
DDBox<DBox<String, Integer>, DBox<String, Integer>> ddbox = new DDBox<>();
ddbox.set(box1, box2);
System.out.println(ddbox);
}
}
class Const {
static final String AND = " & ";
static final String NEW_LINE = "\n";
}
class Functions {
static String concatTwoWith(Object obj1, Object obj2, String bond) {
String str1 = String.valueOf(obj1);
String str2 = String.valueOf(obj2);
return (str1.concat(bond).concat(str2));
}
}
interface Box<T1, T2> {
public abstract void set(T1 t1, T2 t2);
}
class DBox<T1 extends String, T2 extends Integer> implements Box<T1, T2> {
private T1 t1;
private T2 t2;
DBox() {
}
@Override
public void set(T1 t1, T2 t2) {
this.t1 = t1;
this.t2 = t2;
}
@Override
public String toString() {
return (Functions.concatTwoWith(this.t1, this.t2, Const.AND));
}
}
class DDBox<T1 extends DBox, T2 extends DBox> implements Box<T1, T2> {
private T1 t1;
private T2 t2;
DDBox() {
}
@Override
public void set(T1 t1, T2 t2) {
this.t1 = t1;
this.t2 = t2;
}
@Override
public String toString() {
return (Functions.concatTwoWith(this.t1, this.t2, Const.NEW_LINE));
}
}
4. 아래와 같이 출력값이 나오도록 프로그래밍 하시오.
public static void main(String[] args) {
Box<Integer> box1 = new Box<>();
box1.set(99);
Box<Integer> box2 = new Box<>();
box2.set(55);
System.out.println(box1.get() + " & " + box2.get());
swapBox(box1, box2);
System.out.println(box1.get() + " & " + box2.get());
}
class SwapBox {
public static void main(String[] args) {
Box<Integer> box1 = new Box<>();
box1.set(99);
Box<Integer> box2 = new Box<>();
box2.set(55);
System.out.println(box1.get() + " & " + box2.get());
swapBox(box1, box2);
System.out.println(box1.get() + " & " + box2.get());
}
private static <T extends Integer> void swapBox(Box<T> box1, Box<T> box2) {
T tBox1 = box1.get();
T tBox2 = box2.get();
box1.set(tBox2);
box2.set(tBox1);
}
}
class Box<T> {
private T obj;
Box() {
}
void set(T obj) {
this.obj = obj;
}
T get() {
return obj;
}
}