Nama : Alfiyah Kelas : 2 D3 IT A NRP : 2103141009 1. Percobaan 1 (Type parameter yang dibatasi) a. Source Code Stats.java package Generic2; public class Stats <T extends Number>{ T[] nums; public Stats(T[] o) { this.nums = o; } double average(){ double sum = 0.0; for(int i=0; i<nums.length; i++){ sum += nums[i].doubleValue(); } return sum/nums.length; } } BoundsDemo.java package Generic2; public class BoundsDemo { public static void main(String[] args) { Integer inums[] = {1, 2, 3, 4, 5}; Stats<Integer> iob = new Stats<Integer>(inums); double v = iob.average(); System.out.println("iob average is "+v); Double dnums[] = {1.1, 2.2, 3.3, 4.4, 5.5}; Stats<Double> dob = new Stats<Double>(dnums); double w = dob.average(); System.out.println("dob average is "+w); } } b. Hasil Output c. Analisa Program 2. Percobaan 2 (Generic pada konstruktor, dengan typr parameter dibatasi) a. Source Code GenCons.java package Generic2; public class GenCons { private double val; <T extends Number> GenCons(T arg){ val = arg.doubleValue(); } void showval1(){ System.out.println("val : "+val); } } GenConsDemo.java package Generic2; public class GenConsDemo { public static void main(String[] args) { GenCons test = new GenCons(100); GenCons test2 = new GenCons(123.5F); test.showval1(); test2.showval1(); } } b. Hasil Output 2 c. Analisa Program 3. Percobaan 3 (Menggunakan Interface Comparable Generic) a. Source Code MyClass.java package Generic2; interface MinMax<T extends Comparable<T>>{ T min(); T max(); } public class MyClass <T extends Comparable<T>> implements MinMax{ T[] vals; MyClass(T[] o) { this.vals = o; } @Override public T min() { T v = vals[0]; for(int i=1; i<vals.length; i++){ if(vals[i].compareTo(v)<0){ v = vals[i]; } } return v; } @Override public T max() { T v = vals[0]; for(int i = 1; i< vals.length; i++){ if(vals[i].compareTo(v)>0){ v = vals[i]; } } return v; } } 3 GenIfDemo.java package Generic2; public class GenIfDemo { public static void main(String[] args) { Integer inums[] = {3, 6, 2, 8, 6}; Character chs[] = {'b', 'r', 'p', 'w'}; MyClass<Integer> iob = new MyClass<Integer>(inums); MyClass<Character> cob = new MyClass<Character>(chs); System.out.println("Max value in inums : "+iob.max()); System.out.println("Min value in inums : "+iob.min()); System.out.println("Max value in chs : "+cob.max()); System.out.println("Min value in chs : "+cob.min()); } b. Hasil Output c. Analisa Program 4 4. Percobaan 4 (Menggunakan Unbounded Wildcard(1)) a. Source Code TestGeneric.java package Generic2; import java.util.*; public class TestGeneric { public static void printList(List<?> list){ for(Object elem : list){ System.out.println(elem+""); } System.out.println(); } public static void main(String[] args) { List<Integer> li = Arrays.asList(1, 2, 3); List<String> ls = Arrays.asList("one", "two", "three"); printList(li); printList(ls); } } b. Hasil Output c. Analisa Program 5 5. Percobaan 5 (Menggunakan Unbounded Wildcard(2)) a. Source Code Stats.java package Generic2; public class Stats <T extends Number>{ T[] nums; public Stats(T[] o) { this.nums = o; } double average(){ double sum = 0.0; for(int i=0; i<nums.length; i++){ sum += nums[i].doubleValue(); } return sum/nums.length; } boolean sameAvg(Stats<?> ob){ if(average()== ob.average()){ return true; } return false; } } WildCardDemo.java package Generic2; public class WildCardDemo { public static void main(String[] args) { Integer inums[] = {1, 2, 3, 4, 5}; Stats<Integer> iob = new Stats<Integer>(inums); double v = iob.average(); System.out.println("iob average is "+v); Double dnums[] = {1.1, 2.2, 3.3, 4.4, 5.5}; Stats<Double> dob = new Stats<Double>(dnums); double w = dob.average(); System.out.println("dob average is "+w); Float fnums[] = {1.0F, 2.0F, 3.0F, 4.0F, 5.0F}; Stats<Float> fob = new Stats<Float>(fnums); double x = fob.average(); System.out.println("fob average is "+x); System.out.println("Average of iob and dob "); if(iob.sameAvg(dob)){ System.out.println("are the same"); }else{ System.out.println("differ"); } 6 System.out.println("Averages of iob and fob "); if(iob.sameAvg(fob)){ System.out.println("are the same"); }else{ System.out.println("differ"); } } } b. Hasil Output c. Analisa Program 6. Percobaan 6 (Menggunakan UpperBoundWildcard(2)) a. Source Code TestGeneric2.java package Generic2; import java.util.*; public class TestGeneric2 { public static double sumOfList(List<? extends Number> list){ double s = 0.0; for(Number n : list){ s += n.doubleValue(); } return s; } public static void main(String[] args) { List<Integer> li = Arrays.asList(1, 2, 3); System.out.println("sum = "+sumOfList(li)); List<Double> ld = Arrays.asList(1.2, 2.3, 3.5); System.out.println("sum = "+sumOfList(ld)); } } 7 b. Hasil Output c. Analisa Program 7. Percobaan 7(Menggunakan UpperBound WildCard(2)) a. Source Code TwoD.java package Generic2; public class TwoD { int x, y; public TwoD(int a, int b) { this.x = a; this.y = b; } } ThreeD.java package Generic2; public class ThreeD extends TwoD{ int z; public ThreeD(int a, int b, int c) { super(a, b); this.z = c; } } FourD.java package Generic2; public class FourD extends ThreeD{ int t ; public FourD(int a, int b, int c, int d) { super(a, b, c); this.t = d; } } 8 Coords.java package Generic2; public class Coords<T extends TwoD>{ T[] coords; Coords(T[] o) { this.coords = o; } } BoundedWildCard.java package Generic2; public class BoundedWildcard { static void showXY(Coords<?> c){ System.out.println("X Y Coordinates : "); for(int i=0; i<c.coords.length; i++){ System.out.println(c.coords[i].x+" "+c.coords[i].y); } System.out.println(); } static void showXYZ(Coords<? extends ThreeD> c){ System.out.println("X Y Z Coordinates : "); for(int i=0; i<c.coords.length; i++){ System.out.println(c.coords[i].x+" "+c.coords[i].y+" "+c.coords[i].z); } System.out.println(); } static void showAll(Coords<? extends FourD> c){ System.out.println("X Y Z T Coordinates : "); for(int i=0; i<c.coords.length; i++){ System.out.println(c.coords[i].x+" "+c.coords[i].y+" "+c.coords[i].z+" "+c.coords[i].t); } System.out.println(); } public static void main(String[] args) { TwoD td[]= { new TwoD(0, 0), new TwoD(7, 9), new TwoD(18, 4), new TwoD(-1, -23) }; Coords<TwoD> tdlocs = new Coords<TwoD>(td); System.out.println("Content of tdlocs . "); showXY(tdlocs); // showXYZ(tdlocs); // showAll(tdlocs); FourD fd[] = { new FourD(1, 2, 3, 4), new FourD(6, 8, 14, 8), Coords<FourD> fdlocs = new Coords<FourD>(fd); showXY(fdlocs); showXYZ(fdlocs); 9 new FourD(22,9 , 4, 9), new FourD(3, 4, -23, 17) }; Coords<FourD> fdlocs = new Coords<FourD>(fd); showXY(fdlocs); showXYZ(fdlocs); showAll(fdlocs); } } b. Hasil Output c. Analisa Program 10 8. Percobaan 8 (Menggunakan LowerBound Wildcard) a. Source Code BoxImpl.java package Generic2; interface Box<T>{ T get(); void put(T element); } public class BoxImpl<T> implements Box<T>{ private T element; public BoxImpl(T element) { this.element = element; } @Override public T get(){ return element; } public void put(T element){ this.element = element; } void copyTo(BoxImpl<Number> bNum) { element = (T) bNum; } @Override public String toString() { return ""+element; } } TestBoxKu.java package Generic2; public class TestBoxKu { public static void main(String[] args) { BoxImpl<Integer> bln = new BoxImpl<Integer>(7); BoxImpl<Number> bNum = new BoxImpl<Number>(10); System.out.println("Sebelum diCopy"); System.out.println("Data bln : "+bln.toString()); System.out.println("Data bNUm : "+bNum.toString()); bln.copyTo(bNum); System.out.println(""); System.out.println("Sesudah diCopy"); System.out.println("Data bln : "+bln.toString()); System.out.println("Data bNUm : "+bNum.toString()); } } 11 b. Hasil Output c. Analisa Program 12