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 :




                                                

Tuesday, May 5, 2015

MODUL 5 : ABSTRACT CLASS


  1. Objectives 

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

  1. Theoritical Basis
    Java provides a mecanism that make possible a method determined inside of class but it is not included the definition. This method known as Abstract Method and the class called Abstract Class. Definition of class determmined by the each inherited classes. In this case, each inherited class of Abstract Class must define methods that grouped as Abstract Method.

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

  1. Work Steps
  1. Experiment
  1. Creating a new classs named “Kendaraan.java” then writing the codes below :

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public abstract class Kendaraan {
    protected String nama;
    public abstract void jalankan(); //method abstrack
}
  1. Saving and compiling as the name of the class.
  2. Next, creating a new class named “Sepeda.java” , writing the program codes below :

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class Sepeda extends Kendaraan{
    public Sepeda(String nama){
        this.nama = nama;
    }
    public void jalankan(){
        System.out.println("Duduklah di atas sadel "+ this.nama +" dan kayuhlah !");
    }
}
  1. Saving and compiling as the name of the class.
  2. Then, creating a new class with main( ) method that is implementation of “Sepeda” class. Writing this program codes inside of this class.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class TesAbstrakSepeda {
    public static void main (String []args){
        Sepeda sepedaku = new Sepeda("Sepeda Ontel");
        sepedaku.jalankan();
    }
}
  1. Saving and compiling as the name of the class.
  2. Observing and noting the result


  1. Analysis
    Inside class of “Kendaraan.java” contains an Abstract Method that doesn’t have instructions yet. But inside of the subclass we have made, it is written the Abstract Method with instructions. Well, it works very good and get the result. An Abstract Class known that any subclasses are operations that match with the method but among one class and other classes have different treatments.


  1. Tasks
  1. Creating a new class named “Mobil.java” that is subclass of “Kendaraan” class and creating a new class named “TesAbstractMobil.java” as implementation of “Mobil.java”.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class Mobil extends Kendaraan {
    public Mobil(String nama){
        this.nama = nama;
    }
    public void jalankan(){
        System.out.println("Duduklah di depan dan setirlah " + this.nama + " dan Hidupkan Mesin !");
    }

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class TesAbstrakMobil {
    public static void main (String []args){
        Mobil mobilku = new Mobil("Mobil Tua");
        mobilku.jalankan();
        mobilku.bunyikanTlakson();
    }
}
  1. Following the steps above.
  2. Adding a new method inside class of “Mobil.java”.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class Mobil extends Kendaraan {
    public Mobil(String nama){
        this.nama = nama;
    }
    public void jalankan(){
        System.out.println("Duduklah di depan dan setirlah " + this.nama + " dan Hidupkan Mesin !");
    }
    public void bunyikanTlakson(){
        System.out.println("Bunyikan Tlakson " + this.nama + " saat di Persimpangan !");
    }
}
  1. The result will be like this figure.

Thursday, April 2, 2015

MODUL 4 : INHERITANCE



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

B.                 Theoretical Basis
Inheritance is one of main characacter of PBO / OOP and also this makes easy a programmer to create and manage programs. A java class that inherited from its parent class will inherit all parent class characters.

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

D.                Work Steps
1.      Experiment 1
a.       Changing accessor method of “Bicycle.java” become protected.
Example : protected void speedUp(){
}
b.      Typing program codes below.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class MountainBike extends Bicycle {
    static boolean compas = true;
  
    public static void main (String []args){
        MountainBike mountainBike = new MountainBike();
        mountainBike.speedUp(100);
        mountainBike.changeGear(6);
        mountainBike.changeCadance(9);
        mountainBike.printStates();
        mountainBike.lookCompas();  
    }
   
    void lookCompas(){
        System.out.println("Ada kompasnya ?? "+compas);
    }
}

c.       Saving the file as the name.
d.      Running and viewing the result.


2.      Exercise 1
a.       Creating class named “RoadBicycle.java” that has special variable, it is diskBreak.
Static int diskBreak = 2;

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class RoadBicycle extends Bicycle{
    static int diskBreak = 2;
  
    public static void main (String []args){
        RoadBicycle road = new RoadBicycle();
        road.speedUp(200);
        road.changeGear(12);
        road.changeCadance(18);
        road.printStates();
        road.lookDiskBreak();
    }
        void lookDiskBreak(){
            System.out.println("Berapa diskBreak nya ?? "+ diskBreak);
        }
}
b.      Creating class named “TandemBike.java” that has special variable, it is Sadel.
Static int sadel = 2;

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class TandemBike extends Bicycle{
    static int sadel = 2;
  
    public static void main (String []args){
        TandemBike tandem = new TandemBike();
        tandem.speedUp(200);
        tandem.changeGear(12);
        tandem.changeCadance(18);
        tandem.printStates();
        tandem.lookSadel();
    }
  
    void lookSadel(){
        System.out.println("Berapa sadelnya ?? "+ sadel);
    }
}
c.       Running and viewing the result.


3.      Experiment 2 (Calling Super Class Constructor)
a.       Creating class named “Orang.java” and typing program codes below.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class Orang {
    private String nama;
    private int usia;
  
    public Orang(String nama, int usia){
        this.nama = nama;
        this.usia = usia;
    }
   
    //method
    public void info(){
        System.out.println("Nama : "+nama);
        System.out.println("Usia : "+usia);
       
    }
}
b.      Creating a new class named “Pegawai.java” that it is subclass of “Orang.java”. Then typing program codes below.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class Pegawai extends Orang {
    protected String noPegawai;
   
    public Pegawai(String noPegawai, String nama, int usia){
        super(nama, usia);
        this.noPegawai = noPegawai;
    }
   
    //method
    public void info(){
        System.out.println("No.pegawai : "+this.noPegawai);
        super.info();
    }
}
c.       Then, creating a new class with main() function as program codes below.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class AksesKonstSuperClass {
    public static void main (String []args){
        Pegawai karyawan = new Pegawai("1245", "Mr.Budi", 24);
        karyawan.info();      
    }
}
The result after compiling and running :


4.      Exercise 2
a.       Creating class named “mahasiswaTi.java” that is inherited from class of “Orang.java”. This program should print nama, nim, usia, and konsentrasi.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class mahasiswaTi extends Orang {
    protected String NIM;
    protected String konsentrasi;
  
    public mahasiswaTi(String NIM, String konsentrasi, String nama, int usia ){
        super(nama, usia);
        this.NIM = NIM;
        this.konsentrasi = konsentrasi;
    }
   
    //method
    public void info(){
        System.out.println("NIM Mahasiswa : "+ this.NIM);
        System.out.println("Konsentrasi : " + this.konsentrasi);
        super.info();
       
    }
   
}
b.      Then, creating a new class named “AksesMahasiswaTi.java” that has main() function, as implementation from class of “mahasiswaTi.java”.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class AksesMahasiswaTi {
    public static void main(String []args){
        mahasiswaTi mahasiswa = new mahasiswaTi("L200134020","Teknik Informatika", "Jhonny", 20);
        mahasiswa.info();
    }
}
c.       Result after compiling and running the program.

d.      Analyze the result.

E.                 Analysis
After compiling and running the program, subclasses are able to access some variable of parent class has.  Not only variables but also some methods of parent class, moreover all characters that parent class has. It verify that Inheritance method is implemented in Java.

F.                 Tasks
1.      Is a class created in Java should have at least one constructor ?
No, itsn’t. I can compile and run a program I make below.

package salamums;

/**
 *
 * @author Jhonny
 */
public class SalamUMS {

    public static void main(String[] args) {
        System.out.println("UMS : Wacana Keilmuan dan Keislaman");
        System.out.println("Assalamu'alaikum, Mahasiswa UMS !");
    }
  
}

The result after compiling and running :


2.      Editing and repairing program codes so that students don’t meet error message again.

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class Kucing {
    protected String warna;
    protected int umur;
  
    public Kucing(String warna,int umur){
        this.warna = warna;
        this.umur = umur;
    }
  
    //method
    public void info(){
        System.out.println("Kucing warna : " + warna);
        System.out.println("Kucing umur : " + umur);
    }
}

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class KucingRumahan extends Kucing{
  
    protected int kumis; //panjang kumis, berkurang setiap dirawat
  
    public KucingRumahan(String warna, int umur, int kumis){
        super(warna, umur);
        this.kumis = kumis;             
    }
  
    public void rawatKumis(){
        kumis++;
        System.out.println("Panjang kumis kucing : "+kumis);
        super.info();
    }
}

package Percobaan1;

/**
 *
 * @author Jhonny
 */
public class AksesKucingRumahan {
    public static void main (String []args){
        KucingRumahan cat = new KucingRumahan("Hitam", 3, 5);
        cat.rawatKumis();
    }
}

The result after compiling and running :