Skip to content

Commit 914b639

Browse files
committed
#27 - Java
1 parent ef90c39 commit 914b639

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
public class Josegs95 {
2+
public static void main(String[] args) {
3+
//Ejercicio
4+
//Forma incorrecta
5+
Object object1 = new WrongHuman();
6+
if (object1 instanceof WrongHuman)
7+
System.out.println("Los humanos se mueven a dos patas");
8+
else if (object1 instanceof WrongCat)
9+
System.out.println("Los gatos se mueven a cuatro patas");
10+
else if (object1 instanceof WrongPigeon)
11+
System.out.println("Las palomas se mueven por el aire volando");
12+
else
13+
System.out.println("Animal desconocido");
14+
15+
//Forma correcta
16+
Object object2 = new Cat();
17+
if (object2 instanceof Animal)
18+
((Animal) object2).move();
19+
else
20+
System.out.println("Animal desconocido");
21+
22+
//Reto
23+
retoFinal();
24+
}
25+
26+
public static void retoFinal(){
27+
double result = Calculator.calculate(Calculator.Operations.SUMAR, 7, 8);
28+
System.out.println("7 + 8 = " + result);
29+
30+
result = Calculator.calculate(Calculator.Operations.RESTAR, 1, 2.5);
31+
System.out.println("1 - 2.5 = " + result);
32+
33+
result = Calculator.calculate(Calculator.Operations.MULTIPLICAR, 1.3, 4.6);
34+
System.out.println("1.3 * 4.6 = " + result);
35+
36+
result = Calculator.calculate(Calculator.Operations.DIVIDIR, 2, 1.5);
37+
System.out.println("2 / 1.5 = " + result);
38+
39+
result = Calculator.calculate(Calculator.Operations.POTENCIA, 5, 5);
40+
System.out.println("5 ^ 5 = " + result);
41+
}
42+
43+
public static class WrongHuman{}
44+
public static class WrongCat{}
45+
public static class WrongPigeon {}
46+
47+
public interface Animal{
48+
public void move();
49+
}
50+
public static class Human implements Animal{
51+
public void move(){
52+
System.out.println("Los humanos se mueven a dos patas");
53+
}
54+
}
55+
public static class Cat implements Animal{
56+
public void move(){
57+
System.out.println("Los gatos se mueven a cuatro patas");
58+
}
59+
}
60+
public static class Pigeon implements Animal{
61+
public void move(){
62+
System.out.println("Las palomas se mueven por el aire volando");
63+
}
64+
}
65+
66+
public class Calculator{
67+
public enum Operations{
68+
SUMAR(new Sum()),
69+
RESTAR(new Subtract()),
70+
MULTIPLICAR(new Multiply()),
71+
DIVIDIR(new Divide()),
72+
POTENCIA(new Power());
73+
74+
private Operation operation;
75+
76+
Operations (Operation operation){
77+
this.operation = operation;
78+
}
79+
80+
public Operation getOperation(){
81+
return operation;
82+
}
83+
84+
}
85+
86+
public static double calculate(Operations operation, double n1, double n2){
87+
return operation.getOperation().execute(n1, n2);
88+
}
89+
90+
}
91+
public interface Operation{
92+
public double execute(double n1, double n2);
93+
}
94+
public static class Sum implements Operation{
95+
96+
public double execute(double n1, double n2) {
97+
return n1 + n2;
98+
}
99+
}
100+
public static class Subtract implements Operation{
101+
102+
@Override
103+
public double execute(double n1, double n2) {
104+
return n1 - n2;
105+
}
106+
}
107+
public static class Multiply implements Operation{
108+
109+
@Override
110+
public double execute(double n1, double n2) {
111+
return n1 * n2;
112+
}
113+
}
114+
public static class Divide implements Operation{
115+
116+
@Override
117+
public double execute(double n1, double n2) {
118+
return n1 / n2;
119+
}
120+
}
121+
public static class Power implements Operation{
122+
123+
@Override
124+
public double execute(double n1, double n2) {
125+
double result = 1;
126+
for (int i = 0; i < n2; i++)
127+
result *= n1;
128+
return result;
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)