Skip to content

Commit 30984fe

Browse files
authored
Merge pull request mouredev#7168 from Josegs95/main
#28 y #29 - Java
2 parents 48a73df + 938fdc4 commit 30984fe

File tree

2 files changed

+311
-0
lines changed

2 files changed

+311
-0
lines changed
+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}
+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)