INTERFACES
Tujuan
Melengkapi laporan praktikum Pemrograman
Berorientasi Objek modul 5.
Landasan
Teori
Modul praktikum Pemrograman
Berorientasi Objek.
Alat
dan Bahan
1 Unit PC
OS windows
Netbeans
Modul praktikum PBO
Langkah
– Langkah Pratikum
Percobaan
IntLampu.java
package modul6;
/**
*
* @author irwans
*/
interface IntLampu {
public static final int KEADAAN_HIDUP =1;
public static final int KEADAAN_MATI =0;
public abstract void hidupkan();
public abstract void matikan();
}
/**
*
* @author irwans
*/
interface IntLampu {
public static final int KEADAAN_HIDUP =1;
public static final int KEADAAN_MATI =0;
public abstract void hidupkan();
public abstract void matikan();
}
compile seperti gambar dibawah ini :
Lampu.java
package modul6;
import static modul6.IntLampu.KEADAAN_HIDUP;
import static modul6.IntLampu.KEADAAN_MATI;
/**
*
* @author irwans
*/
public class Lampu implements IntLampu {
private int statusLampu = 0;
public void hidupkan(){
if(this.statusLampu == KEADAAN_MATI){
this.statusLampu = KEADAAN_HIDUP;
System.out.println("Lampu Hidup");
}
else{
System.out.println("Lampu Sudah Hidup");
}
}
public void matikan(){
if(this.statusLampu == KEADAAN_HIDUP){
this.statusLampu = KEADAAN_MATI;
System.out.println("Lampu Mati");
}
else {
System.out.println("Lampu sudah mati");
}
}
}
import static modul6.IntLampu.KEADAAN_HIDUP;
import static modul6.IntLampu.KEADAAN_MATI;
/**
*
* @author irwans
*/
public class Lampu implements IntLampu {
private int statusLampu = 0;
public void hidupkan(){
if(this.statusLampu == KEADAAN_MATI){
this.statusLampu = KEADAAN_HIDUP;
System.out.println("Lampu Hidup");
}
else{
System.out.println("Lampu Sudah Hidup");
}
}
public void matikan(){
if(this.statusLampu == KEADAAN_HIDUP){
this.statusLampu = KEADAAN_MATI;
System.out.println("Lampu Mati");
}
else {
System.out.println("Lampu sudah mati");
}
}
}
hasil compile seperti gambar dibawah ini :
TesInterface.java
package modul6;
/**
*
* @author irwans
*/
public class TesInterface {
public static void main (String[]args){
Lampu lampuKamar = new Lampu();
lampuKamar.hidupkan();
lampuKamar.hidupkan();
lampuKamar.matikan();
lampuKamar.matikan();
}
}
/**
*
* @author irwans
*/
public class TesInterface {
public static void main (String[]args){
Lampu lampuKamar = new Lampu();
lampuKamar.hidupkan();
lampuKamar.hidupkan();
lampuKamar.matikan();
lampuKamar.matikan();
}
}
hasil compile seperti gambar dibawah ini :
Latihan
IntLampu.java
package modul6.tugas;
/**
*
* @author irwans
*/
interface IntLampu {
public static final int KEADAAN_HIDUP =2;
public static final int KEADAAN_REDUP =1;
public static final int KEADAAN_MATI =0;
public abstract void hidupkan();
public abstract void matikan();
public abstract void redupkan();
}
/**
*
* @author irwans
*/
interface IntLampu {
public static final int KEADAAN_HIDUP =2;
public static final int KEADAAN_REDUP =1;
public static final int KEADAAN_MATI =0;
public abstract void hidupkan();
public abstract void matikan();
public abstract void redupkan();
}
TestInterface.java
package modul6.tugas;
/**
*
* @author irwans
*/
public class TestInterface {
public static void main (String[]args){
Lampu lampuKamar = new Lampu();
lampuKamar.hidupkan();
lampuKamar.hidupkan();
lampuKamar.matikan();
lampuKamar.matikan();
lampuKamar.hidupkan();
lampuKamar.redupkan();
lampuKamar.redupkan();
}
}
/**
*
* @author irwans
*/
public class TestInterface {
public static void main (String[]args){
Lampu lampuKamar = new Lampu();
lampuKamar.hidupkan();
lampuKamar.hidupkan();
lampuKamar.matikan();
lampuKamar.matikan();
lampuKamar.hidupkan();
lampuKamar.redupkan();
lampuKamar.redupkan();
}
}
hasil compile seperti gambar dibawah ini :