Skip to content

Commit a5bd491

Browse files
authored
Merge pull request mouredev#6378 from martinbohorquez/java#28
#28 - java
2 parents fbbfddc + 0761c65 commit a5bd491

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
import java.util.Random;
2+
3+
public class martinbohorquez {
4+
5+
public static void main(String[] args) {
6+
// incorrectLSP();
7+
correctLSP();
8+
/*
9+
* DIFICULTAD EXTRA
10+
*/
11+
vehicleLSP();
12+
13+
}
14+
15+
/*
16+
private static void incorrectLSP() {
17+
Bird sparrow = new Sparrow();
18+
makeBirdFly(sparrow); // Funciona correctamente
19+
20+
Bird ostrich = new Ostrich();
21+
makeBirdFly(ostrich); // Lanza excepción, lo cual es un error de LSP
22+
}
23+
24+
public static void makeBirdFly(Bird bird) {
25+
bird.fly();
26+
}
27+
*/
28+
29+
private static void correctLSP() {
30+
FlyingBird sparrow = new Sparrow();
31+
makeBirdFly(sparrow); // Funciona correctamente
32+
33+
// Ostrich no puede ser pasado a makeBirdFly, ya que no implementa FlyingBird.
34+
}
35+
36+
private static void makeBirdFly(FlyingBird bird) {
37+
bird.fly();
38+
}
39+
40+
private static void vehicleAccelerateBrake(Vehicule vehicule) {
41+
Random random = new Random();
42+
vehicule.accelerate(random.nextInt(10));
43+
vehicule.brake(random.nextInt(5));
44+
}
45+
46+
private static void vehicleLSP() {
47+
Vehicule car = new Car(60);
48+
vehicleAccelerateBrake(car);
49+
Vehicule bicycle = new Bicycle(0);
50+
vehicleAccelerateBrake(bicycle);
51+
Vehicule motorcycle = new Motorcycle(40);
52+
vehicleAccelerateBrake(motorcycle);
53+
54+
}
55+
}
56+
57+
/*
58+
// Clase base
59+
class Bird {
60+
public void fly() {
61+
System.out.println("El pájaro vuela.");
62+
}
63+
}
64+
65+
// Clase derivada
66+
class Sparrow extends Bird {
67+
@Override
68+
public void fly() {
69+
System.out.println("El gorrión vuela.");
70+
}
71+
}
72+
73+
// Clase derivada que no vuela
74+
class Ostrich extends Bird {
75+
@Override
76+
public void fly() {
77+
throw new UnsupportedOperationException("El avestruz no puede volar.");
78+
}
79+
}*/
80+
81+
// Interfaz base
82+
interface Bird {
83+
// Métodos comunes
84+
}
85+
86+
interface FlyingBird extends Bird {
87+
void fly();
88+
}
89+
90+
class Sparrow implements FlyingBird {
91+
@Override
92+
public void fly() {
93+
System.out.println("El gorrión vuela.");
94+
}
95+
}
96+
97+
class Ostrich implements Bird {
98+
// No implementa fly()
99+
}
100+
101+
abstract class Vehicule {
102+
private Integer speed;
103+
104+
protected Vehicule() {
105+
this(0);
106+
}
107+
108+
protected Vehicule(Integer speed) {
109+
this.speed = speed;
110+
System.out.printf("Se crea un %s un con velocidad %d.%n", getClass().getSimpleName(), speed);
111+
}
112+
113+
void accelerate(Integer increment) {
114+
speed += increment;
115+
System.out.printf("Velocidad: %d Km/h.%n", speed);
116+
}
117+
118+
void brake(Integer decrement) {
119+
speed -= decrement;
120+
if (speed < 0) speed = 0;
121+
System.out.printf("Velocidad: %d Km/h.%n", speed);
122+
}
123+
}
124+
125+
class Car extends Vehicule {
126+
protected Car() {
127+
super();
128+
}
129+
130+
protected Car(Integer speed) {
131+
super(speed);
132+
}
133+
134+
@Override
135+
void accelerate(Integer increment) {
136+
System.out.printf("El auto está acelerando en %d.%n", increment);
137+
super.accelerate(increment);
138+
}
139+
140+
@Override
141+
void brake(Integer decrement) {
142+
System.out.printf("El auto está frenando en %d.%n", decrement);
143+
super.brake(decrement);
144+
}
145+
}
146+
147+
class Bicycle extends Vehicule {
148+
protected Bicycle() {
149+
super();
150+
}
151+
152+
protected Bicycle(Integer speed) {
153+
super(speed);
154+
}
155+
156+
@Override
157+
void accelerate(Integer increment) {
158+
System.out.printf("La bicicleta está acelerando en %d.%n", increment);
159+
super.accelerate(increment);
160+
}
161+
162+
@Override
163+
void brake(Integer decrement) {
164+
System.out.printf("La bicicleta está frenando en %d.%n", decrement);
165+
super.brake(decrement);
166+
}
167+
}
168+
169+
class Motorcycle extends Vehicule {
170+
protected Motorcycle() {
171+
super();
172+
}
173+
174+
protected Motorcycle(Integer speed) {
175+
super(speed);
176+
}
177+
178+
@Override
179+
void accelerate(Integer increment) {
180+
System.out.printf("La moto está acelerando en %d.%n", increment);
181+
super.accelerate(increment);
182+
}
183+
184+
@Override
185+
void brake(Integer decrement) {
186+
System.out.printf("La moto está frenando en %d.%n", decrement);
187+
super.brake(decrement);
188+
}
189+
}

0 commit comments

Comments
 (0)