IKG2I4 / Software Project I Mahmud Imrona, M.T. Izzatul Ummah, M.T. Kelompok Keahlian Algoritma dan Komputasi LECTURE NOTE WEEK 7 1 3/11/2015 WEEK 7 Java Library, JAVA API DOCS (java.lang, java.util, Java Collection) Java IO Stream & File IO Materi pendukung lainnya: Wrapper, Type Casting, Package, JAR, JAVADOC 2 3/11/2015 IKG2I4 – Software Project I JAVA LIBRARY Beberapa library utama: – java.lang: Math (misalnya Math.PI, Math.SQRT) – java.io: Input/Output – java.util: Collection (struktur data yang sudah tersedia) – Javax.swing: untuk membuat GUI Swing – Javax.sql: untuk koneksi dan query ke database 3 3/11/2015 IKG2I4 – Software Project I MATH Contoh penggunaan Library Math: public static void main(String args[]) { System.out.println("E = " + Math.E); System.out.println("PI = " + Math.PI); double pi = Math.PI; System.out.println("sin 90 = " + Math.sin(pi/2)); System.out.println("cos 90 = " + Math.cos(pi/2)); System.out.println("tan 90 = " + Math.tan(pi/2)); System.out.println("Konversi dari degree ke radian.......90 deg = " + Math.toRadians(90) + " rad."); System.out.println("Konversi dari radian ke degree.......PI/2 rad = " + Math.toDegrees(pi/2) + " deg."); System.out.println("10 log 10000 = " + Math.log10(10000)); 4 3/11/2015 IKG2I4 – Software Project I MATH Contoh penggunaan Library Math: int x = 800; int y = 50; System.out.println("max(x,y) = " + Math.max(x,y)); System.out.println("min(x,y) = " + Math.min(x,y)); double z = 5.6; System.out.println("Pembulatan (round) dari z = " + Math.round(z)); System.out.println("Membangkitkan bilangan random antara 0 dan 1 = " + Math.random()); } } 5 3/11/2015 IKG2I4 – Software Project I JAVA DATE import java.util.Date; class CobaDate { public static void main(String args[]) { System.out.println("Latihan menggunakan library class Date!"); Date tgl = new Date(2012,12,24); Date tgl2 = new Date(2013,1,10); System.out.println("Apakah tgl sebelum tgl2 ? " + tgl.before(tgl2)); System.out.println("Apakah tgl setelah tgl2 ? " + tgl.after(tgl2)); System.out.println("Selisih tgl dan tgl2 = " + tgl.compareTo(tgl2)); System.out.println("Apakah tgl = tgl2 ? " + tgl.equals(tgl2)); } } 6 3/11/2015 IKG2I4 – Software Project I RANDOM NUMBER GENERATOR import java.util.Random; class CobaRandom { public static void main(String args[]) { Random r = new Random(); System.out.println(r.nextInt()); System.out.println(r.nextInt(15000)); System.out.println(r.nextLong()); System.out.println(r.nextDouble()); System.out.println(r.nextFloat()); System.out.println(r.nextBoolean()); } } 7 3/11/2015 IKG2I4 – Software Project I COLLECTION Collection adalah struktur data yang sudah disediakan oleh Java meliputi stack, queue, linkedlist, vector, dsb. Ada tiga jenis Collection: – Ordered List – Dictionaries – Set 8 3/11/2015 IKG2I4 – Software Project I COLLECTION Karena array yg biasa kita pakai masih statis, harus dialokasi size-nya di awal: – String[] arrString = new String[50] – Mahasiswa[] arrMhs = new Mahasiswa[50] Collection adalah penampung (array) yang bersifat dinamis – Size-nya bisa mengembang/menyusut otomatis tergantung pemakaian saat runtime 9 3/11/2015 IKG2I4 – Software Project I COLLECTION: ARRAYLIST Contoh ArrayList: ArrayList<String> arrlistMhs = new ArrayList<String>(20); // initial capacity 20 (bila tidak diisi, defaultnya 10) arrlistMhs.add("Naruto"); arrlistMhs.add("Sakura"); arrlistMhs.add("Sasuke"); 10 3/11/2015 IKG2I4 – Software Project I COLLECTION: VECTOR Contoh Vector: Vector<String> vectorMhs = new Vector<String>(20); // initial capacity 20 (bila tidak diisi, defaultnya 10) vectorMhs.add("Naruto"); vectorMhs.add("Sakura"); vectorMhs.add("Sasuke"); 11 3/11/2015 IKG2I4 – Software Project I COLLECTION: LINKEDLIST Contoh LinkedList: LinkedList<String> listMhs = new LinkedList<String>(); listMhs.addFirst("1-Naruto"); listMhs.addLast("2-Sakura"); listMhs.addFirst("3-Sasuke"); listMhs.addFirst("4-Doraemon"); listMhs.addLast("5-Nobita"); 12 3/11/2015 IKG2I4 – Software Project I COLLECTION: STACK Contoh Stack: Stack<String> stackMhs = new Stack<String>(); stackMhs.push("1-Naruto"); stackMhs.push("2-Sakura"); stackMhs.push("3-Sasuke"); 13 3/11/2015 IKG2I4 – Software Project I COLLECTION: PRIORITYQUEUE Contoh PriorityQueue: PriorityQueue<String> queueMhs = new PriorityQueue<String>(); queueMhs.add("1-Naruto"); queueMhs.add("2-Sakura"); queueMhs.add("3-Sasuke"); queueMhs.add("4-Doraemon"); queueMhs.add("5-Nobita"); 14 3/11/2015 IKG2I4 – Software Project I COLLECTION: HASHMAP Contoh HashMap: HashMap<Integer,String> hmapMhs = new HashMap<Integer,String>(20); // initial capacity 20 (bila tidak diisi, defaultnya 10) hmapMhs.put(118,"Naruto"); hmapMhs.put(119,"Sakura"); hmapMhs.put(120,"Sasuke"); 15 3/11/2015 IKG2I4 – Software Project I COLLECTION: HASHSET Contoh HashSet: HashSet<String> hsetMhs = new HashSet<String>(20); // initial capacity 20 (bila tidak diisi, defaultnya 10) hsetMhs.add("Naruto"); hsetMhs.add("Sakura"); hsetMhs.add("Sasuke"); 16 3/11/2015 IKG2I4 – Software Project I JAVA IO STREAM & FILE IO Java menyediakan cara muda untuk: – Menampilkan (OUTPUT) sesuatu ke layar Java menyediakan banyak cara untuk: – Menerima masukan (INPUT) dari keyboard – Membaca file (READ) – Menulis file (WRITE) 17 3/11/2015 IKG2I4 – Software Project I JAVA IO STREAM import java.util.Scanner; //utility class Driver { public static void main(String[] args) { Scanner objScanner = new Scanner(System.in); String nama,alamat; int umur; float x; System.out.print("Anjeun saha namina? "); nama = objScanner.nextLine(); // meminta masukan dari keyboard System.out.print("Umur berapa? "); umur = objScanner.nextInt(); // meminta masukan dari keyboard 18 3/11/2015 IKG2I4 – Software Project I JAVA IO STREAM System.out.print("Masukkan angka float "); x = objScanner.nextFloat(); // meminta masukan dari keyboard System.out.println("Halo "+nama); System.out.println("Umur "+umur+" tahun!"); System.out.println("Angka yang Anda masukkan = "+x); } } 19 3/11/2015 IKG2I4 – Software Project I FILE IO: READ class Driver { public static void main(String[] args) { try { FileReader fr = new FileReader("bacajava.txt"); BufferedReader br = new BufferedReader(fr); String baca = br.readLine(); System.out.println(baca); baca = br.readLine(); System.out.println(baca); baca = br.readLine(); System.out.println(baca); br.close(); } catch (FileNotFoundException fnfe) { } catch (IOException ioe) { } } } 3/11/2015 20 IKG2I4 – Software Project I FILE IO: READ class Driver { public static void main(String[] args) { try { byte[] arrByte = new byte[10]; FileInputStream fis = new FileInputStream("bacajava.txt"); fis.read(arrByte,0,10); String baca = new String(arrByte); System.out.println(baca); } catch (IOException ioe) { } } } 21 3/11/2015 IKG2I4 – Software Project I FILE IO: WRITE class Driver { public static void main(String[] args) { try { // ciptakan dulu filenya secara fisik FileOutputStream fos = new FileOutputStream("tulisjava.txt"); byte[] arrByte = “Latihan menulis ke file".getBytes(); fos.write(arrByte); } catch (FileNotFoundException fnfe) { } catch (IOException ioe) { } } } 22 3/11/2015 IKG2I4 – Software Project I FILE IO: WRITE class Driver { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("tulisjava.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); byte[] arrByte = "Mau nulis ke file nih".getBytes(); bos.write(arrByte); bos.close(); } catch (FileNotFoundException fnfe) { } catch (IOException ioe) { } } } 23 3/11/2015 IKG2I4 – Software Project I FILE IO: WRITE class Driver { public static void main(String[] args) { try { FileOutputStream fos = new FileOutputStream("tulisjava.txt"); PrintWriter pw = new PrintWriter(fos); pw.println("Practice makes perfect!"); pw.println("No pain no gain!"); pw.println("Berdoa dan berusaha!"); pw.println("Fight for your dream and make it true!"); pw.close(); } catch (FileNotFoundException fnfe) { } catch (IOException ioe){ } } } 24 3/11/2015 IKG2I4 – Software Project I THANK YOU