11
LAPORAN PRAKTIKUM PEMROGRAMAN BERORIENTASI OBJEK Dengan Java MODUL KE-6 ABSTRACT CLASS, INTERFACE, DAN EXCEPTION Dosen Pembina : Gita Indah Marthasari, ST Disusun Oleh: Rudi Syaifudin (08560225) LABORATORIUM PEMROGRAMAN PROGRAM STUDI TEKNIK INFORMATIKA FAKULTAS TEKNIK UNIVERSITAS MUHAMMADIYAH MALANG 2009

laporan tgas 6

  • Upload
    fsmtech

  • View
    121

  • Download
    11

Embed Size (px)

Citation preview

Page 1: laporan tgas 6

LAPORAN PRAKTIKUM PEMROGRAMAN BERORIENTASI OBJEK

Dengan JavaMODUL KE-6

ABSTRACT CLASS, INTERFACE, DAN EXCEPTION

Dosen Pembina :Gita Indah Marthasari, ST

Disusun Oleh:Rudi Syaifudin (08560225)

LABORATORIUM PEMROGRAMANPROGRAM STUDI TEKNIK INFORMATIKA

FAKULTAS TEKNIKUNIVERSITAS MUHAMMADIYAH MALANG

2009

Page 2: laporan tgas 6

I. TUJUANTujuan praktikum ini adalah :1. Mahasiswa memahami konsep abstract class, interface, dan penanganan eksepsi.2. Mahasiswa mampu menurunkan dari kelas abstrak dan interface3. Mahasiswa mampu menangani eksepsi yang dapat terjadi pada program

II. DASAR TEORI1. konsep abstract class dan interface dan turunannya

Abstract Class merupakan fungsi yang tidak memiliki implementasi .Contoh : Public void draw(); //tanpa “{ }”

Abstract Class juga tidak bisa di instantiateContoh : abstract class shape {

public void print(){}abstract double getarea();}

class circle extends shape{ //turunan dari shapeprotected double r;double getarea(){}

}

Dalam suatu interface hanya berisi fungsi abstract dan konstanta sajaContoh:Interface animal{ public void herbivora (); public void carnivora (); public void omnivora ();}

Class bertelur implements animal{ //turunan dr animal public void herbivora (){} public void carnivora (){} public void omnivora (){}}

Beberapa interface dapat diturunkan menjadi 1 classClass bertelur implements animal,oviduk,ooviduk{ //turunan dr animal,oviduk,ooviduk}

Page 3: laporan tgas 6

2. Exception Handling Hirarki Exception Class

Try-catch dan Try-catch-finallykeyword try, catch dan finally digunakan dalam menangani bermacam tipe exception. 3 Keyword tersebut digunakan bersama, namun finally bersifat opsional. Akan lebih baik jika memfokuskan pada dua keyword pertama, kemudian membahas finally pada bagian akhir.Syntax :try {//kode yg mungkin terjadi exception} catch (<ExceptionType1> <ObjName>) {//kode penanganan exception}...} catch (<ExceptionTypeN> <ObjName>) {//kode penanganan exception}

Contoh : try{ } catch(RuntimeException re){ System.out.println("RuntimeException : " +re.getMessage()); } catch(NullPointerException npe){ System.out.println("NullPointerException : " +npe.getMessage()); } finally{ System.out.println("enter finally block”); }

Page 4: laporan tgas 6

ThrowDisamping menangkap exception, Java juga mengijinkan seorang user melempar sebuah exception (throw). Fungsi ini digunakan untuk menggganti tampilan exception secara manual.

Syntax :Throw new exception();

Contoh :Try{Throw new RuntimeException(“melempar exception”);}Catch(RuntimeException re){System.out.println(“runtimeexception” +re.getmessage());}

Hasil outputnya :Runtimeexception : Melempar exception

throwsJika sebuah method dapat menyebabkan sebuah exception namun tidak menangkapnya, maka digunakan keyword throws. Aturan ini hanya berlaku pada checked exception.

Syntax :<type> <methodName> (<parameterList>) throws <exceptionList> {<methodBody>}

Contoh :

Static void method1()throws ioexception{Throw new ioexception(“ioexception dari method1”);

}

III. PROSEDUR PRAKTIKUM1. Mahasiswa menerima tugas dari asisten2. Mahasiswa mengerjakan tugas yang diberikan3. Mahasiswa menulis laporan berdasarkan hasil praktikum. Laporan dikerjakan individu

dan dikumpulkan satu minggu setelah praktikum.

Page 5: laporan tgas 6

IV. HASIL PRAKTIKUM1. Buatlah interface Process

Source kode :interface proses { public void createProcess (object x); public void endProcess (); public void viewProcess ();}

Run Program :

Page 6: laporan tgas 6

2. kelas RecruitmentProcess Source kode :public class RecruitmentProcess implements proses{ public void createProcess (object x){} public void endProcess (){} public void viewProcess (){} protected String namaProses; protected int kodeProses;}

Run Program :

Page 7: laporan tgas 6

3. NullPointerException Source kode :

public class main1 { public static void main(String[] args) { proses p; RecruitmentProcess r = new RecruitmentProcess(); p=r; try{ throw new NullPointerException("terjadi NullPointer Exc."); } catch(NullPointerException npe){ System.out.println("NullPointerException : " +npe.getMessage()); } }

}

Run Program :

Page 8: laporan tgas 6

4. MissingResourceException Source kode :

import java.util.MissingResourceException;public class main2 { public static void main(String[] args) { String key="yes"; try{ throw new MissingResourceException("terjadi MissingResourceException Exc.","main2",key); } catch(MissingResourceException mse){ System.out.println("MissingResourceException message : " +mse.getMessage()); System.out.println("MissingResourceException key : " +mse.getKey()); } finally{ System.out.println("ini adalah blok finnally"); } }}

Run Program :

Page 9: laporan tgas 6

5. exception handling Source kode :

public class Main { public static void main (String[] args) { try{

int [] array = {1,2,3,4,5};

for (int i=0; i<=5; i++)System.out.print(array[i]);

} catch(ArrayIndexOutOfBoundsException a){ System.out.println("\nArrayIndexOutOfBoundsException : " +a.getMessage());

}}}

Run Program :

Page 10: laporan tgas 6

6. Analisa Program Source kode :

public class DemoFinally{static void myMethod(int n) throws Exception{

try { switch(n) { case 1: System.out.println("1st case"); return; case 3: System.out.println("3rd case"); throw new RuntimeException("3!"); case 4: System.out.println("4th case"); throw new Exception("4!"); case 2: System.out.println("2nd case"); } } catch (RuntimeException e) { System.out.print("RuntimeException: "); System.out.println(e.getMessage()); } finally { System.out.println("finally-block entered."); } }

public static void main(String args[]){ for (int i=1; i<=4; i++) { try { DemoFinally.myMethod(i); } catch (Exception e){ System.out.print("Exception caught: "); System.out.println(e.getMessage()); } System.out.println(); } }}

Run Program :

Hasil analisa : dalam fungsi myMethod terdapat switch yang didalamnya terdapat beberapa

exception. Case berhenti pada ”case 3” karena didalamnya terdapat Runtimeexception, semudian langsung mencetak finally.

Dalam fungsi main hanya terdapat 1 fungsi exception, sehingga yang dipanggil dari fungsi my method hanya exception dan finally saja.

Page 11: laporan tgas 6

V. KesimpulanDari praktikum yang telah kami laksanakan, kami dapat memahami abstract class, interface, dan

cara kerja exception handling, disamping itu abstract class dan interface dapat diturunkan dengan metode tertentu.