Implementasi Abtraksi

Nama: Azka Rizqullah Ramadhani

NRP: 5025231148

Kelas: Pemrograman Berbasis Objek


Tugas

1. Tuliskan implementasi dari program kecil Abstract Class Makhluk hidup yang diwariskan kepada manusia, hewan, dan tumbuhan

2. Pelajari dan baca simulasi Foxes and Rabbit yang ada di buku. Kemudian buat program perubahan dari struktur class umum menjadi bentuk Abstract Class.


Nomer 1

Class LivingBeing

class ini akan bertindak sebagai abstraksi dari class lainnya (human, animals, plant)
public abstract class LivingBeing {
public abstract void breathe();

public abstract void grow();
}

Class Human

class ini adalah representasi dari manusia

public class Human extends LivingBeing {
@Override
public void breathe() {
System.out.println("Manusia bernapas menggunakan paru-paru.");
}

@Override
public void grow(){
System.out.println("Manusia berkembang dari kecil sampai dewasa.");
}
}
Class Plant

class ini adalah representasi dari tumbuhan

public class Plant extends LivingBeing{
@Override
public void breathe() {
System.out.println("Tumbuhan bernapas menggunakan klorofil.");
}


@Override
public void grow(){
System.out.println("Tumbuhan tumbuh sampai besar.");
}
}

Class Animals

class ini adalah representasi dari hewan

public class Animals extends LivingBeing{
@Override
public void breathe() {
System.out.println("Hewan bernapas menggunakan paru-paru (macam-macam).");
}

@Override
public void grow(){
System.out.println("Hewan Tumbuh dari kecil sampai besar.");
}
}

Class Main
public class Main {
public static void main(String[] args) {
LivingBeing azka = new Human();
LivingBeing sapi = new Animals();
LivingBeing anggrek = new Plant();

System.out.println("Manusia:");
azka.breathe();
azka.grow();

System.out.println("Hewan:");
sapi.breathe();
sapi.grow();

System.out.println("Tumbuhan:");
anggrek.breathe();
anggrek.grow();
}
}


Hasil

Nomer 2

Foxes and Rabbits adalah simulasi pengejaran beberapa kelinci oleh beberapa rubah. Jika si kelinci sudah mati maka dia otomatis tidak bergerak lagi atau kalau di kode ini dia akan di remove dari listnya. Pada kali ini saya merubah beberapa fungsi di buku menjadi lebih simple yaitu terdapat 6 class (Rabbit, Fox, Animal, Location, Field, Simulation) dengan class Animal sebagai abstraksi dari class rabbit dan fox.

Class Animal

public abstract class Animal {
protected Location location;
protected Field field;
protected boolean alive = true;

public Animal(Field field, Location location) {
this.field = field;
this.location = location;
field.place(this, location);
}

public void move() {
Location newLoc = field.randomAdjacent(location);
field.move(location, newLoc);
location = newLoc;
}

public abstract void act();
public boolean isAlive() { return alive; }
public void die() { alive = false; }
}

Class Rabbit

class Rabbit extends Animal {
public Rabbit(Field field, Location location) {
super(field, location);
}

@Override
public void act() {
if (isAlive()) {
move();
}
}
}

Class Fox

class Fox extends Animal {
public Fox(Field field, Location location) {
super(field, location);
}

@Override
public void act() {
if (isAlive()) {
move(); // Rubah bergerak
hunt(); // Rubah memangsa kelinci
}
}

private void hunt() {
for (Animal animal : Simulation.animals) {
if (animal instanceof Rabbit && animal.isAlive() && animal.location.equals(this.location)) {
animal.die(); // Memangsa kelinci
System.out.println("Fox ate a rabbit at (" + location.getX() + ", " + location.getY() + ")");
break;
}
}
}
}

Class Location

public class Location {
private int x, y;

public Location(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() { return x; }
public int getY() { return y; }

public void setLocation(int x, int y) {
this.x = x;
this.y = y;
}
}

Class Field

import java.util.Random;

public class Field {
private int width, height;
private Animal[][] grid;
private Random random = new Random();

public Field(int width, int height) {
this.width = width;
this.height = height;
grid = new Animal[width][height];
}

public boolean place(Animal animal, Location loc) {
if (isInBounds(loc) && grid[loc.getX()][loc.getY()] == null) {
grid[loc.getX()][loc.getY()] = animal;
return true;
}
return false;
}

public void move(Location from, Location to) {
if (isInBounds(from) && isInBounds(to)) {
grid[to.getX()][to.getY()] = grid[from.getX()][from.getY()];
grid[from.getX()][from.getY()] = null;
}
}

public Location randomAdjacent(Location loc) {
int x = loc.getX() + random.nextInt(3) - 1; // -1, 0, or 1
int y = loc.getY() + random.nextInt(3) - 1;
return isInBounds(new Location(x, y)) ? new Location(x, y) : loc;
}

private boolean isInBounds(Location loc) {
return loc.getX() >= 0 && loc.getX() < width && loc.getY() >= 0 && loc.getY() < height;
}
}

Class Simulation

import java.util.ArrayList;
import java.util.List;

public class Simulation {
protected static List<Animal> animals = new ArrayList<>(); // Daftar hewan di simulasi
private Field field;

public Simulation(int width, int height) {
field = new Field(width, height);

for (int i = 0; i < 5; i++) {
animals.add(new Rabbit(field, new Location(i, i)));
animals.add(new Fox(field, new Location(width - i - 1, height - i - 1)));
}
}

public void simulateStep() {
for (Animal animal : new ArrayList<>(animals)) { // Iterasi pada salinan daftar
if (animal.isAlive()) {
animal.act();
}
}

animals.removeIf(animal -> !animal.isAlive() && animal instanceof Rabbit);

long rabbitsLeft = animals.stream().filter(a -> a instanceof Rabbit).count();
System.out.println("Rabbits remaining: " + rabbitsLeft);
}

public static void main(String[] args) {
Simulation sim = new Simulation(3, 3);
for (int step = 0; step < 10; step++) {
System.out.println("\nStep " + step + ":");
sim.simulateStep();
}
}
}

Hasil





Komentar

Postingan populer dari blog ini

Implementasi Unit Test

Implementasi Support System

Tugas Ticket Machine