Skip to content

Commit 938fdc4

Browse files
committed
#29 - Java
1 parent 928325d commit 938fdc4

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
public class Josegs95 {
2+
public static void main(String[] args) {
3+
//Ejercicio
4+
//Forma incorrecta
5+
WrongPigeon pidgeon1 = new WrongPigeon();
6+
WrongHuman human1 = new WrongHuman();
7+
8+
pidgeon1.walk();
9+
pidgeon1.fly();
10+
human1.walk();
11+
human1.fly();
12+
13+
//Forma correcta
14+
Pigeon pigeon2 = new Pigeon();
15+
Human human2 = new Human();
16+
17+
pigeon2.fly();
18+
pigeon2.walk();
19+
human2.walk();
20+
21+
//Reto
22+
new Josegs95().retoFinal();
23+
}
24+
25+
public void retoFinal(){
26+
OldPrinter printer1 = new OldPrinter();
27+
MediumPrinter printer2 = new MediumPrinter();
28+
ModernPrinter printer3 = new ModernPrinter();
29+
30+
printer1.print("Texto ejemplo");
31+
printer2.print("Texto ejemplo");
32+
printer3.print("Texto ejemplo");
33+
34+
System.out.println(printer3.scan());
35+
printer3.fax("Texto por fax");
36+
}
37+
38+
//Forma incorrecta
39+
public interface WrongAnimal{
40+
public void walk();
41+
public void fly();
42+
}
43+
public static class WrongPigeon implements WrongAnimal{
44+
45+
@Override
46+
public void walk() {
47+
System.out.println("La paloma camina...");
48+
}
49+
50+
@Override
51+
public void fly() {
52+
System.out.println("La paloma vuela...");
53+
}
54+
}
55+
public static class WrongHuman implements WrongAnimal{
56+
57+
@Override
58+
public void walk() {
59+
System.out.println("El humano camina...");
60+
}
61+
62+
@Override
63+
public void fly() {} //Los humanos no vuelan
64+
}
65+
//Forma correcta
66+
public interface WalkableAnimal{
67+
public void walk();
68+
}
69+
public interface FlyingAnimal {
70+
public void fly();
71+
}
72+
public static class Pigeon implements WalkableAnimal, FlyingAnimal {
73+
74+
@Override
75+
public void walk() {
76+
System.out.println("La paloma camina...");
77+
}
78+
79+
@Override
80+
public void fly() {
81+
System.out.println("La paloma vuela...");
82+
}
83+
}
84+
public static class Human implements WalkableAnimal{
85+
86+
@Override
87+
public void walk() {
88+
System.out.println("El humano camina...");
89+
}
90+
}
91+
//Reto
92+
public abstract class Printer{
93+
public void print(String text){
94+
System.out.println(text);
95+
}
96+
}
97+
public interface BWPrinter{
98+
public void printBW(String text);
99+
}
100+
public interface ColorPrinter{
101+
public void printColor(String text);
102+
}
103+
public interface MultiFunctionPrinter{
104+
public String scan();
105+
public void fax(String text);
106+
}
107+
public class OldPrinter extends Printer implements BWPrinter{
108+
109+
@Override
110+
public void printBW(String text) {
111+
super.print("\"" + text + "\" en blanco y negro");
112+
}
113+
114+
@Override
115+
public void print(String text) {
116+
printBW(text);
117+
}
118+
}
119+
public class MediumPrinter extends Printer implements ColorPrinter{
120+
121+
@Override
122+
public void printColor(String text) {
123+
super.print("\"" + text + "\" a color");
124+
}
125+
126+
@Override
127+
public void print(String text) {
128+
printColor(text);
129+
}
130+
}
131+
public class ModernPrinter extends Printer implements ColorPrinter, MultiFunctionPrinter{
132+
133+
@Override
134+
public void printColor(String text) {
135+
super.print("\"" + text + "\" a color");
136+
}
137+
138+
@Override
139+
public String scan() {
140+
return "Texto escaneado";
141+
}
142+
143+
@Override
144+
public void fax(String text) {
145+
System.out.println("Texto enviado por fax con éxito");
146+
}
147+
148+
@Override
149+
public void print(String text) {
150+
printColor(text);
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)