Monday, May 11, 2015

MODUL 6 : INTERFACE

A.     Objectives
Objectives of making this report are :
1.      Students are able to understand what the definition of Interface in Java.
2.      Students are able to implement Interface in this practical laboratory.

B.      Theoritical Basis
Interface is a mechanism that provided by Java so that possible to share constant or determine form of method that able used by a number of classes. A glance, Interface is like Abstract class because Abstract class also determine method for subclasses.

C.      Tool and Material
1.      Laptop
2.      Java Development Kit (JDK 8)
3.      NetBeans IDE 8.0.2

D.     Work Steps
1.      Experiment
a.      Creating a new interface named “IntLampu.java” then writing the codes below :

package Percobaan1;

/**
 *
 * @author Jhonny
 */
interface IntLampu {
    public static final int KEADAAN_HIDUP = 1;
    public static final int KEADAAN_MATI = 0;
   
    public abstract void hidupkan();
    public abstract void matikan();
   
}

b.      Compiling the program.
c.  Then, creating a new class named “Lampu.java” that is implementation of “IntLampu.java” interface.

d.      Writing program codes below :

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class Lampu implements IntLampu {
    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");
        }
    }
}

e.      Compiling, analyzing, describing the class above.
f.        Creating a new class with main() as class implementation of “Lampu.java” class, as this codes :

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class TesInterface {
    public static void main (String []args){
        Lampu lampuKamar = new Lampu();
        lampuKamar.hidupkan();
        lampuKamar.hidupkan();
        lampuKamar.matikan();
        lampuKamar.matikan();
    }
}

g.      Compiling and running the program.

E.      Analysis
Interfaces is almost same with class, the difference is about keyword of interface replace keyword of Class. Public access determiner usually used so that able to be accessed by any classes. If there is not public access determiner, so interface is just able to accessed by some classes that inside of the same package. If interface is defined as public so it should be saved with same first name and with interface name. 

F.       Tasks
1.      Modif interface of “IntLampu.java” above by changing final variable become :

 public static final int KEADAAN_HIDUP = 2;
 public static final int KEADAAN_REDUP = 1;
 public static final int KEADAAN_MATI = 0

Also adding redupkan() method :

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class Lampu implements IntLampu {
    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 redupkan(){
        if (this.statusLampu != KEADAAN_REDUP){
            this.statusLampu = KEADAAN_REDUP;
            System.out.println("Lampu Redup");
        }
        else{
            System.out.println("Lampu Sudah Diredupkan");
        }
    }
   
    public void matikan(){
        if (this.statusLampu != KEADAAN_MATI){
            this.statusLampu = KEADAAN_MATI;
            System.out.println("Lampu Mati");
        }
        else{
            System.out.println("Lampu Sudah Mati");
        }
    }
}

2.      Modif class of “TestInterface.java”  :

package Percobaan1;

/**
 *
 * @author Jhonny
 */
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();
    }
}

So that when the program is running, it will result the image below :




                                                

No comments:

Post a Comment