Archive for April 2015

MODUL_4 PBO :INHERITANCE

Selasa, 07 April 2015
Posted by Octo The Azura


A.    Objective
Students are understand about inheritance in java

B.     Basic Teory
Inheritance is one of the main characters from object-oriented programming and very abridging for the programmers to make and arrange the program. a class tha is inherited from the parent class will heir all variables and functions from the parent class. in java, inheritance implementation uses keywords "extends".

C.    Tools and Materials
·      Laptop
·      Practice Object Oriented Programming Modul
·      Software NetBeans IDE 8.0.2
·      Java Development Kit (JDK) 1.8.0_31

D.    Work Step
EXPERIMENT 1
Steps of experiment:
1.    Change Method Aksesor in class “Bicycle.java” become protected
2.    Write the code program
            /**
 *
 * @author Octopus_girl
 */
public class MountainBike extends Bicycle {
    static boolean compas = true;
   
    public static void main (String[]args){
        MountainBike mountBike = new MountainBike();
        mountBike.speedUp(100);
        mountBike.changeGear(6);
        mountBike.changeCadence(9);
        mountBike.printStates();
        mountBike.lookCompas();
    }
    void lookCompas(){
        System.out.println("ada kompasnya ??"+compas);
    } 
}
3.    Save file like the name of class
4.    Run and observe the result
EXECISE

1.    Make RoadBicycle.java that have special variable it is diskBreak.
Static int diskBreak =  2;
/**
 *
 * @author Octopus_girl
 */
public class RoadBicycle extends Bicycle {
    static int diskBreak = 2;
   
    public static void main (String[]args){
        RoadBicycle roadbicycle = new RoadBicycle();
        roadbicycle.speedUp(100);
        roadbicycle.changeGear(6);
        roadbicycle.changeCadence(9);
        roadbicycle.printStates();
        roadbicycle.lookdiskBreak();
    }
    void lookdiskBreak(){
        System.out.println("diskBreak = "+diskBreak);
    } 
}
2.    Make TandemBike.java class that is have special variable it is sadel.
Static int sadel = 2;
/**
 *
 * @author Octopus_girl
 */
public class TandemBike extends Bicycle {
    static int sadel = 2;
   
    public static void main (String[]args){
        TandemBike tandembike = new TandemBike();
        tandembike.speedUp(100);
        tandembike.changeGear(6);
        tandembike.changeCadence(9);
        tandembike.printStates();
        tandembike.looksadel();
    }
    void looksadel(){
        System.out.println("Sadel = " +sadel);
    } 
}
3.    Run and observe the result
EXPERIMENT 2 (Calling Constructor super class)
In this experiment we will try to access constructor in super class for subclass.

Steps of experiment:
1.    Create class “Orang.java”, write the program code:
            /**
 *
 * @author Octopus_girl
 */
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+"th");
    }
}
2.    Create new class with name “Pegawai.java” that is subclass from ‘Orang.java’ write code program:
/**
 *
 * @author Octopus_girl
 */
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();
    }
}
3.    Create new class with main function ( ), like picture
 /**
 *
 * @author Octopus_girl
 */
public class AksesKonstSuperClass {
    public static void main (String[]args){
        Pegawai karyawan = new Pegawai("1245","Mr.Budi",24);
        karyawan.info();
    }
}
4.    Save file by class name, run and observe the result
    

EXERCISE

1.      Create class “mahasiswaTI.java” that is class derivative from orang class by show nama, nim, usia and konsentrasi jurusan
/**
 *
 * @author Octopus_girl
 */
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 Jurusan : "+this.Konsentrasi);
        super.info();
    }
}
2.      Then make class AksesMahasiswaTI.java that have main function ( ), as implement of class MahasiswaTI.java
/**
 *
 * @author Octopus_girl
 */
public class AksesMahasiswaTI {
    public static void main (String[]args){
        mahasiswaTI mahasiswaTI = new mahasiswaTI("L200134004","Rekayasa Perangkat Lunak","Anggi",19);
        mahasiswaTI.info();
    }
}
3.      The result
4.      Analyst the result

E.   Analysis
 After running the program, the subclasses are able to access some variables in the parent class. Not only the variables, but also the methods, or even the whole characters in parent class. It's means that the inheritance make more simple and help us.
F.   Assignment
1.         Is every class must have minimal one constructor? Proved your answer by try the class is haven’t constructor and try to run it. Attach the class

/**
 *
 * @author Octopus_girl
 */
public class HitungKarakter {
    String x;

   void changeword (String newword){
       x = newword;
       System.out.println("Silahkan tuliskan kalimat: \n"+x);
   }
   void counting (){
       System.out.println("Jumlah karakternya = "+x.length());
   }
   void enlarge (){
       System.out.println(x.toUpperCase());
   }
    public static void main(String []args){
        HitungKarakter kalimat1 = new HitungKarakter();
      
        kalimat1.changeword("Namaku Rahmatul Mahdalina");
        kalimat1.counting();
        kalimat1.enlarge();
    }
}


No, not all class must have constructor. The constructor is just special method that make our work more simple 
2.         Observed code and compile the class then watch the error messages and report.
Edit the class until the error message lost. Report the result
/**
 *
 * @author Octopus_girl
 */
public class KucingRumahan extends Kucing {
    int kumis; //panjang kumis, berkurang setiap kali dirawat
   
    KucingRumahan(int kumis){
        this.kumis = kumis;
    }
   
    KucingRumahan (int kumis, int umur, String warnabulu){
        super();
        this.kumis = kumis;
        this.umur = umur;
        this.warnabulu = warnabulu;
    }
   
    void rawatKumis(){
        kumis++;
    }
   
    public static void main (String[]args){
        KucingRumahan Kucing = new KucingRumahan (3,5,"Abu-abu");
        Kucing.printStates();
    }
}


Welcome to My Blog

Popular Post

Blogger templates

Pengikut

Diberdayakan oleh Blogger.

Arsip Blog

- Copyright © Mahdalina -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -