Professional Documents
Culture Documents
INTERFACE
A. OBJECTIVE
Understanding interface as a provided mechanism by java, which allowing to be used by
classes.
B. BASIC THEORY
Interface is similar as class, the difference is interface keyword substituting class
keyword position. Public access determinant is used in order that interface able to be
accessed from any other classes. Suppose there is no public access determinant so classes
located in the same package can access the interface.
C. TOOLS
1. Computer.
2. NetBeans application
D. EXPERIMENT
WORK STEPS
1. Create new interface named as IntLampu.java, write down the following source code:
2.
interface IntLampu {
public static final int KEADAAN_HIDUP=1;
public static final int KEADAAN_MATI=0;
Compi
le the
Solution:
public class TesInterface {
public static void main (String[]arg){
Lampu lampuKamar=new Lampu();
lampuKamar.hidupkan();
lampuKamar.hidupkan();
lampuKamar.matikan();
lampuKamar.matikan();
lampuKamar.hidupkan();
lampuKamar.redupkan();
lampuKamar.redupkan();
}
}
3. Create analyze result.
E. ANALYSIS
In Lampu.java class as implements of IntLampu.java interface there is a variable
(statusLampu) with 0 as default value and two method, those are hidupkan() and matikan().
The hidupkan() method having function to turn on the light, the instruction is to change the
value of statusLampu as the value of KEADAAN_HIDUP or 1 as in intLampu.java
interface if the current value of statusLampu is 0 and to give notification that the lamp has
turned on. And giving notification that the lamp has been turned on already if the statusLampu
current value is 1, and in reverse for matikan() method.
For the exercise it is not so different with the experiment, the differences are that we add a
variable in the IntLampu.java interface (KEADAAN_REDUP) and a method (redupkan()). The
main idea of redupkan() method is similar with two other method, if we call this method while
the lamp is turned on it will reduce the lamp brightness and if we try to reduce the brightness
while the lamp brightness is reduced already it will gives notification if so and nothing is
happened.