Dalam pertemuan sebelumnya, materi yang diberikan berupa Grouping Object dan Sifat Object. Sebelum itu, saya akan menjelaskan apa itu Java Collection. Java Collections adalah framework yang menyediakan sebuah tempat untuk menyimpan dan memanipulasi sekumpulan objek. Adapun kegiatan yang dapat dilakukan pada sebuah data seperti pencarian, pengurutan, memasukkan, menghapus dapat dilakukan oleh Java Collections. Java Collection secara sederhana merupakan sebuah objek. Kerangka kerja Java Collection menyediakan dalam bentuk interfaces(Set, List, Queue, Deque dll) dan untuk class (ArrayList, Vector, LingkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet dll). Dalam blog ini, saya akan mencoba beberapa source code yang diberikan oleh Blog pemateri.
ArrayList
ArrayList merupakan implementasi dari interface list. contoh source code yang dipakai untuk collection ArrayList adalah sebagai berikut :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | import java.util.*;
public class ArrayListContoh
{
public static void main(String[] args)
{
ArrayList<String> al=new ArrayList<String>();
al.add("Ahmad");
al.add("Budi");
al.add("Ahmad");
al.add("Dora");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
|
Output :
Untuk Iterasi elemen, dapat juga menggunakan For-Each Loop. Seperti berikut :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | import java.util.ArrayList;
public class ArrayListFor
{
public static void main(String[] args)
{
ArrayList<String> al=new ArrayList<String>();
al.add("Ahmad");
al.add("Budi");
al.add("Cecept");
al.add("Dora");
for(String obj:al)
{
System.out.println(obj);
}
}
}
|
Output :
Contoh program ArrayList dan Class Data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 | import java.util.*;
public class ArrayListClass
{
public static void main(String[] args)
{
Mahasiswa mhs1 = new Mahasiswa ("K121","Adam Kamil",67,69,91);
Mahasiswa mhs1 = new Mahasiswa ("K122","Budi Ridho",57,89,93);
Mahasiswa mhs1 = new Mahasiswa ("K123","Cecep Manayur",77,79,80);
Mahasiswa mhs1 = new Mahasiswa ("K124","Dadang Habibie",67,79,60);
Mahasiswa mhs1 = new Mahasiswa ("K125","Evi Julita",89,79,95);
ArrayList<Mahasiswa> al=new ArrayList<Mahasiswa>();
Iterator itr=al.iterator();
while(itr.hasNext())
{
Mahasiswa tampungMhs = (Mahasiswa)itr.next();
System.out.print(tampungMhs.getNim() +" ");
System.out.print(tampungMhs.getNama() +" ");
System.out.print(tampungMhs.getTugas() +" ");
System.out.print(tampungMhs.getUts() +" ");
System.out.print(tampungMhs.getUas() +" ");
}
}
}
|
Contoh kode program menggabungkan ArrayList:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListGabung
{
public static void main(String[] args)
{
ArrayList<String> al=new ArrayList<String>();
al.add("Ahmad");
al.add("Budi");
al.add("Cecep");
al.add("Dora");
ArrayList<String> al2=new ArrayList<String>();
al2.add("Endo");
al2.add("Fany");
//digabungkan
al.addAll(al2);
Iterator itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
|
Output :
Contoh penggunaan remove all pada ArrayList :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListRemoveAll
{
public static void main(String[] args)
{
ArrayList<String> al=new ArrayList<String>();
al.add("Ahmad");
al.add("Budi");
al.add("Cecep");
al.add("Dora");
ArrayList<String> al2=new ArrayList<String>();
al2.add("Endo");
al2.add("Dora");
al.removeAll(al2);
Iterator itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
|
Contoh penggunaan retailAll pada ArrayList :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 | import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListRetain
{
public static void main(String[] args)
{
ArrayList<String> al=new ArrayList<String>();
al.add("Ahmad");
al.add("Budi");
al.add("Cecep");
al.add("Dora");
ArrayList<String> al2=new ArrayList<String>();
al2.add("Endo");
al2.add("Dora");
al.retainAll(al2);
Iterator itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
|
LinkedList
Contoh Source codenya adalah sebagai berikut :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | import java.util.*;
public class LinkedListContoh
{
public static void main(String args[])
{
LinkedList<String> al=new LinkedList<String>();
al.add("Zaza");
al.add("Yadi");
al.add("Mutiara");
al.add("Santoso");
Iterator<String> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
|
HashSet
Contoh source code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | import java.util.*;
public class HashSetContoh
{
public static void main(String args[])
{
HashSet<String> al=new HashSet<String>();
al.add("Anto");
al.add("Budi");
al.add("Cecep");
al.add("Dani");
al.add("Eka");
Iterator<String> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
|
Contoh LinkedHashSet :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | import java.util.Iterator;
import java.util.LinkedHashSet;
public class LinkedHashSetContoh
{
public static void main(String args[])
{
LinkedHashSet<String> al=new LinkedHashSet<String>();
al.add("Anto");
al.add("Budi");
al.add("Cecep");
al.add("Dani");
al.add("Eka");
Iterator<String> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
|
Contoh HashMap :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | import java.util.*;
public class HashMapContoh
{
public static void main(String args[])
{
HashMap<Integer, String> hm=new HashMap<Integer, String>();
hm.put(100, "Kano");
hm.put(101, "Lalia");
hm.put(102, "Badu");
hm.put(null, "Johan");
for(Map.Entry m:hm.entrySet())
{
System.out.println(m.getKey() +" "+m.getValue());
}
}
}
|
Contoh HashTable :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | import java.util.*;
public class HashTableContoh
{
public static void main(String args[])
{
Hashtable<Integer, String> hm=new Hashtable<Integer, String>();
hm.put(100, "Andri");
hm.put(102, "Roni");
hm.put(101, "Fahrul");
hm.put(103, "Badu");
for(Map.Entry m:hm.entrySet())
{
System.out.println(m.getKey() +" "+m.getValue());
}
}
}
|
Auction Project
Untuk tugas kedua, yaitu auction project. Bentuk modularnya adalah sebagai berikut :
Source code masing masing class adalah :
Auction:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 | import java.util.ArrayList;
/**
* A simple model of an auction.
* The auction maintains a list of lots of arbitrary length.
*
* @author David J. Barnes and Michael Kölling.
* @version 2011.07.31
*/
public class Auction
{
// The list of Lots in this auction.
private ArrayList<Lot> lots;
// The number that will be given to the next lot entered
// into this auction.
private int nextLotNumber;
/**
* Create a new auction.
*/
public Auction()
{
lots = new ArrayList<Lot>();
nextLotNumber = 1;
}
/**
* Enter a new lot into the auction.
* @param description A description of the lot.
*/
public void enterLot(String description)
{
lots.add(new Lot(nextLotNumber, description));
nextLotNumber++;
}
/**
* Show the full list of lots in this auction.
*/
public void showLots()
{
for(Lot lot : lots) {
System.out.println(lot.toString());
}
}
/**
* Make a bid for a lot.
* A message is printed indicating whether the bid is
* successful or not.
*
* @param lotNumber The lot being bid for.
* @param bidder The person bidding for the lot.
* @param value The value of the bid.
*/
public void makeABid(int lotNumber, Person bidder, long value)
{
Lot selectedLot = getLot(lotNumber);
if(selectedLot != null) {
boolean successful = selectedLot.bidFor(new Bid(bidder, value));
if(successful) {
System.out.println("The bid for lot number " +
lotNumber + " was successful.");
}
else {
// Report which bid is higher.
Bid highestBid = selectedLot.getHighestBid();
System.out.println("Lot number: " + lotNumber +
" already has a bid of: " +
highestBid.getValue());
}
}
}
/**
* Return the lot with the given number. Return null
* if a lot with this number does not exist.
* @param lotNumber The number of the lot to return.
*/
public Lot getLot(int lotNumber)
{
if((lotNumber >= 1) && (lotNumber < nextLotNumber)) {
// The number seems to be reasonable.
Lot selectedLot = lots.get(lotNumber - 1);
// Include a confidence check to be sure we have the
// right lot.
if(selectedLot.getNumber() != lotNumber) {
System.out.println("Internal error: Lot number " +
selectedLot.getNumber() +
" was returned instead of " +
lotNumber);
// Don't return an invalid lot.
selectedLot = null;
}
return selectedLot;
}
else {
System.out.println("Lot number: " + lotNumber +
" does not exist.");
return null;
}
}
/**
* Close an auction. Print the final status of each lot.
*/
public void close() {
System.out.println("Closing auction.");
for(Lot lot : lots) {
System.out.print(lot.getNumber() + ": " + lot.getDescription());
if ( lot.getHighestBid() == null ) {
System.out.println(" (No bids)");
}
else {
Bid highestBid = lot.getHighestBid();
System.out.println(" sold to " + highestBid.getBidder().getName() + " for " + highestBid.getValue());
}
}
}
}
|
Lot:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 | /**
* A class to model an item (or set of items) in an
* auction: a lot.
*
* @author David J. Barnes and Michael Kölling.
* @version 2011.07.31
*/
public class Lot
{
// A unique identifying number.
private final int number;
// A description of the lot.
private String description;
// The current highest bid for this lot.
private Bid highestBid;
/**
* Construct a Lot, setting its number and description.
* @param number The lot number.
* @param description A description of this lot.
*/
public Lot(int number, String description)
{
this.number = number;
this.description = description;
this.highestBid = null;
}
/**
* Attempt to bid for this lot. A successful bid
* must have a value higher than any existing bid.
* @param bid A new bid.
* @return true if successful, false otherwise
*/
public boolean bidFor(Bid bid)
{
if(highestBid == null) {
// There is no previous bid.
highestBid = bid;
return true;
}
else if(bid.getValue() > highestBid.getValue()) {
// The bid is better than the previous one.
highestBid = bid;
return true;
}
else {
// The bid is not better.
return false;
}
}
/**
* @return A string representation of this lot's details.
*/
public String toString()
{
String details = number + ": " + description;
if(highestBid != null) {
details += " Bid: " +
highestBid.getValue();
}
else {
details += " (No bid)";
}
return details;
}
/**
* @return The lot's number.
*/
public int getNumber()
{
return number;
}
/**
* @return The lot's description.
*/
public String getDescription()
{
return description;
}
/**
* @return The highest bid for this lot.
* This could be null if there is
* no current bid.
*/
public Bid getHighestBid()
{
return highestBid;
}
}
|
Bid:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 | /**
* A class that models an auction bid.
* It contains a reference to the Person bidding and the amount bid.
*
* @author David J. Barnes and Michael Kölling.
* @version 2011.07.31
*/
public class Bid
{
// The person making the bid.
private final Person bidder;
// The value of the bid. This could be a large number so
// the long type has been used.
private final long value;
/**
* Create a bid.
* @param bidder Who is bidding for the lot.
* @param value The value of the bid.
*/
public Bid(Person bidder, long value)
{
this.bidder = bidder;
this.value = value;
}
/**
* @return The bidder.
*/
public Person getBidder()
{
return bidder;
}
/**
* @return The value of the bid.
*/
public long getValue()
{
return value;
}
}
|
Person:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | /**
* Maintain details of someone who participates in an auction.
* @author David J. Barnes and Michael Kölling.
* @version 2011.07.31
*/
public class Person
{
// The name of this person.
private final String name;
/**
* Create a new person with the given name.
* @param name The person's name.
*/
public Person(String name)
{
this.name = name;
}
/**
* @return The person's name.
*/
public String getName()
{
return name;
}
}
|
Cara untuk memulai program adalah dengan menjalankan object Auction. Masukkan nama nama barang menggunakan fungsi enterLot. Nama barang yang dimasukkan harus berupa string dengan menggunakan petik dua (""), contoh "Meja". Isi nama orang di object Person sebagai bidder. Tamplikan lots dan tentukan barang yang akan di bid. Masukkan bid menggunakan fungsi makeABid di dalam object Auction. Masukkan nomor lot, nama orang sebagai bidder, dan harga bidnya.
Sekian dari saya, Terima Kasih.
Comments
Post a Comment