0% found this document useful (0 votes)
50 views10 pages

Laporan Resmi Algoritma Dan Struktur Data (Generic 2) : Yuliarta Rizki Nusantoko (2103181008)

1. The document discusses generic programming in Java and provides examples of using generics with classes, interfaces, wildcards, and bounded wildcards. 2. Several code examples are given to demonstrate how to define generic classes and methods, as well as how to specify type parameters and bounds. 3. The examples show how to write code that works for multiple data types rather than being specific to one type, making the code more flexible and reusable.

Uploaded by

Arta Rizki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views10 pages

Laporan Resmi Algoritma Dan Struktur Data (Generic 2) : Yuliarta Rizki Nusantoko (2103181008)

1. The document discusses generic programming in Java and provides examples of using generics with classes, interfaces, wildcards, and bounded wildcards. 2. Several code examples are given to demonstrate how to define generic classes and methods, as well as how to specify type parameters and bounds. 3. The examples show how to write code that works for multiple data types rather than being specific to one type, making the code more flexible and reusable.

Uploaded by

Arta Rizki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

LAPORAN RESMI ALGORITMA DAN

STRUKTUR DATA (GENERIC 2)


Yuliarta Rizki Nusantoko (2103181008)
2.TUJUAN
1.Memahami mengenai konsep generic

2.Mengetahui cara merubah dari bentuk non generic menjadi generic.

3.Memahami generic pada Collection.


3.PERCOBAAN
1. Listing Program :

class Stats<T extends Number> {


T[] nums;

Stats(T[] o) {
nums = o;
}
double average() {
double sum = 0.0;

for (int i = 0; i < nums.length; i++)


sum += nums[i].doubleValue();
return sum / nums.length;
}
}
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);
}
}
Outputnya :

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 :

interface MinMax<T extends Comparable<T>> {


T min();
T max();
}

class MyClass<T extends Comparable<T>> implements MinMax<T> {


T[] vals;

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;

public class TestGeneric {


public static void printList(List<?> list) {
for (Object elem : list) {
System.out.print(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);
}
}
Outputnya :

Analisa :

5. Listing Program :

class Stats<T extends Number> {


T[] nums; // array of Number or subclass

// Pass the constructor a reference to


// an array of type Number or subclass.
Stats(T[] o) {
nums = o;
}

// Return type double in all cases.


double average() {
double sum = 0.0;

for(int i=0; i < nums.length; i++)


sum += nums[i].doubleValue();
return sum / nums.length;
}

// Determine if two averages are the same.


// Notice the use of the wildcard.
boolean sameAvg(Stats<?> ob) {
if(average() == ob.average())
return true;

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);

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);

// See which arrays have same average.


System.out.print("Averages of iob and dob ");
if(iob.sameAvg(dob))
System.out.println("are the same.");
else
System.out.println("differ.");

System.out.print("Averages of iob and fob ");


if(iob.sameAvg(fob))
System.out.println("are the same.");
else
System.out.println("differ.");
}
}
Outputnya :

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;
}

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));
}
}
Outputnya :

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();
}

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("Contents of tdlocs.");
showXY(tdlocs); // OK, is a TwoD
// showXYZ(tdlocs); // Error, not a ThreeD
// showAll(tdlocs); // Erorr, not a FourD

// Now, create some FourD objects.


FourD fd[] = {
new FourD(1, 2, 3, 4),
new FourD(6, 8, 14, 8),
new FourD(22, 9, 4, 9),
new FourD(3, -2, -23, 17)
};

Coords<FourD> fdlocs = new Coords<FourD>(fd);

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 :

public static void print(List<? extends Number> list) {


for (Number n : list)
System.out.print(n + " ");
System.out.println();
}
Outputnya :

Analisa :

2. Listing Program :

import java.util.*;

public class Latihan2 {


public static <T extends Object & Comparable<? super T>> T max(List<?
extends T> list, int begin, int end) {
T xl=list.get(0);
for(int i=begin;i<=end;i++) {
if(list.get(i).compareTo(xl)>0) {
xl=list.get(i);
}
}
return (T) xl;
}

public static void main(String[] args) {


List<Character> karakter = Arrays.asList('a','b','c','x','y','z');
System.out.println("Nilai Karakter = " + karakter);
System.out.println("Nilai Maks = " + max(karakter, 0,
karakter.size()-1));
}
}
Outputnya :

Analisa :

3. Listing Program :

Outputnya :

Analisa :

5. TUGAS
4. Listing Program :

Outputnya :

Analisa :

6.KESIMPULAN

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy