Created
June 23, 2025 01:31
-
-
Save zenxvolt/c4bc4e32e07c8c05748f0ff9da78555c to your computer and use it in GitHub Desktop.
hello world di javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kelas dasar: Kendaraan | |
class Kendaraan { | |
constructor(merk, tahun, warna) { | |
this.merk = merk; | |
this.tahun = tahun; | |
this.warna = warna; | |
this.status = "Tersedia"; | |
} | |
infoKendaraan() { | |
return `Merk: ${this.merk}, Tahun: ${this.tahun}, Warna: ${this.warna}, Status: ${this.status}`; | |
} | |
sewa() { | |
if (this.status === "Tersedia") { | |
this.status = "Disewakan"; | |
return `Kendaraan merk ${this.merk} berhasil disewakan.`; | |
} else { | |
return `Kendaraan merk ${this.merk} sedang tidak tersedia.`; | |
} | |
} | |
kembali() { | |
if (this.status === "Disewakan") { | |
this.status = "Tersedia"; | |
return `Kendaraan merk ${this.merk} telah dikembalikan.`; | |
} else { | |
return `Kendaraan merk ${this.merk} sudah tersedia.`; | |
} | |
} | |
} | |
// Kelas turunan: Mobil | |
class Mobil extends Kendaraan { | |
constructor(merk, tahun, warna, jumlahPintu) { | |
super(merk, tahun, warna); | |
this.jumlahPintu = jumlahPintu; | |
} | |
infoKendaraan() { | |
return `${super.infoKendaraan()}, Jumlah Pintu: ${this.jumlahPintu}`; | |
} | |
} | |
// Kelas turunan: Motor | |
class Motor extends Kendaraan { | |
constructor(merk, tahun, warna, kapasitasMesin) { | |
super(merk, tahun, warna); | |
this.kapasitasMesin = kapasitasMesin; | |
} | |
infoKendaraan() { | |
return `${super.infoKendaraan()}, Kapasitas Mesin: ${this.kapasitasMesin}cc`; | |
} | |
} | |
// Contoh penggunaan | |
let mobil1 = new Mobil("Toyota", 2020, "Hitam", 4); | |
let motor1 = new Motor("Yamaha", 2019, "Merah", 150); | |
console.log(mobil1.infoKendaraan()); | |
console.log(motor1.infoKendaraan()); | |
console.log(mobil1.sewa()); | |
console.log(motor1.sewa()); | |
console.log(mobil1.kembali()); | |
console.log(motor1.kembali()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment