Inheritance Overiding dan Overloading
untuk membuat proses penggajian dengan Eclipse pertama buatlah class Pegawai sebagai superclass nya.
Berikut script coding nya: Klik Next
package office;
public class pegawai
{
private String nama;
private String golongan;
private double gaji;
private String nip;
public void setNama(String nama)
{
this.nama=nama;
}
public void setNip(String nip)
{
this.nip=nip;
}
public void setGolongan(String golongan)
{
this.golongan=golongan;
switch(golongan.charAt(0))
{
case'1':this.gaji=5000000;
break;
case'2':this.gaji=3000000;
break;
case'3':this.gaji=2000000;
break;
case'4':this.gaji=1000000;
break;
case'5':this.gaji=750000;
break;
}
}
public void setGaji(double gaji)
{
this.gaji=gaji;
}
public String getNama()
{
return nama;
}
public String getNip()
{
return nip;
}
public String getGolongan()
{
return golongan;
}
public double getGaji()
{
return gaji;
}
}
Selanjutnya untuk membuat sub class tentukan class bos/manager karena dalam kepegawaian ada yang namanya Karyawan/staff dan bos/manager
berikut script coding nya:
package office;
public class bos extends pegawai {
private double tunjangan;
private String bagian;
private Staff st[];
public void setTunjangan(double tunjangan)
{
this.tunjangan=tunjangan;
}
public double getTunjangan()
{
return tunjangan;
}
public void setBagian(String bagian)
{
this.bagian=bagian;
}
public String getBagian()
{
return bagian;
}
public void setStaff(Staff st[])
{
this.st=st;
}
public void viewStaff()
{
int i;
System.out.println("----------");
for(i=0;i<st.length;i++)
{
st[i].lihatInfo();
}
System.out.println("----------");
}
public void lihatInfo()
{
System.out.println("bos/manager ");
System.out.println("NIP: "+this.getNip());
System.out.println("Nama: "+this.getNama());
System.out.println("Glgn: "+this.getGolongan());
System.out.println(this.getTunjangan());
System.out.println(this.getGaji());
System.out.println("bagian: "+this.getBagian());
this.viewStaff();
}
public double getGaji()
{
return super.getGaji()+tunjangan;
}
}
kemudian buat lagi sebuah class Staff
package office;
public class Staff extends pegawai{
private int lembur;
private double gajiLembur;
public int lihatInfo;
public void setLembur(int lembur)
{
this.lembur=lembur;
}
public int getLembur()
{
return lembur;
}
public void setGajiLembur(double gajiLembur)
{
this.gajiLembur=gajiLembur;
}
public double getGajiLembur()
{
return gajiLembur;
}
public double getGaji(int lembur,double gajiLembur)
{
return super.getGaji()+lembur+gajiLembur;
}
public double getGaji()
{
return super.getGaji()+lembur+gajiLembur;
}
public void lihatInfo()
{
System.out.println("NIP: "+this.getNip());
System.out.println("Nama: "+this.getNama());
System.out.println("Golongan: "+this.getGolongan());
System.out.println("jmlLembur: "+this.getLembur());
System.out.println(this.getGajiLembur());
System.out.println(this.getGaji());
}
}
Selanjutnya ada class lagi yakni kelas utama:
package office;
public class Utama {
public static void main(String[]args)
{
System.out.println("Program class Bos/manager &Staff");
bos man[]=new bos[2];
Staff staff1[]=new Staff[2];
Staff staff2[]=new Staff[3];
//pembuatanBos
man[0]=new bos();
//rizka
man[0].setNama("Rizka");
man[0].setNip("057");
man[0].setGolongan("3A");
man[0].setTunjangan(5000000);
man[0].setBagian("Sekertaris");
man[1]=new bos();
//kim
man[1].setNama("kim");
man[1].setNip("102");
man[1].setGolongan("1");
man[1].setTunjangan(2500000);
man[1].setBagian("Admin");
//staff susi
staff1[0]=new Staff();
staff1[0].setNama("saripah");
staff1[0].setNip("0004");
staff1[0].setGolongan("1A");
staff1[0].setLembur(10);
staff1[0].setGajiLembur(100000);
staff1[1]=new Staff();
staff1[1].setNama("Parmin");
staff1[1].setNip("00005");
staff1[1].setGolongan("2A");
staff1[1].setLembur(10);
man[0].setStaff(staff1);
//staff kim
staff2[0]=new Staff();
staff2[0].setNama("Paidjo");
staff2[0].setNip("0004");
staff2[0].setGolongan("1");
staff2[0].setLembur(15);
staff2[0].setGajiLembur(100000);
staff2[1]=new Staff();
staff2[1].setNama("Semprul");
staff2[1].setNip("0005");
staff2[1].setGolongan("1");
staff2[1].setLembur(10);
staff2[1].setGajiLembur(100000);
staff2[2]=new Staff();
staff2[2].setNama("sujiyem");
staff2[2].setNip("0006");
staff2[2].setGolongan("1");
staff2[2].setLembur(10);
staff2[2].setGajiLembur(100000);
man[1].setStaff(staff2);
//cetak informasi dari bos+staffnya
man[0].lihatInfo();
man[1].lihatInfo();
}
}
Comments
Post a Comment