20
Jan
10

contoh implementasi class pada java (interface, inheritance, polymorphism)


Penggabungan 3 class :

contoh studi kasus biaya pengiriman / pemaketan barang sederahana :

1. class Harga :

package ujianakhirku;

/**
*
* @author elie
*/
public interface  harga {
double biayaPerKg();

}

2. class Paket

package ujianakhirku;

/**
*
* @author elie
*/
public class paket implements harga {
private String nmPengirim;
private String tujuanPaket;
private int beratPaket;
private int biayaPerKg;
private double bayar;

public paket(){

}
public String getNmPengirim(){
return nmPengirim;
}
public void setNmPengirim(String temp){
nmPengirim=temp;
}
public String getTujuanPaket(){
return tujuanPaket;
}
public void setTujuanPaket(String temp){
tujuanPaket=temp;
}
public int getBeratPaket(){
return beratPaket;
}
public void setBeratPaket(int temp){
beratPaket=temp;
}
public double biayaPerKg(){
if(tujuanPaket.equalsIgnoreCase(“Bali”)){
biayaPerKg=10000;
return biayaPerKg;

}
else if(tujuanPaket.equalsIgnoreCase(“Surabaya”)){
biayaPerKg=15000;
return biayaPerKg;
}
else if (tujuanPaket.equalsIgnoreCase(“Jakarta”)){
biayaPerKg=20000;
return biayaPerKg;

}
else
{biayaPerKg=25000;
return biayaPerKg;

}
}
public  double Bayar(){
bayar=beratPaket*biayaPerKg;
return bayar;
}
}

3.class Pengiriman (Main Class)

package ujianakhirku;
import javax.swing.JOptionPane;

/**
*
* @author elie
*/
public class pengiriman extends paket {
public pengiriman(){
}

public static void main(String args[]){
pengiriman elie = new pengiriman();

String nmPengirim=JOptionPane.showInputDialog(“Biaya Pengiriman Paket” +
“\n Inputkan Nama Pengirim “);
elie.setNmPengirim(nmPengirim);
String tujuanPaket=JOptionPane.showInputDialog(“Biaya Pengiriman Paket” +
“\n Inputkan Tujuan paket : Bali | Surabaya | Jakarta | Bandung”);
elie.setTujuanPaket(tujuanPaket);
String br=JOptionPane.showInputDialog(“Biaya Pengiriman Paket” +
“\n Inputkan Barat paket :”);
int beratPaket=Integer.parseInt(br);
elie.setBeratPaket(beratPaket);
elie.biayaPerKg();
elie.Bayar();

JOptionPane.showMessageDialog(null,”Biaya Pengiriman Paket Adalah” +
“\n Nama Pengirirm : “+elie.getNmPengirim()+
“\n Tujuan paket : “+elie.getTujuanPaket()+
“\n Berat paket : “+elie.getBeratPaket()+” Kg”+
“\n Biaya Per Kg : “+elie.biayaPerKg()+
“\n Total Bayar : “+elie.Bayar()+
“\n Terima Kasih….. ^_^ “);

}
}

Output Program :

Selamat mencoba…… ^^ semoga sukses….

Mohon kritik dan sarannya……. kita sama-sama belajar…. 🙂

Tetep smangatz……..!!! ciayoo…….!!!  \(^_^)/


9 Responses to “contoh implementasi class pada java (interface, inheritance, polymorphism)”


  1. 1 Kasuga Kinoshita
    March 13, 2010 at 3:18 am

    Ummm
    saya coba untuk belajar ya…
    Trimakasih atas contoh programnya
    So cool
    dn.nb

  2. 3 abcd
    October 4, 2010 at 4:19 pm

    keren,,keren,,izin copas buat tugas yah kk..
    😀

  3. October 23, 2010 at 6:04 am

    mbak mau tanya, koq saya jalankan nggak bisa ya ???
    mohon bantuannya ya mbak…,,,

  4. April 10, 2011 at 10:39 pm

    Makasih ya atas contoh programnya… aku ikut belajarya…

    Jika Anda ingin bersama2 belajar berwira usaha kunjungi situs web:
    prasoit.blogspot.com

    Terimakasih…

  5. 8 chacha
    July 10, 2012 at 6:20 am

    bisa gga sist, kasi tau mana bagian yg polymorphism, mana yg bagian inheritance? makasi🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow My Twitter

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

My Calendar….

January 2010
M T W T F S S
« Dec   May »
 123
45678910
11121314151617
18192021222324
25262728293031

Chat

””

It’s Me….

Man Jadda Wajada...!!!!

(Siapa Yang Bersungguh-Sungguh Akan Berhasil...!!! )

Translate

function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'id'
}, 'google_translate_element');
}


%d bloggers like this: