|
| 1 | +import java.util.HashMap; |
| 2 | +import java.util.Map; |
| 3 | + |
| 4 | +public class danhingar { |
| 5 | + |
| 6 | + public static void main(String[] args) { |
| 7 | + Form1 square = new Square(); |
| 8 | + square.draw(); |
| 9 | + |
| 10 | + Form1 circle = new Circle(); |
| 11 | + circle.draw(); |
| 12 | + |
| 13 | + Form1 triangle = new Triangle(); |
| 14 | + triangle.draw(); |
| 15 | + |
| 16 | + //Extra |
| 17 | + Calculator calculator = new Calculator(); |
| 18 | + |
| 19 | + calculator.addOperation("addition", new Addition()); |
| 20 | + calculator.addOperation("substration", new Substration()); |
| 21 | + calculator.addOperation("multiplication", new Multiplication()); |
| 22 | + calculator.addOperation("division", new Division()); |
| 23 | + calculator.addOperation("power", new Power()); |
| 24 | + |
| 25 | + System.out.printf("Resultado %f\n",calculator.calculate("addition",5.0,3.5)); |
| 26 | + System.out.printf("Resultado %f\n",calculator.calculate("substration",10.0,4.0)); |
| 27 | + System.out.printf("Resultado %f\n",calculator.calculate("multiplication",5.0,3.0)); |
| 28 | + System.out.printf("Resultado %f\n",calculator.calculate("division",20.0,5.0)); |
| 29 | + System.out.printf("Resultado %f\n",calculator.calculate("power",2.0,2.0)); |
| 30 | + System.out.printf("Resultado %f\n",calculator.calculate("example",2.0,2.0)); |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +//Incorrecta |
| 37 | +class Form { |
| 38 | + |
| 39 | + public void drawSquare(){ |
| 40 | + System.out.println("Dibujar un cuadrado"); |
| 41 | + } |
| 42 | + |
| 43 | + public void drawCircle(){ |
| 44 | + System.out.println("Dibujar un círculo"); |
| 45 | + } |
| 46 | + |
| 47 | + public void drawTriangle(){ |
| 48 | + System.out.println("Dibujar un círculo"); |
| 49 | + } |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +//Forma correcta |
| 54 | + |
| 55 | +interface Form1 { |
| 56 | + |
| 57 | + void draw(); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +class Square implements Form1 { |
| 62 | + |
| 63 | + @Override |
| 64 | + public void draw() { |
| 65 | + System.out.println("Dibuja un cuadrado"); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +} |
| 70 | + |
| 71 | +class Circle implements Form1 { |
| 72 | + |
| 73 | + @Override |
| 74 | + public void draw() { |
| 75 | + System.out.println("Dibuja un círculo"); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +class Triangle implements Form1 { |
| 82 | + |
| 83 | + @Override |
| 84 | + public void draw() { |
| 85 | + System.out.println("Dibuja un triángulo"); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +//EXTRA |
| 94 | + |
| 95 | +interface Operation { |
| 96 | + |
| 97 | + Double operate(Double num1,Double num2); |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +class Addition implements Operation { |
| 102 | + |
| 103 | + @Override |
| 104 | + public Double operate(Double num1, Double num2) { |
| 105 | + return num1+num2; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +class Substration implements Operation { |
| 110 | + |
| 111 | + @Override |
| 112 | + public Double operate(Double num1, Double num2) { |
| 113 | + return num1-num2; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +class Multiplication implements Operation { |
| 118 | + |
| 119 | + @Override |
| 120 | + public Double operate(Double num1, Double num2) { |
| 121 | + return num1*num2; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +class Division implements Operation { |
| 126 | + |
| 127 | + @Override |
| 128 | + public Double operate(Double num1, Double num2) { |
| 129 | + return num1/num2; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +class Power implements Operation { |
| 134 | + |
| 135 | + @Override |
| 136 | + public Double operate(Double num1, Double num2) { |
| 137 | + return Math.pow(num1, num2); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | + class Calculator { |
| 142 | + |
| 143 | + Map<String,Operation> operations; |
| 144 | + |
| 145 | + public Calculator(){ |
| 146 | + this.operations = new HashMap<>(); |
| 147 | + } |
| 148 | + |
| 149 | + public void addOperation(String name, Operation operation){ |
| 150 | + operations.put(name, operation); |
| 151 | + } |
| 152 | + |
| 153 | + public double calculate(String name, double num1, double num2){ |
| 154 | + if(!operations.containsKey(name)){ |
| 155 | + throw new IllegalArgumentException("La operación "+name+" no es válida"); |
| 156 | + } |
| 157 | + return operations.get(name).operate(num1, num2); |
| 158 | + } |
| 159 | + |
| 160 | + |
| 161 | +} |
0 commit comments