Implementar super()
Publicado por Fernando (26 intervenciones) el 20/07/2018 15:02:03
Hola gente, verán, estoy haciendo un curso de java. Estoy trabado en un ejercicio. Es de herencia y polimorfismo.
Básicamente tengo una clase Shape de donde se desprenden 2 clases, Circle y Rectangle, la clase Square hereda de la clase Rectangle. El problema puntual que tengo es el siguiente:
Para cuadrado:
f. Proporcione los constructores apropiados (como se muestra en el diagrama de clases). Pista:
public Square(double side) {
super(side, side); // Call superclass Rectangle(double, double)
}

Basicamente, no sé como implementar super(side, side), no sé como seguir a partir de ahi
Esto es lo que tengo hecho hasta ahora:
Muchas gracias desde yá
Básicamente tengo una clase Shape de donde se desprenden 2 clases, Circle y Rectangle, la clase Square hereda de la clase Rectangle. El problema puntual que tengo es el siguiente:
Para cuadrado:
f. Proporcione los constructores apropiados (como se muestra en el diagrama de clases). Pista:
public Square(double side) {
super(side, side); // Call superclass Rectangle(double, double)
}

Basicamente, no sé como implementar super(side, side), no sé como seguir a partir de ahi
Esto es lo que tengo hecho hasta ahora:
Clase Shape
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Shape {
protected String color = "red";
protected boolean filled = true;
public Shape() {};
public Shape(String color, boolean filled) {
this.color = color;
this.filled = filled;
};
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public String toString() {
return "Una figura de color " + this.getColor() + " y " + "llena " + this.isFilled();
}
}
Clase Circle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Circle extends Shape {
private double radius = 1.0;
Circle(){};
Circle(double radius){
this.radius = radius;
};
Circle(double radius, String color, boolean filled) {
this.radius = radius;
this.color = color;
this.filled = filled;
};
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double getArea() {
double area = Math.PI * Math.pow(this.radius, 2);
return area;
}
public double getPerimeter() {
double perimeter = (2 * Math.PI) * this.radius;
return perimeter;
}
public String toString() {
return "Una figura de color " + this.getColor() + " y " + "llena " + this.isFilled();
}
}
Clase Rectangle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class Rectangle extends Shape{
private double width = 1.0;
private double length = 1.0;
public Rectangle() {};
public Rectangle(double width, double length){
this.width = width;
this.length = length;
};
public Rectangle(double width, double length, String color, boolean filled) {
this.width = width;
this.length = length;
this.color = color;
this.filled = filled;
};
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
double area = this.width * this.length;
return area;
}
public double getPerimeter() {
double perimeter = (this.width * 2) + (this.length * 2);
return perimeter;
}
public String toString() {
return "Una figura de color " + this.getColor() + " y " + "llena " + this.isFilled();
}
}
Clase Square
1
2
3
4
5
6
7
8
9
10
public class Square extends Rectangle{
Square(){};
Square(double side) {
super(side, side);
};
Square(double side, String color, boolean filled) {
};
}
Muchas gracias desde yá
Valora esta pregunta


0