forked from mouredev/hello-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
48 lines (39 loc) · 1.17 KB
/
Copy pathCar.java
File metadata and controls
48 lines (39 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package basic.c08_oop;
public class Car extends Vehicle {
private String brand;
private String model;
private int speed; // velocidad privada
// Constructor
public Car(String brand, String model) {
this.brand = brand;
this.model = model;
this.speed = 0; // velocidad inicial
}
@Override
public void describe() {
System.out.println("Soy un coche de marca " + brand + " modelo " + model);
}
public void showData() {
System.out.println("Marca: " + brand + ", Modelo: " + model + ", Velocidad: " + speed);
}
public void accelerate(int amount) {
if (amount > 0) {
speed += amount;
if (speed > 120) speed = 120; // velocidad máxima
System.out.println("Velocidad actual: " + speed);
}
}
public void brake(int amount) {
if (amount > 0) {
speed -= amount;
if (speed < 0) speed = 0; // velocidad mínima
System.out.println("Velocidad actual: " + speed);
}
}
public int getSpeed() {
return speed;
}
public void honk() {
System.out.println("¡Bocina! Beep beep!");
}
}