Laporan Resmi Algoritma Dan Struktur Data (Generic 2) : Yuliarta Rizki Nusantoko (2103181008)
Laporan Resmi Algoritma Dan Struktur Data (Generic 2) : Yuliarta Rizki Nusantoko (2103181008)
Stats(T[] o) {
nums = o;
}
double average() {
double sum = 0.0;
Analisa :
2. Listing Program :
class GenCons {
private double val;
<T extends Number> GenCons(T arg) {
val = arg.doubleValue();
}
void showval() {
System.out.println("val: " + val);
}
}
public class GenConsDemo {
public static void main(String args[]) {
GenCons test = new GenCons(100);
GenCons test2 = new GenCons(123.5F);
test.showval();
test2.showval();
}
}
Outputnya :
Analisa :
3. Listing Program :
MyClass(T[] o) { vals = o; }
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;
}
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;
}
}
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());
}
}
Outputnya :
Analisa :
4. Listing Program :
import java.util.List;
import java.util.Arrays;
Analisa :
5. Listing Program :
return false;
}
}
// Demonstrate wildcard.
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);
Analisa :
6. Listing Program :
import java.util.Arrays;
import java.util.List;
public class TestGeneric2 {
public static double sumOfList(List<? extends Number> list) {
double s = 0.0;
for (Number n : list) {
s += n.doubleValue();
}
return s;
}
Analisa :
7. Listing Program :
class TwoD {
int x, y;
TwoD(int a, int b) {
x = a;
y = b;
}
}
// Three-dimensional coordinates.
class ThreeD extends TwoD {
int z;
ThreeD(int a, int b, int c) {
super(a, b);
z = c;
}
}
// Four-dimensional coordinates.
class FourD extends ThreeD {
int t;
FourD(int a, int b, int c, int d) {
super(a, b, c);
t = d;
}
}
// This class holds an array of coordinate objects.
class Coords<T extends TwoD> {
T[] coords;
Coords(T[] o) { coords = o; }
}
// Demonstrate a bounded wildcard.
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();
}
System.out.println("Contents of tdlocs.");
showXY(tdlocs); // OK, is a TwoD
// showXYZ(tdlocs); // Error, not a ThreeD
// showAll(tdlocs); // Erorr, not a FourD
System.out.println("Contents of fdlocs.");
// These are all OK.
showXY(fdlocs);
showXYZ(fdlocs);
showAll(fdlocs);
}
}
Outputnya :
Analisa :
8. Listing Program :
Outputnya :
Analisa :
4. LATIHAN
1. Listing Program :
Analisa :
2. Listing Program :
import java.util.*;
Analisa :
3. Listing Program :
Outputnya :
Analisa :
5. TUGAS
4. Listing Program :
Outputnya :
Analisa :
6.KESIMPULAN