|
| 1 | +import java.util.Random; |
| 2 | + |
| 3 | +public class Josegs95 { |
| 4 | + public static void main(String[] args) { |
| 5 | + //Ejercicio |
| 6 | + //Forma incorrecta |
| 7 | + WrongAnimal animal1 = new WrongAnimal(); |
| 8 | + WrongAnimal snake1 = new WrongSnake(); |
| 9 | + WrongAnimal tiger1 = new WrongTiger(); |
| 10 | + |
| 11 | + animal1.putEggs(); |
| 12 | + snake1.putEggs(); |
| 13 | + tiger1.putEggs(); |
| 14 | + //Forma correcta |
| 15 | + OviparousAnimal animal2 = new OviparousAnimal(); |
| 16 | + OviparousAnimal snake2 = new Snake(); |
| 17 | + MammalAnimal animal3 = new MammalAnimal(); |
| 18 | + MammalAnimal tiger2 = new Tiger(); |
| 19 | + |
| 20 | + animal2.putEggs(); |
| 21 | + snake2.putEggs(); |
| 22 | + animal3.breastfeed(); |
| 23 | + tiger2.breastfeed(); |
| 24 | + |
| 25 | + //Reto |
| 26 | + new Josegs95().retoFinal(); |
| 27 | + } |
| 28 | + |
| 29 | + public void retoFinal(){ |
| 30 | + Random rnd = new Random(); |
| 31 | + Vehicle vehicle1 = new Car(50); |
| 32 | + Vehicle vehicle2 = new Train(100); |
| 33 | + Vehicle vehicle3 = new Plane(150); |
| 34 | + Vehicle[] vehicles = {vehicle1, vehicle2, vehicle3}; |
| 35 | + |
| 36 | + for (int i = 0; i < 50; i++){ |
| 37 | + if (rnd.nextBoolean()) |
| 38 | + vehicles[rnd.nextInt(vehicles.length)].accelerate(); |
| 39 | + else |
| 40 | + vehicles[rnd.nextInt(vehicles.length)].brake(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + //Clases incorrectas |
| 45 | + public static class WrongAnimal{ |
| 46 | + public void putEggs(){ |
| 47 | + System.out.println("El animal pone huevos"); |
| 48 | + } |
| 49 | + } |
| 50 | + public static class WrongSnake extends WrongAnimal{ |
| 51 | + @Override |
| 52 | + public void putEggs() { |
| 53 | + System.out.println("La serpiente pone huevos"); |
| 54 | + } |
| 55 | + } |
| 56 | + public static class WrongTiger extends WrongAnimal{ |
| 57 | + @Override |
| 58 | + public void putEggs() { |
| 59 | + //throw new UnsupportedOperationException("Los tigres no ponen huevos"); |
| 60 | + } |
| 61 | + } |
| 62 | + //Clases correctas |
| 63 | + public static class Animal{} |
| 64 | + public static class OviparousAnimal extends Animal{ |
| 65 | + public void putEggs() { |
| 66 | + System.out.println("El animal ovíparo pone huevos"); |
| 67 | + } |
| 68 | + } |
| 69 | + public static class MammalAnimal extends Animal{ |
| 70 | + public void breastfeed() { |
| 71 | + System.out.println("El animal mamífero da de mamar"); |
| 72 | + } |
| 73 | + } |
| 74 | + public static class Snake extends OviparousAnimal{ |
| 75 | + @Override |
| 76 | + public void putEggs() { |
| 77 | + System.out.println("La serpiente pone huevos"); |
| 78 | + } |
| 79 | + } |
| 80 | + public static class Tiger extends MammalAnimal{ |
| 81 | + @Override |
| 82 | + public void breastfeed() { |
| 83 | + System.out.println("El tigre da de mamar"); |
| 84 | + } |
| 85 | + } |
| 86 | + //Reto |
| 87 | + public class Vehicle{ |
| 88 | + private double speed; |
| 89 | + |
| 90 | + public Vehicle(double speed){ |
| 91 | + this.speed = speed; |
| 92 | + } |
| 93 | + |
| 94 | + public void accelerate(){ |
| 95 | + ++speed; |
| 96 | + } |
| 97 | + public void brake(){ |
| 98 | + if (speed > 0) |
| 99 | + --speed; |
| 100 | + } |
| 101 | + |
| 102 | + public double getSpeed() { |
| 103 | + return speed; |
| 104 | + } |
| 105 | + } |
| 106 | + public class Car extends Vehicle{ |
| 107 | + |
| 108 | + public Car(double speed) { |
| 109 | + super(speed); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void accelerate() { |
| 114 | + super.accelerate(); |
| 115 | + System.out.println("El coche acelera y su velocidad es de " + super.getSpeed() + "Km/h"); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void brake() { |
| 120 | + super.brake(); |
| 121 | + System.out.println("El coche frena y su velocidad es de " + super.getSpeed() + "Km/h"); |
| 122 | + } |
| 123 | + } |
| 124 | + public class Train extends Vehicle{ |
| 125 | + public Train(double speed) { |
| 126 | + super(speed); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void accelerate() { |
| 131 | + super.accelerate(); |
| 132 | + System.out.println("El tren acelera y su velocidad es de " + super.getSpeed() + "Km/h"); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void brake() { |
| 137 | + super.brake(); |
| 138 | + System.out.println("El tren frena y su velocidad es de " + super.getSpeed() + "Km/h"); |
| 139 | + } |
| 140 | + } |
| 141 | + public class Plane extends Vehicle{ |
| 142 | + public Plane(double speed) { |
| 143 | + super(speed); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public void accelerate() { |
| 148 | + super.accelerate(); |
| 149 | + System.out.println("El avión acelera y su velocidad es de " + super.getSpeed() + "Km/h"); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public void brake() { |
| 154 | + super.brake(); |
| 155 | + System.out.println("El avión frena y su velocidad es de " + super.getSpeed() + "Km/h"); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments