Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.

Learning Java OOP: Understanding the Basic Concepts of OOP (for Beginners)

Learn Java OOP
OOP is something that must be learned to understand Java more deeply. Because Java itself is a language designed for OOP.
The proof:
When we make the first program , we are required to use the class .
class Hello {
    public static void main(String[] args){
        System.out.println("Hello World!");
    }
}
OOP is known as a modern programming technique that is more efficient and widely used in the Framework .

If you want to understand the Framework, first study the OOP concept

So there is no reason not to study OOP.
What will we learn in this article?
  1. What is OOP?
  2. Objects and Classes
  3. Class Diagram
Let's start…

What is OOP?

OOP ( Object Oriented Programming ) or in Indonesian known as object oriented programming (PBO) is a paradigm or programming technique that is object oriented.
In OOP, functions and variables are wrapped in an object or class that can interact with each other, thus forming a program.
Object
Variables in objects will store data from objects. While the function will determine its operation.
Examples of objects in the real world: Cars, Birds, Drones, Tables, Trees, etc.
------------------OBJEK
Drone
------------------Variabel/Atribut
energi = 100;
ketinggian = 200;
kecepatan = 29;
------------------Fungsi
terbang();
matikanMesin();
turun();
maju();
mundur();
belok();
------------------
All objects in the real world that have properties and behavior, we can represent in code.
Keywords to remember:
"The object contains data and functions"

Class and Object

Class is a design or blue print of an object.
While the object in programming is a variable that is an instance of Class .
Instance can be interpreted as a form of class .
Class contains definitions of variables and functions that describe an object.
In OOP:
  • Variables are called attributes or properties ;
  • The function is called the method .
Example Class :
class NamaClass {
    String atribut1;
    String atribut2;

    void namaMethod(){ ... }
    void namaMethodLain(){ ... }
}
Then, we usually create objects (instances) like this:
NamaClass namaObj = new NamaClass();
Keywords newfunction to create new objects from certain classes .
After creating the object, we can access the attributes and methods of the object.
Example:
namaObj.namaMethod();
namaObj.atribut1;
The dot ( .) functions to access attributes and methods.

Examples of OOP Programs

We will use Netbeans, please create a new project.
Choosing a Java project
Fill in the name of the project with "BelajarOOP" and don't check "Create Main Class" .
Give the name of a Java project
After that, please create a new package . Right-click on <default package> , then select New-> Java Package .
Creating Java packages
Fill in the package name with "base" :
Creating Java packages
After that, in the package dasar, please create a new classnamed Player.java.
Right-click on the package dasarand then choose New-> Java Class . After that, fill in the name with Player.java.
Please write the code Player.javalike this:
package dasar;

public class Player {
    
    // definisi atribut
    String name;
    int speed;
    int healthPoin;

    // definisi method run
    void run(){
        System.out.println(name +" is running...");
        System.out.println("Speed: "+ speed);
    }

    // definisi method isDead untuk mengecek nilai kesehatan (healthPoin)
    boolean isDead(){
        if(healthPoin <= 0) return true;
        return false;
    }
    
}
Next, create a new class named Game.javaand fill in the code like this:
package dasar;

public class Game {
    public static void main(String[] args){

        // membuat objek player
        Player petani = new Player();

        // mengisi atribut player
        petani.name = "Petani Kode";
        petani.speed = 78;
        petani.healthPoin = 100;

        // menjalankan method
        petani.run();

        if(petani.isDead()){
            System.out.println("Game Over!");
        }

    }
}
Try execution Game.javaby right-clicking Game.javathen selecting Run File . Then the result:
Examples of Java OOP Programs
Try changing the value healthPointo 0.
petani.healthPoin = 0;
After that, try executing again:
Program output
The player petaniis dead, the game is finished!😄

How come? Explain Don!

Well, we start from making classes .
First of all, we create a class with a name PlayerThis class defines the Player object in the Game with the following attributes:
  • name is the name of the object;
  • speed is speed;
  • healthPoinis the health value of the player, usually abbreviated hp.
Then the class Playerhas a method:
  • run() to move the player;
  • isDead()to check the health condition of the player. This method will return a value trueif the value hp is smaller or equal to zero ( 0), otherwise it will return a value false.
Next we create a new object from the class Playerin the Gamenamed class petani.
// membuat objek player
Player petani = new Player();
After that, fill in the attributes. Because if it's not filled it will be null and can cause NullPointerException .
Farmer Object
Next we try modifying the value hpto zero.
As a result, a message Game Over!will be displayed. Because the method isDead()returns a value trueif the value is hpless or equal to zero.
Farmer Object

Examples of OOP 2 Programs: Drone Programs

For the next practice, try making a class Drone.
class Drone {
    // atribut
    int energi;
    int ketinggian;
    int kecepatan;
    String merek;

    // method
    void terbang(){
        energi--;
        if(energi > 10){
            // terbang berarti nilai ketinggian bertambah
            ketinggian++;
            System.out.println("Dorne terbang...");
        } else {
            System.out.println("Energi lemah: Drone nggak bisa terbang");
        }
    }

    void matikanMesin(){
        if(ketinggian > 0){
            System.out.println("Mesin tidak bisa dimatikan karena sedang terbang");
        } else {
            System.out.println("Mesin dimatikan...");
        }
    }

    void turun(){
        // ketinggian berkurang, karena turun
        ketinggian--;
        energi--;
        System.out.println("Drone turun");
    }

    void belok(){
        energi--;
        System.out.println("Drone belok");
        // belok ke mana? perlu dicek :)
    }

    void maju(){
        energi--;
        System.out.println("Drone maju ke depan");
        kecepatan++;
    }

    void mundur(){
        energi--;
        System.out.println("Drone mundur");
        kecepatan++;
    }
}

How to determine the attributes and methods of an object?

As explained earlier. The object has data (attributes) and operations (functions).
The key:
  • The attribute is like the characteristics of an object.
  • The method is like behavior or operation.
Example:
Suppose we want to make a class from Senjata.
What are the characteristics of weapons or data that can be used as attributes?
  • number of bullets
  • type
  • weapon name
  • weight
  • color
  • etc
Then what is the behavior or operation (function) that can be done?
  • shoot ()
  • throw ()
  • less Bullet ()
  • reload ()
  • etc.
Weapon Objects
Then the class can be made like this:
class Senjata {
    // atribut
    String nama;
    int jumlahPeluru;
    
    // method
    void tembak(){
        //..
        // setelah nembak, kurangi peluru
        jumlahPeluru--;
    }

    void reload(){
        //..
        // kalau di-reload maka jumlah peluru diisi ulang
        jumlahPeluru = 300;
    }
}
It's easy not ...

Get to know the Class Diagram

Class Diagram is a diagram that describes the relationships between classes . We can create Class Diagrams with design applications (CASE), such as StarUML.
Class diagram in StarUML
class is represented by a table of 1 column and 3 rows.
The first line contains the class name The second line contains attributes; and the third line contains the method.
Class diagram in StarUML
In addition, there is a line that describes the relationship between classes .
Relationship diagram in StarUML
Class Diagrams are usually used by software engineers to design software with the OOP paradigm.
As programmers, we only need to know how to read and write it in code.
If you are interested in class diagrams, you can read books about UML.

0 Komentar untuk "Learning Java OOP: Understanding the Basic Concepts of OOP (for Beginners)"

Silahkan berkomentar sesuai artikel

 
Copyright © 2015 - 2024 Blog for Learning - All Rights Reserved
Template By Kunci Dunia
Back To Top