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 :

No comments:

Post a Comment