You are on page 1of 3

Praktikum Pemrograman Berorentasi Objek

enkapsulasi

Oleh :
Nama : Muhammad Akhmal Akbar Nurrizky
NRP : 3121521026
Praktikum

Source code EROR

Vehicle.java
public class Vehicle {
public double load = 0;
public double maxLoad = 0;
public Vehicle(double maxLoad) {
this.maxLoad = maxLoad;
}
public double getLoad(){
return this.load;
}
public double getMaxLoad(){
return this.maxLoad;
}
}

TestVehicle.java
public class TestVehicle {
public static void main(String args[]){
System.out.println("Creating a vehicle with a 10.000kg maximum load");
Vehicle vehicle = new Vehicle(10000);
System.out.println("add box #1 (500kg)");
vehicle.load = vehicle.load + 500;
System.out.println("add box #2 (250kg)");
vehicle.load = vehicle.load + 250;
System.out.println("add box #3 (5000kg)");
vehicle.load = vehicle.load + 5000;
System.out.println("add box #4 (4000kg)");
vehicle.load = vehicle.load + 4000;
System.out.println("add box #5 (300kg)");
vehicle.load = vehicle.load + 300;
System.out.println("vehicle load is " + vehicle.getLoad() + "
kg");
}
}
Source code Correct

Vehicle.java

public class Vehicle {


private double load = 0;
private double maxLoad = 0;
public Vehicle(double maxLoad) {
this.maxLoad = maxLoad;
}
public double getLoad(){
return this.load;
}
public double getMaxLoad(){
return this.maxLoad;
}
public boolean addBox(double weight){
if (load + weight > maxLoad){
return false;
}else{
this.load += weight;
return true;
}
}
}
TestVehicle.java

public class TestVehicle {


public static void main(String args[]){
System.out.println("creating a vehicle with a 10.000kg maximum load");
Vehicle vehicle = new Vehicle(10000);

System.out.println("add box #1 (500kg) : " + vehicle.addBox(500));


System.out.println("add box #2 (250kg) : " + vehicle.addBox(250));
System.out.println("add box #3 (5000kg) : " + vehicle.addBox(5000));
System.out.println("add box #4 (4000kg) : " + vehicle.addBox(4000));
System.out.println("add box #5 (300kg) : " + vehicle.addBox(300));

System.out.println("Vehicle load is " + vehicle.getLoad() + " kg");


}
}

You might also like