================================= Topic07_Code01_GenericMethod preparation ================================= public class Main { public static void main(String[] args) { printTwice("hello"); } public static void printTwice(String x) { System.out.println(x); System.out.println(x); } } // printTwice(1234); // printTwice(4.0 / 3); ================================= Topic07_Code01_GenericMethod ================================= public class Main { public static void printTwice(T x) { System.out.println(x); System.out.println(x); } public static void main(String[] args) { printTwice("hello"); printTwice(1234); printTwice(4.0 / 3); } } // public static void printTwiceB(T x) // { // T y=x; // System.out.println(y); // System.out.println(y); // } ================================= Topic07_Code02_GenericClass (preparation) ================================= import java.io.*; import java.util.*; class Pair { private String first; private String second; public Pair() { } // first and second automatically initialized as null public Pair(String x1, String x2) { first = x1; second = x2; } @Override public String toString() { return "(1)" + first + " (2)" + second; } } public class Main { public static void main(String[] args) { Pair p0 = new Pair(); Pair p1 = new Pair("hello", "cheers"); System.out.println(p0); System.out.println(p1); // Pair p2 = new Pair(true, false); // Pair p3 = new Pair(123, 456); // Pair p4 = new Pair(12.3, 45.6); // Pair p5 = new Pair(123, "cheers"); // System.out.println(p2); // System.out.println(p3); // System.out.println(p4); // System.out.println(p5); } } ================================= Topic07_Code02_GenericClass ================================= class Pair { public T first; public T second; public Pair() { } public Pair(T x1, T x2) { first = x1; second = x2; } @Override public String toString() { return "(1)" + first + " (2)" + second; } } public class Main { public static void main(String[] args) { Pair p0 = new Pair(); Pair p1 = new Pair("hello", "cheers"); Pair p5 = new Pair(123, "cheers"); System.out.println(p0); // (1)null (2)null System.out.println(p1); // (1)hello (2)cheers System.out.println(p5); // (1)123 (2)cheers System.out.println(p5.first.getClass().toString()); System.out.println(p5.second.getClass().toString()); } } ================================= Topic07_Code02_GenericClass ================================= class Pair { private T first; private T second; public Pair() { } // first and second automatically initialized as null public Pair(T x1, T x2) { first = x1; second = x2; } @Override public String toString() { return "(1)" + first + " (2)" + second; } } public class Main { public static void main(String[] args) { Pair p0 = new Pair(); Pair p1 = new Pair("hello", "cheers"); Pair p2 = new Pair(true, false); Pair p3 = new Pair(123, 456); Pair p4 = new Pair(123, 456); Pair p5 = new Pair(123, "cheers"); //Error: The constructor Pair(int, String) is undefined // Pair p6 = new Pair(123,"cheers"); System.out.println(p0); System.out.println(p1); System.out.println(p2); System.out.println(p3); System.out.println(p4); System.out.println(p5); } } // (1)null (2)null // (1)hello (2)cheers // (1)true (2)false // (1)123 (2)456 // (1)123 (2)456 // (1)123 (2)cheers ================================= Topic07_Code03_ArrayListAndSort ================================= import java.util.ArrayList; import java.util.Collections; public class Main { public static void main(String[] args) { ArrayList arrlist = new ArrayList<>(); arrlist.add(1234); arrlist.add(8899); arrlist.add(36); Collections.sort(arrlist); System.out.println(arrlist); // Output: [36, 1234, 8899] } } ================================= Topic07_Code03_ArrayAndSort ================================= import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Collections; public class Main { public static void main(String[] args) { ArrayList arrlist = new ArrayList<>(); arrlist.add(1234); arrlist.add(8899); arrlist.add(36); Collections.sort(arrlist); Object[] a = arrlist.toArray(); System.out.println(Arrays.toString(a)); // Output: [36, 1234, 8899] List b = arrlist.subList(0, 2); System.out.println(b); // Output: [36, 1234] } } ================================= Topic07_Code04_DiamondSyntax ================================= class Pair { private T first; private T second; public Pair() { } // first and second automatically initialized as null public Pair(T x1, T x2) { first = x1; second = x2; } @Override public String toString() { return "(1)" + first + " (2)" + second; } } public class Main { public static void main(String[] args) { Pair p0 = new Pair<>(); Pair p1 = new Pair<>("hello", "cheers"); Pair p2 = new Pair<>(true, false); Pair p3 = new Pair<>(123, 456); Pair p4 = new Pair<>(123, 456); Pair p5 = new Pair<>(123, "cheers"); System.out.println(p0); System.out.println(p1); System.out.println(p2); System.out.println(p3); System.out.println(p4); System.out.println(p5); } } ================================= Topic07_Code05_MobileDevice ================================= class Smartphone { } class Pager { } class TabletPC { } class MobileDevice { private static int count = 0; MobileDevice() { count++; System.out.println(count); } // ... } public class Main { public static void main(String[] args) { MobileDevice phone = new MobileDevice<>(); MobileDevice pager = new MobileDevice<>(); MobileDevice pc = new MobileDevice<>(); } } ================================= Topic07_Code06_Pitfalls ================================= import java.util.*; class Smartphone {} class Pager {} class TabletPC {} class MobileDevice { private static int count = 0; MobileDevice() { count++; System.out.println(count); } public static void append_arbitary(ArrayList list) { T one = null; // one = new T(); // compile-time error list.add(one); } public static void appendOne(ArrayList list, T one) { list.add(one); } } public class Main { public static void main(String[] args) { MobileDevice phone = new MobileDevice<>(); MobileDevice pager = new MobileDevice<>(); MobileDevice pc = new MobileDevice<>(); } } ================================= Topic07_Code06_Pitfalls ================================= class Smartphone { } class Pager { } class TabletPC { } class MobileDevice { private static int count=0; MobileDevice() {count++;System.out.println(count);} } public class Main { // public void print(MobileDevice x) {} // public void print(MobileDevice x) {} public static void main(String[] args) { MobileDevice phone = new MobileDevice<>(); MobileDevice pager = new MobileDevice<>(); MobileDevice pc = new MobileDevice<>(); } } ================================= Topic07_Code09_PairTypeErased ================================= //Topic07 Generic Programming Slide 10 class Pair { public Object first; public Object second; public Pair() { } public Pair(Object first, Object second) { this.first = first; this.second = second; } @Override public String toString() { return "(1)" + first + " (2)" + second; } } public class Main { public static void main(String[] args) { Pair p0 = new Pair(); Pair p1 = new Pair("hello", "cheers"); Pair p5 = new Pair(123, "cheers"); System.out.println(p0); // (1)null (2)null System.out.println(p1); // (1)hello (2)cheers System.out.println(p5); // (1)123 (2)cheers System.out.println(p5.first.getClass().toString()); System.out.println(p5.second.getClass().toString()); } } ================================= ^_^ ================================= import java.util.*; class Student {} class Teacher {} public class Main { static ArrayList arrT = new ArrayList<>(); static ArrayList arrS = new ArrayList<>(); private static void lowerSchoolFees(ArrayList arr) {} private static void raiseTeacherSalary(ArrayList arr) {} public static void main(String[] args) { //.. raiseTeacherSalary(arrT); lowerSchoolFees(arrS); } } ================================= Topic07_Code10_TestingRawTypes ================================= class Box { private T t; public void set(T t) { this.t = t; } @Override public String toString() { return "This is " + t; } // ... } public class Main { public static void main(String[] args) { Box a = new Box<>(); a.set(3); System.out.println(a); Box b = new Box(); // warning <== "raw type" b.set(4.0 / 3); System.out.println(b); // Box b = new Box<>(); // b.set(4.0 / 3); // System.out.println(b); } ================================= Topic07_Code ================================= class Pair { public T first; public T second; public Pair() { } public Pair(T x1, T x2) { first = x1; second = x2; } @Override public String toString() { return "(1) " + first + ", (2) " + second; } } public class Main { public static void main(String[] args) { Pair p0 = new Pair<>(100, 200); // Pair p1 = p0; // Pair p2 = (Pair)p0; Pair p3 = (Pair) p0; Pair p4 = p0; System.out.println(p3); System.out.println(p4); } } ================================= Topic07_Code11_Pair2TypeParameters ================================= class TwoTypePair { private T1 first; private T2 second; public TwoTypePair(T1 firstItem, T2 secondItem) { first = firstItem; second = secondItem; } public void setFirst(T1 newFirst) { first = newFirst; } public void setSecond(T2 newSecond) { second = newSecond; } public T1 getFirst() { return first; } public T2 getSecond() { return second; } public String toString() { return ("first: " + first.toString() + "\n" + "second: " + second.toString()); } public boolean equals(Object otherObject) { if (otherObject == null) return false; else if (getClass() != otherObject.getClass()) return false; else { TwoTypePair otherPair = (TwoTypePair) otherObject; return (first.equals(otherPair.first) && second.equals(otherPair.second)); } } } public class Main { public static void main(String[] args) { TwoTypePair x1 = new TwoTypePair<>("USD", 123); TwoTypePair x2 = new TwoTypePair<>("USD", 456); TwoTypePair x3 = new TwoTypePair<>("USD", 123); System.out.println(x1.equals(x2));// false System.out.println(x1.equals(x3));// true } } ================================= Topic07_Code12_Inheritance ================================= class C0 { } class C1 extends C0 { } class C2 extends C1 { } class C3 extends C1 { } class Pair { private T first; private T second; public Pair(T x1, T x2) { first = x1; second = x2; } public void display(S head1, S head2) { System.out.println("(" + head1 + ")" + first + ", (" + head2 + ")" + second); } public > void showHigherCost(S cost1, S cost2) { S higher = cost1.compareTo(cost2) > 0 ? cost1 : cost2; if (higher == cost1) System.out.printf("More expensive: " + first + " $" + higher); else System.out.println("More expensive: " + second + " $" + higher); } } public class Main { public static void main(String[] args) { Pair p = new Pair("Orange", "Pear"); p.display("a", "b"); p.display(1, 2); p.showHigherCost(10.0, 20.0); p.display("a", "b"); p.display(1, 2); p.showHigherCost(10.0, 20.0); } } /* (a)Orange, (b)Pear (1)Orange, (2)Pear More expensive: Pear $20.0 (a)Orange, (b)Pear (1)Orange, (2)Pear More expensive: Pear $20.0 */ ================================= Topic07_Code13_GenericClassUnorderedPair ================================= class Pair { private T first; private T second; public Pair(T x1, T x2) { first = x1; second = x2; } @Override public String toString() { return "(1)" + first + " (2)" + second; } public T getFirst() { return first; } public T getSecond() { return second; } } class UnorderedPair extends Pair { public UnorderedPair(T firstItem, T secondItem) { super(firstItem, secondItem); } public boolean equals(Object otherObject) { if (otherObject == null) return false; else if (getClass() != otherObject.getClass()) return false; else { UnorderedPair otherPair = (UnorderedPair) otherObject; return ( getFirst().equals(otherPair.getFirst()) && getSecond().equals(otherPair.getSecond()) ) || ( getFirst().equals(otherPair.getSecond()) && getSecond().equals(otherPair.getFirst())); } } } public class Main { public static void main(String[] args) { UnorderedPair x1 = new UnorderedPair<>("ABC", "DEF"); UnorderedPair x2 = new UnorderedPair<>("DEF", "ABC"); UnorderedPair x3 = new UnorderedPair<>("DEF", "AAA"); System.out.println(x1.equals(x2));// true System.out.println(x1.equals(x3));// false } } =================================