You are on page 1of 5

LAPORAN PRAKTIKUM

MATA KULIAH PEMROGRAMAN BERORIENTASI OBJEK


“EXERCISE 13.12”

Dosen Pengampu :
Benedika Ferdian Hutabarat, S.Komp., M.Kom.

Disusun Oleh :
Tisa Ameiliawati
F1E122045
R003

PROGRAM STUDI SISTEM INFORMASI


FAKULTAS SAINS DAN TEKNOLOGI
UNIVERSITAS JAMBI
2023
1. EXERCISE 12.2

SINTAKS
package ch13;

public class exc13_12 {


public static void main(String[] args) {
GeometricObject[] objects = new GeometricObject[4];
objects[0] = new Rectangle(Math.random() * 6, Math.random() * 20);
objects[1] = new Rectangle(Math.random() * 4, Math.random() * 20);
objects[2] = new Circle(Math.random() * 19, "red", true);
objects[3] = new Circle(Math.random() * 5, "orange", false);

System.out.printf("Total area of object in the array is %.2f cm^2",


sumArea(objects));
}

public static double sumArea(GeometricObject[] a) {


double sum = 0;

for (int i = 0; i < a.length; i++) {


if (a[i] instanceof Circle) {
sum += ((Circle) a[i]).getArea();
System.out.printf("The Circle area is %.2f cm^2 \n", ((Circle)
a[i]).getArea());
} else if (a[i] instanceof Rectangle) {
sum += ((Rectangle) a[i]).getArea();
System.out.printf("The Rectangle area is %.2f cm^2 \n",
((Rectangle) a[i]).getArea());
}
}
return sum;
}
}

class GeometricObject {
private String color = "white";
private boolean filled;
private java.util.Date dateCreated;

public GeometricObject() {
dateCreated = new java.util.Date();
}
public GeometricObject(String color, boolean filled) {
dateCreated = new java.util.Date();
this.color = color;
this.filled = filled;
}

public String getColor() {


return color;
}

public void setColor(String color) {


this.color = color;
}

public boolean isFilled() {


return filled;
}

public void setFilled(boolean filled) {


this.filled = filled;
}

public java.util.Date getDateCreated() {


return dateCreated;
}

public String toString() {


return "created on " + dateCreated + "\ncolor: " + color + " and filled: "
+ filled;
}
}

class Circle extends GeometricObject {


private double radius;

public Circle() {
}

public Circle(double radius) {


this.radius = radius;
}

public Circle(double radius, String color, boolean filled) {


this.radius = radius;
setColor(color);
setFilled(filled);
}

public double getRadius() {


return radius;
}

public void setRadius(double radius) {


this.radius = radius;
}

public double getArea() {


return this.radius * this.radius * Math.PI;
}

public double getDiameter() {


return 2 * radius;
}

public double getPerimeter() {


return 2 * radius * Math.PI;
}

public void printCircle() {


System.out.println("The circle is created " + getDateCreated() +
" and the radius is " + radius);
}
}

class Rectangle extends GeometricObject {


private double width;
private double height;

public Rectangle() {
}

public Rectangle(double width, double height) {


this.width = width;
this.height = height;
}

public Rectangle(double width, double height, String color, boolean filled) {


this.width = width;
this.height = height;
setColor(color);
setFilled(filled);
}

public double getWidth() {


return width;
}

public void setWidth(double width) {


this.width = width;
}
public double getHeight() {
return height;
}

public void setHeight(double height) {


this.height = height;
}

public double getArea() {


return width * height;
}

public double getPerimeter() {


return 2 * (width + height);
}
}

Program Java di atas memanfaatkan konsep dasar pemrograman berorientasi objek (OOP)
untuk mengimplementasikan kelas-kelas geometris yang terkait, yaitu `GeometricObject`, `Circle`,
dan `Rectangle`. Kelas `GeometricObject` berfungsi sebagai kelas dasar yang memiliki properti
seperti warna (`color`), keadaan terisi atau tidak terisi (`filled`), dan tanggal penciptaan
(`dateCreated`). Selain itu, kelas ini menyediakan metode getter dan setter untuk propertinya, serta
metode `toString` untuk menghasilkan representasi string objek.
Kelas `Circle` dan `Rectangle` merupakan turunan dari `GeometricObject`, yang
menunjukkan konsep pewarisan dalam OOP. Kelas `Circle` menambahkan properti radius (`radius`)
dan menyediakan metode untuk menghitung area, diameter, dan keliling lingkaran. Sementara itu,
`Rectangle` menambahkan properti lebar (`width`) dan tinggi (`height`), dan menyediakan metode
untuk menghitung area dan keliling persegi panjang.
Penting untuk dicatat bahwa program ini menerapkan polimorfisme melalui metode
`sumArea`. Metode ini mengambil array dari objek `GeometricObject`, dan menggunakan
polimorfisme untuk mengakses metode `getArea` dari objek, tanpa memperhatikan tipe sebenarnya
(Circle atau Rectangle). Ini menunjukkan fleksibilitas OOP dalam memproses objek dengan tipe yang
berbeda secara bersamaan.
Selain itu, dalam `main` method, objek-objek geometris dibuat dengan menggunakan nilai
acak untuk propertinya. Setelah itu, metode `sumArea` dipanggil untuk menghitung dan mencetak
total luas dari objek-objek tersebut. Keseluruhan, program ini memberikan contoh nyata penggunaan
konsep-konsep dasar OOP untuk memodelkan objek-objek geometris dengan sifat-sifat khusus
masing-masing, seperti radius untuk lingkaran dan lebar serta tinggi untuk persegi panjang.
OUTPUT

You might also like