1 / 11

BAHASA PEMROGRAMAN

BAHASA PEMROGRAMAN. - PERTEMUAN 9 - BERBAGAI KELAS UTILITAS DI JAVA. Berbagai Kelas Utilitas di Java. Kelas Random Kelas Vector Kelas Stack Kelas Queue Kelas Hastable. Kelas Random. Kelas Random terdapat pada paket Java.util .

tonya
Download Presentation

BAHASA PEMROGRAMAN

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. BAHASA PEMROGRAMAN - PERTEMUAN 9 - BERBAGAI KELAS UTILITAS DI JAVA

  2. BerbagaiKelasUtilitasdi Java • Kelas Random • Kelas Vector • Kelas Stack • Kelas Queue • KelasHastable

  3. Kelas Random • KelasRandomterdapatpadapaketJava.util. • Kelasinimerupakankelas yang memungkinkanpembangkitanbilanganacak. • Berbedadenganfungsi random padakelas Math, kelas Random memungkinkanAndamemberikanbijiygdigunakanuntukmenentukanbilanganacak yang bersifatstatis; dalamartianhasilnyaakanberupanilai yang samasetiap kali program dijalankan.

  4. ContohRandom.java • import java.util.Random; • public class ContohRandom { • public static void main(String[] args) { • byte acak[] = new byte[5]; • //Membangkitkanbilanganacakmenurutwaktu • Random rnd1 = new Random(); • rnd1.nextBytes(acak); • System.out.println("Random menurutWaktu : "); • for(inti=0;i<acak.length;i++) { • System.out.println((i+1)+". " + acak[i]); • } • //Membangkitkanbilanganacakmenurutbiji • Random rnd2 = new Random(5); • rnd2.nextBytes(acak); • System.out.println("Random denganbiji : "); • for(inti=0;i<acak.length;i++) { • System.out.println((i+1)+". " + acak[i]); • } • } • }

  5. Kelas Vector • Kelas Vector merupakankelas yang beradadalampaketJava.util. • Kelasinimemungkinkanpengimplementasianarray yang ukurannyadapatdiubahsewaktu-waktu.

  6. SejumlahMetodepadaKelas Vector • addElement(Object obj) • capacity() • elementAt(intindeks) • firstElement() • lastElement() • indexOf(Object obj) • indexOf(Object elem, intindeks) • insertElementAt(Object obj, intindeks) • removeElement(Object elem) • size()

  7. Vector1.java • import java.util.Vector; • import java.util.Enumeration; • //Kelas Enumeration adalah interface yang menyediakan 2 buahmetodeyaituhasMoreElements() dannextElement() • public class Vector1 { • public static void main(String[] args) { • Vector kota = new Vector(); • kota.addElement("Bandung"); • kota.addElement("Surabaya"); • kota.addElement("Jakarta"); • kota.addElement("Medan"); • Enumeration e = kota.elements(); • while (e.hasMoreElements()) • System.out.println(e.nextElement()); • } • }

  8. Vector2.java import java.util.Vector; public class Vector2 { public static void main(String[] a) { Vector kota = new Vector(); // Penambahan Kota secaraberurutan kota.addElement("Bandung"); kota.addElement("Surabaya"); kota.addElement("Jakarta"); kota.addElement("Medan"); tampilVektor("addElement", kota); //Penambahanpadasembarangtempat kota.insertElementAt("Kudus", 1); tampilVektor("insertElement", kota); //Penghapusansebuahelemen kota.removeElement("Bandung"); tampilVektor("removeElement : Bandung", kota); //Hapussemuaelemen kota.removeAllElements(); tampilVektor("removeAllElement", kota); } static void tampilVektor(String info, Vector v) { System.out.println(info); System.out.println("=============="); if(v.isEmpty()) { System.out.println("VektorKosong"); } else { System.out.println("IsiVektor"); for(inti=0;i<v.size();i++) { System.out.println(v.elementAt(i)); } System.out.println("=============="); } } }

  9. Kelas Stack • Kelas Stack merupakanbagiandarikelasJava.util. • Kelasinibisadigunakanuntukmembuatstruktur data yang dinamakan“tumpukan”. • Contoh : • Tumpukanpiring • Tumpukanbuku • Implementasikelas Stack sebenarnyaditurunkandarikelas Vector. • Itulahsebabnyasemuametodepadakelas Vector diwarisioleh Stack.

  10. TestStack.java • import java.util.*; // for ArrayList<E> • public class TestStack{ • public static void main (String[] args) // for demonstration only • { • Stack<String> s = new Stack<String>(); • // push five Student references onto s • s.push("bagus"); • s.push("Darla"); • s.push("spanky"); • s.pop(); • s.push("Aji"); • s.push("Rudi"); • s.pop(); • System.out.println("The last name pushed was " + s.peek()); • System.out.println(); • System.out.println("The names in reverse order are:"); • while(!s.empty()) • System.out.println(s.pop()); • System.out.println(); • System.out.println("The size of the stack is now " + s.size()); • } • }

  11. TestQueue.java import java.util.LinkedList; import java.util.Queue; public class TestQueue { public void queueExample() { Queue queue = new LinkedList();. queue.add("item1"); queue.add("item2"); queue.offer("Item3"); queue.offer("Item4"); System.out.println("remove: " + queue.remove()); System.out.println("element: " + queue.element()); System.out.println("poll: " + queue.poll()); System.out.println("peek: " + queue.peek()); } /** * @paramargs the command line arguments */ public static void main(String[] args) { new TestQueue().queueExample(); } }

More Related