Tugas PBO 13 Abstract Class

Nama : Afel Allaric Absor
NRP   : 5025231140
Kelas : PBO (A)

                                                           Tugas PBO 13 Abstract Class


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

- Isi dari file MakhlukHidup.java :
public abstract class MakhlukHidup {
private String nama;
private int usia;

public MakhlukHidup(String nama, int usia) {
this.nama = nama;
this.usia = usia;
}

public String getNama() {
return nama;
}

public void bertambahUsia() {
this.usia++;
System.out.println(nama + " bertambah usia. Usia sekarang: " + usia);
}

public abstract void bernapas();
public abstract void berkembangBiak();
}

class Manusia extends MakhlukHidup {
public Manusia(String nama, int usia) {
super(nama, usia);
}

@Override
public void bernapas() {
System.out.println(getNama() + " bernapas dengan paru-paru.");
}

@Override
public void berkembangBiak() {
System.out.println(getNama() + " berkembang biak dengan cara melahirkan.");
}

public void berpikir() {
System.out.println(getNama() + " dapat berpikir.");
}
}

class Hewan extends MakhlukHidup {
public Hewan(String nama, int usia) {
super(nama, usia);
}

@Override
public void bernapas() {
System.out.println(getNama() + " bernapas dengan paru-paru atau insang.");
}

@Override
public void berkembangBiak() {
System.out.println(getNama() + " berkembang biak dengan cara bertelur atau melahirkan.");
}

public void berburu() {
System.out.println(getNama() + " sedang berburu makanan.");
}
}

class Tumbuhan extends MakhlukHidup {
public Tumbuhan(String nama, int usia) {
super(nama, usia);
}

@Override
public void bernapas() {
System.out.println(getNama() + " bernapas melalui stomata.");
}

@Override
public void berkembangBiak() {
System.out.println(getNama() + " berkembang biak dengan biji atau stek.");
}

public void melakukanFotosintesis() {
System.out.println(getNama() + " sedang melakukan fotosintesis.");
}
}

- Isi dari file Main.java :

public class Main {
public static void main(String[] args) {
Manusia manusia = new Manusia("Kinan", 25);
manusia.bernapas();
manusia.berkembangBiak();
manusia.berpikir();
manusia.bertambahUsia();

System.out.println();

Hewan hewan = new Hewan("Kucing", 3);
hewan.bernapas();
hewan.berkembangBiak();
hewan.berburu();
hewan.bertambahUsia();

System.out.println();

Tumbuhan tumbuhan = new Tumbuhan("Buah_Naga", 5);
tumbuhan.bernapas();
tumbuhan.berkembangBiak();
tumbuhan.melakukanFotosintesis();
tumbuhan.bertambahUsia();
}
}

- Hasil kode ketika dijalankan :

Kinan bernapas dengan paru-paru.
Kinan berkembang biak dengan cara melahirkan.
Kinan dapat berpikir.
Kinan bertambah usia. Usia sekarang: 26

Kucing bernapas dengan paru-paru atau insang.
Kucing berkembang biak dengan cara bertelur atau melahirkan.
Kucing sedang berburu makanan.
Kucing bertambah usia. Usia sekarang: 4

Buah_Naga bernapas melalui stomata.
Buah_Naga berkembang biak dengan biji atau stek.
Buah_Naga sedang melakukan fotosintesis.
Buah_Naga bertambah usia. Usia sekarang: 6

Process finished with exit code 0


2). Simulasi Foxes dan Rabbit yang ada di buku. Ubah menjadi bentuk Abstract Class!

- Isi dari file Animal.java :
import java.util.List;

public abstract class Animal {
private int age;
private boolean alive;
private Field field;
private Location location;

public Animal(Field field, Location location) {
this.age = 0;
this.alive = true;
this.field = field;
setLocation(location);
}

public boolean isAlive() {
return alive;
}

public void setDead() {
alive = false;
if (location != null) {
field.clear(location);
location = null;
field = null;
}
}

public Location getLocation() {
return location;
}

public void setLocation(Location newLocation) {
if (location != null) {
field.clear(location);
}
location = newLocation;
field.place(this, newLocation);
}

public Field getField() {
return field;
}

public abstract void act(List<Animal> newAnimals);
}
- Isi dari file Rabbit.java :
import java.util.List;

public abstract class Animal {
private int age;
private boolean alive;
private Field field;
private Location location;

public Animal(Field field, Location location) {
this.age = 0;
this.alive = true;
this.field = field;
setLocation(location);
}

public boolean isAlive() {
return alive;
}

public void setDead() {
alive = false;
if (location != null) {
field.clear(location);
location = null;
field = null;
}
}

public Location getLocation() {
return location;
}

public void setLocation(Location newLocation) {
if (location != null) {
field.clear(location);
}
location = newLocation;
field.place(this, newLocation);
}

public Field getField() {
return field;
}

public abstract void act(List<Animal> newAnimals);
}
- Isi dari file Fox.java :
import java.util.List;

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

@Override
public void act(List<Animal> newAnimals) {
if (isAlive()) {
Location newLocation = findFood();
if (newLocation == null) {
newLocation = getField().freeAdjacentLocation(getLocation());
}
if (newLocation != null) {
setLocation(newLocation);
} else {
setDead();
}
}
}

private Location findFood() {
List<Location> adjacent = getField().adjacentLocations(getLocation());
for (Location loc : adjacent) {
Object animal = getField().getObjectAt(loc);
if (animal instanceof Rabbit) {
Rabbit rabbit = (Rabbit) animal;
rabbit.setDead();
System.out.println("Fox has died.");
return loc;
}
}
return null;
}
}
- Isi dari file Field.java :
import java.util.ArrayList;
import java.util.List;

public class Field {
private Object[][] field;

public Field(int depth, int width) {
field = new Object[depth][width];
}

public int getDepth() {
return field.length;
}

public int getWidth() {
return field[0].length;
}

public void clear(Location location) {
field[location.getRow()][location.getCol()] = null;
}

public void place(Object object, Location location) {
field[location.getRow()][location.getCol()] = object;
}

public Object getObjectAt(Location location) {
return field[location.getRow()][location.getCol()];
}

public Location freeAdjacentLocation(Location location) {
List<Location> free = new ArrayList<>();
List<Location> adjacent = adjacentLocations(location);
for (Location loc : adjacent) {
if (getObjectAt(loc) == null) {
free.add(loc);
}
}
return free.isEmpty() ? null : free.get(0);
}

public List<Location> adjacentLocations(Location location) {
List<Location> locations = new ArrayList<>();
int row = location.getRow();
int col = location.getCol();
for (int r = Math.max(0, row - 1); r <= Math.min(field.length - 1, row + 1); r++) {
for (int c = Math.max(0, col - 1); c <= Math.min(field[0].length - 1, col + 1); c++) {
if (r != row || c != col) {
locations.add(new Location(r, c));
}
}
}
return locations;
}
}

- Isi dari file Location.java :

public class Location {
private int row;
private int col;

public Location(int row, int col) {
this.row = row;
this.col = col;
}

public int getRow() {
return row;
}

public int getCol() {
return col;
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Location location = (Location) obj;
return row == location.row && col == location.col;
}

@Override
public int hashCode() {
return 31 * row + col;
}
}

- Isi dari file Simulator.java :

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

public class Simulator {
private Field field;
private List<Animal> animals;

public Simulator(int depth, int width) {
field = new Field(depth, width);
animals = new ArrayList<>();
populate();
}

public void simulate(int steps) {
for (int step = 0; step < steps; step++) {
System.out.println("Step " + (step + 1) + ":");
List<Animal> newAnimals = new ArrayList<>();
for (Animal animal : animals) {
animal.act(newAnimals);
}
animals.addAll(newAnimals);
printField();
}
}

private void populate() {
for (int row = 0; row < field.getDepth(); row++) {
for (int col = 0; col < field.getWidth(); col++) {
if (Math.random() > 0.8) {
Location location = new Location(row, col);
if (Math.random() > 0.5) {
Rabbit rabbit = new Rabbit(field, location);
animals.add(rabbit);
} else {
Fox fox = new Fox(field, location);
animals.add(fox);
}
}
}
}
}

private void printField() {
for (int row = 0; row < field.getDepth(); row++) {
for (int col = 0; col < field.getWidth(); col++) {
Object obj = field.getObjectAt(new Location(row, col));
if (obj instanceof Rabbit) {
System.out.print("R ");
} else if (obj instanceof Fox) {
System.out.print("F ");
} else {
System.out.print(". ");
}
}
System.out.println();
}
System.out.println();
}
}
- Isi dari file Main.java :
public class Main {
public static void main(String[] args) {
Simulator simulator = new Simulator(6, 6);
simulator.simulate(10);
}
}

- Hasil dari kode : 

Step 1:
Rabbit at Location@3e is moving.
Rabbit at Location@3f is moving.
Rabbit at Location@5e is moving.
. . . F . . 
R R . . . . 
R . . . . . 
. F . F . . 
. . . . . . 
. . . . . . 

Step 2:
Rabbit at Location@1f is moving.
Rabbit at Location@20 is moving.
Rabbit at Location@3e is moving.
R R F . . . 
R . . . . . 
F . F . . . 
. . . . . . 
. . . . . . 
. . . . . . 

Step 3:
Fox has died.
Rabbit at Location@0 is moving.
Rabbit at Location@1f is moving.
Fox has died.
R F . . . . 
. F F . . . 
. . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 

Step 4:
Fox has died.
F F F . . . 
. . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 

Step 5:
F F . . . . 
F . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 

Step 6:
F . . . . . 
F F . . . . 
. . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 

Step 7:
. F . . . . 
F F . . . . 
. . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 

Step 8:
F F . . . . 
. F . . . . 
. . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 

Step 9:
F F . . . . 
F . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 

Step 10:
F . . . . . 
F F . . . . 
. . . . . . 
. . . . . . 
. . . . . . 
. . . . . . 


Process finished with exit code 0





Komentar

Postingan populer dari blog ini

PWEB 15 Desain Final Project

Tugas 8 Pemrograman Web (E) Membuat Form Register & Login Menggunakan Bootstrap

Tugas PBO 14 GUI & Panel Login