Tugas PPBO Week-1

Run Settings
LanguageJava
Language Version
Run Command
// Nama : Muhammad Daffa Rahman // NIM : L0124062 // Program : Rakit PC Simulator import component.Component; import component.OS; import computer.PC; // Rakit PC Simulator public class PPBO_01_L0124062 { public static void main(String[] args) { // Komponen PC OS windows95 = new OS("Windows 95", "4.0"); Component softram98 = new Component("Softram98 2GB Virtual RAM"); Component ambastonSSD = new Component("Ambaston 64 GB SSD"); Component m3ultra = new Component("Apple M3 Ultra"); // PC-nya PC macintosh = new PC("Macintosh PC"); // install 1 komponen di PC macintosh.installComponent(windows95); // Cek apakah PC sudah bisa dihidupkan (ini belum) System.out.println("Can " + macintosh.name + " be turned on? " + macintosh.isPowerable()); // Install 3 komponen lagi di PC macintosh.installComponent(softram98); macintosh.installComponent(ambastonSSD); macintosh.installComponent(m3ultra); // Cek lagi apakah PC sudah bisa dihidupkan (ini suda) System.out.println("Can " + macintosh.name + " be turned on? " + macintosh.isPowerable()); // Mencoba menghidupkan PC 2 kali macintosh.turnOn(); macintosh.turnOn(); // Shutdown PC macintosh.turnOff(); } }
package component; public class Component { public String name; public Component(String name) { this.name = name; } }
package computer; import java.util.ArrayList; import component.Component; public class PC extends Component { private ArrayList<Component> components; private boolean state = false; public PC(String name) { super(name); this.components = new ArrayList<>(); } public void installComponent(Component component) { if(this.components.contains(component)) { System.out.println("Component: " + component.name + " already installed"); } else { this.components.add(component); System.out.println("Component: " + component.name + " is successfuly installed"); } } public boolean isTurnedOn() { return this.state == true; } public void turnOn() { if(this.isPowerable()) { if(this.state == true) { System.out.println(this.name + " is currently running . . ."); } else { this.state = true; System.out.println("Starting " + this.name + " . . ."); System.out.println("Successful turning on " + this.name + "."); } } else { System.out.println("Error starting " + this.name + " check your component."); } } public void turnOff() { if(this.state == false) { System.out.println(this.name + " is currently powered off . . ."); } else { this.state = false; System.out.println("Shutting down " + this.name + " . . ."); } } public boolean isPowerable() { boolean osCheck = false; int componentCount = 0; for(Component c: this.components) { if(c instanceof component.OS) { osCheck = true; } componentCount++; } return (componentCount >= 4 && osCheck == true); } }
package component; public class OS extends Component { public String version; public int size; public OS(String name, String version) { super(name); this.version = version; } public void updateOS(String toVersion) { this.version = toVersion; } }
Editor Settings
Theme
Key bindings
Full width
Lines