Selasa, 12 Mei 2015

Modul 6 Praktikum Object Oriented Programming



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

Selasa, 05 Mei 2015

Modul 5 Object Oriented Programming



MODUL 5
ABSTRACT CLASS

A.   Objective
To understand how was abstract class run.

B.   Basic Theory

Abstract Class
Java provide a mechanism which able a method that determine in the class but not accompany with it definition. This method known as abstract method and it class named abstract class.
          Class definition determined by each inheritance class. In this case, every inheritance class of abstract class must be define methods that classified as an abstract method.

C.   Tools and Materials
·        Laptop
·        NetBeans Software
·        Java Development Kit (JDK)
D.   Steps

1.       Make a new class named “Kendaraan.java” as below

/**
 *
 * @author DESY
 */
public abstract class kendaraan {
    protected String nama;
    public abstract void jalankan();
}

2.       Save with class name and compile
3.       Next, make a new class named “Sepeda.java”, write down program code as below

/**
 *
 * @author DESY
 */
public class Sepeda extends kendaraan {
    public Sepeda (String nama){
        this.nama = nama;
    }
   
    public void jalankan (){
        System.out.println("Duduklah diatas sadel " + " "+this.nama+" dan kayuhlah");
    }
}

4.    Save file as the name of the class and compile
5.    Next, make a new class with main method ( ) that implemented sepeda class. Write down the program code as below

/**
 *
 * @author DESY
 */
public class TesAbstrakSepeda {
    public static void main(String[]args){
        Sepeda sepedaku = new Sepeda("Sepeda Ontel");
        sepedaku.jalankan();
    }
}
6.    Save file as the name of the class and compile
7.    Analyze and write down the result

Exercises
1.    Make a new class named “Mobil.java” which is a subclass of kendaraan and make class “TesA        bstrakMobil.java” as an implementation of class “Mobil.java”

Mobil class:
/**
 *
 * @author DESY
 */
public class Mobil extends kendaraan {
    public Mobil (String nama){
        this.nama = nama;
    }
    public void jalankan (){
        System.out.println("Duduklah didepan Setir"+" "+this.nama+" dan Hidupkan mesin");
    }
    public void bunyikanTlakson(){
        System.out.println("Bunyikan Tlakson"+" "+this.nama+" Saat di persimpangan");
    }
}

TesAbstrakMobil class:

/**
 *
 * @author DESY
 */
public class TesAbstrakMobil {
    public static void main(String []args){
        Mobil mobilku = new Mobil("Mobil Tua");
        mobilku.jalankan();
        mobilku.bunyikanTlakson();
    }
}

2.      Follow that steps
3.      Add one new method in “Mobil.java” class

public void bunyikanTlakson(){
        System.out.println("Bunyikan Tlakson"+" "+this.nama+" Saat di persimpangan");
    }
4.        The result as below

run:
Duduklah didepan Setir Mobil Tua dan Hidupkan mesin
Bunyikan Tlakson Mobil Tua Saat di persimpangan