MODUL 6
INTERFACE
A. Objective
To understand how was java Interface run.
B. Basic Theory
Interface
Interface
is a mechanism that provided by java which able to share constanta or determine
the method shape which can used by some of class. Interface look like an
abstract class, because abstract class also determine the method for subclass.
How to declare an Interface
public interface NamaInterface {
//Method
without implementation
//constanta
declaration
}
C. Tools and
Materials
·
Laptop
·
NetBeans Software
·
Java Development Kit (JDK)
D. Steps
1. Make a new interface
named “IntLampu.java” , write down program code as below
/**
*
*
@author DESY
*/
interface IntLamp {
public static final int KEADAAN_HIDUP = 1;
public static int KEADAAN_MATI = 0;
public abstract void hidupkan();
public abstract void matikan();
}
2. Compile the
program
3. Next, make a
new class named “Lampu.java” which is an implementation of interface
“IntLamp.java”
4. write down
program code as below
/**
*
*
@author DESY
*/
public class Lampu implements IntLamp {
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");
}
}
}
5.
Compile, analyze and described that class
6.
Next, make a new class with main function ( ) that
implemented Lampu.java class, as program code as below
/**
*
*
@author DESY
*/
public class TesInterface {
public static void main (String[]args){
Lampu lampuKamar = new Lampu();
lampuKamar.hidupkan();
lampuKamar.hidupkan();
lampuKamar.matikan();
lampuKamar.matikan();
}
}
7.
Compile and run the program
Exercises
1.
Modified interface IntLamp.java by change the
variable become
/**
*
*
@author DESY
*/
interface IntLamp {
public static final int KEADAAN_HIDUP = 2;
public static final int KEADAAN_REDUP = 1;
public static int KEADAAN_MATI = 0;
public abstract void hidupkan();
public abstract void redupkan();
public abstract void matikan();
}
2.
Modify class “TesInterface.java” so after run
will shown an output below
Lampu class:
/**
*
* @author DESY
*/
public class Lampu implements
IntLamp {
private int statusLampu = 0;
public void hidupkan(){
if(this.statusLampu != KEADAAN_HIDUP){
this.statusLampu = KEADAAN_HIDUP;
System.out.println("Lampu
Hidup");
}
else {
System.out.println("Lampu
Sudah Hidup");
}
}
public void matikan(){
if(this.statusLampu != KEADAAN_MATI){
this.statusLampu = KEADAAN_MATI;
System.out.println("Lampu
Mati");
}
else {
System.out.println("Lampu
Sudah Mati");
}
}
public void redupkan(){
if(this.statusLampu != KEADAAN_REDUP){
this.statusLampu = KEADAAN_REDUP;
System.out.println("Lampu
Redup");
}
else {
System.out.println("Lampu
Sudah Di redupkan");
}
}
}
TesInterface.java class:
/**
*
* @author DESY
*/
public class TesInterface {
public static void main (String[]args){
Lampu lampuKamar = new Lampu();
lampuKamar.hidupkan();
lampuKamar.hidupkan();
lampuKamar.matikan();
lampuKamar.matikan();
lampuKamar.hidupkan();
lampuKamar.redupkan();
lampuKamar.redupkan();
}
}
3. Make the
result analizing into report
Tidak ada komentar:
Posting Komentar