Untuk menggunakan banyak class dalam satu program kita bisa menggunakan extends
Pertama, buatlah sebuah class dengan nama manusia
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MIFTAHUL ILMI
*/
import javax.swing.*;
public class manusia {
protected String nama="";
protected String sex="";
protected String tgl="";
protected String alamat="";
public manusia(){
this.nama="";
this.sex="";
this.tgl="";
this.alamat="";
}
public void setnama(){
this.nama="";
}
public void setsex(){
this.sex="";
}
public void settgl(){
this.tgl="";
}
public void setalamat(){
this.alamat="";
}
public String getnama(){
return nama=JOptionPane.showInputDialog("nama : ");
}
public String getsex(){
return sex=JOptionPane.showInputDialog("Jenis kelamin : ");
}
public String gettgl(){
return tgl=JOptionPane.showInputDialog("Tangal Lahir : ");
}
public String getalamat(){
return alamat=JOptionPane.showInputDialog("alamat : ");
}
}
Selanjutnya buat class dengan nama pegawai
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MIFTAHUL ILMI
*/
import javax.swing.*;
public class pegawai extends manusia {
private String nip="";
private String dep="";
private String gaji="";
public pegawai(){
this.nip="";
this.dep="";
this.gaji="";
}
public void setnip(){
this.nip="";
}
public void setgaji(){
this.gaji="";
}
public void setdep(){
this.dep="";
}
public String getnip(){
return nip=JOptionPane.showInputDialog("NIP : ");
}
public String getgaji(){
return gaji=JOptionPane.showInputDialog("Gaji : ");
}
public String getdep(){
return dep=JOptionPane.showInputDialog("Departemen : ");
}
}
Setelah itu buat class pegawai tetap untuk memberi keunggulan daripada pegawai tidak tetap
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MIFTAHUL ILMI
*/
import javax.swing.*;
public class pegawaitetap extends pegawai {
private String gol="";
private String tunj="";
public pegawaitetap(){
this.gol="";
this.tunj="";
}
public void setgol(){
this.gol="";
}
public void settunj(){
this.tunj="";
}
public String getgol(){
return gol=JOptionPane.showInputDialog("Golongan : ");
}
public String gettunj(){
return tunj=JOptionPane.showInputDialog("Tunjangan : ");
}
}
Selanjutnya buat class honor agar kita bisa menentukan honor tiap karyawan
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MIFTAHUL ILMI
*/
import javax.swing.*;
public class honor extends pegawai {
private String shf="";
public honor(){
this.shf="";
}
public void setshf(){
this.shf="";
}
public String getshf(){
return shf="Shift : ";
}
}
dan yang terakhir buatlah class demo
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MIFTAHUL ILMI
*/
import javax.swing.*;
public class demo {
public static void main( String[] args){
pegawaitetap d=new pegawaitetap();
honor r=new honor();
System.out.println("Nama : "+d.getnama());
System.out.println("Jenis Kelamin : "+d.getsex());
System.out.println("Alamat : "+d.getalamat());
System.out.println("NIP : "+d.getnip());
System.out.println("Depatemen : "+d.getdep());
System.out.println("Gaji : "+d.getgaji());
String z= JOptionPane.showInputDialog("Status :\n1.Pegawai Tetap\n2.Pegawai Honor");
int s=Integer.parseInt(z);
switch(s){
case 1 :
System.out.println("Golongan : "+d.getgol());
System.out.println("Tunjangan : "+d.gettunj());
break;
case 2 :
System.out.println("Shift : "+r.getshf());
break;
}
}
}
No comments:
Post a Comment