Certificación Java
Preparándome para la certificación Java tratare de ir poniendo los ejercicios que van realizándose dentro del curso.
Problema:
public class TestVehicle {
public static void main(String[] args) {
// Create a vehicle that can handle 10,000 kilograms weight
System.out.println("Creating a vehicle with a 10,000kg maximum load.");
Vehicle vehicle = new Vehicle(10000.0);
// Add a few boxes
System.out.println("Add box #1 (500kg)");
vehicle.load = vehicle.load + 500.0;
System.out.println("Add box #2 (250kg)");
vehicle.load = vehicle.load + 250.0;
System.out.println("Add box #3 (5000kg)");
vehicle.load = vehicle.load + 5000.0;
System.out.println("Add box #4 (4000kg)");
vehicle.load = vehicle.load + 4000.0;
System.out.println("Add box #5 (300kg)");
vehicle.load = vehicle.load + 300.0;
// Print out the final vehicle load
System.out.println("Vehicle load is " + vehicle.getLoad() + " kg");
}
}
Solución:
public class Vehicle {
public double load,maxLoad;
public Vehicle(double maxLoad) {
this.maxLoad=maxLoad;
}
public double getLoad() {
return this.load;
}
public double getMaxload() {
return this.maxLoad;
}
}
Listo!
Comentarios