Praktikum PBO D 2026/Week 3: Inheritance/Live Coding

Live Coding

App.java

public class App {
    public static void main(String[] Args) {
        Vector2 fatisda = new Vector2(-7.558592134562593, 110.85800061735068);
        Vector2 momoyoAlfian = new Vector2(-7.563187731028337, 110.85171212613771);

        System.out.println(fatisda.distance(momoyoAlfian));

        Vector3 boeing737 = new Vector3(-7.902901940481244, 110.05601267573857, 0);
        Vector3 boeing787 = new Vector3(-7.910458880495463, 110.07132548599691, 0.001);
        System.out.println(boeing737.distance(boeing787));
    }
}

Vector2.java

public class Vector2 {
    double x;
    double y;

    public Vector2() {
        this.x = this.y = 0;
    }

    public Vector2(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double distance(Vector2 v) {
        // Ini elemen vektor yang sekarang
        double x1, y1, z1;
        x1 = this.x;
        y1 = this.y;
        z1 = 0;

        if (this instanceof Vector3) {
            z1 = ((Vector3) this).z;
        }

        // Titik ini, kita udah dapet nilai final x,y,z dari objek ini
        double x2, y2, z2;
        x2 = v.x;
        y2 = v.y;
        z2 = 0;

        if (v instanceof Vector3) {
            z2 = ((Vector3) v).z;
        }

        double x3 = Math.pow(x1 - x2, 2);
        double y3 = Math.pow(y1 - y2, 2);
        double z3 = Math.pow(z1 - z2, 2);

        return Math.sqrt(x3 + y3 + z3);
    }
}

Vector3.java

public class Vector3 extends Vector2 {
    double z;

    public Vector3() {
        super();
        this.z = 0;
    }

    public Vector3(double x, double y, double z) {
        super(x, y);
        this.z = z;
    }
}

Entitas.java

public class Entitas {
    Vector2 point;
    String nama;
    double radius;

    public Entitas(String nama, double radius, boolean is3D) {
        this.nama = nama;
        this.radius = radius;
        this.point = (is3D) ? new Vector3() : new Vector2();
    }

    // Bikin method buat cek dia nabrak apa enggak
    public boolean isColliding(Entitas e) {
        // TODO: Replace true with the code to calculate isColliding
        return true;
    }
}