0
Good afternoon, I’m with a college job and it’s the first semester I’m learning java.
The job basically is:
Make an interface that would represent any geometric figure, in it specify methods of area and perimeter;
Make an abstract class that would represent any quadrilateral and that receives 4 attributes in the constructor (x,y,z,w) and already implement the method perimeter interface.
Make a rectangular class, and has only the attributes (base and height) and inherits everything in the quadrilateral class, which as I mentioned has Mplements of the geometric figure.
I’m having doubts right here:
The parent class receives 4 attributes in the constructor (x,y,z,w) and for the area and perimeter calculations I only need 2 (base and height), so talking to the teacher he commented something of manipulating the attributes so that every 2 were 1. Ex: my parent class has x,y,z,w as attributes; The rectangular daughter class has base, height as attributes. The idea is that the daughter associates base with x,y (I have no idea how, but it was what he suggested) and height with z,w.
Follow the code excerpt from my program, just don’t notice why it’s quite beginner yet.
public interface FigGeometrica {
double perimetro();
double area();
String nome();
}
//classe pai abstrata.
abstract class Quadrilateros implements FigGeometrica{
public double x;
public double y;
public double z;
public double w;
public Quadrilateros(double x, double y, double z, double w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public double perimetro(){
return x+y+z+w;
}
}
//Classe filha que herda Quadrilateros
public class Retangulo extends Quadrilateros{
private String objNome;
public Retangulo(String objNome, double x, double y, double z, double w){
super(x, y, z, w);
this.objNome = "Retangulo";
}
@Override
public double perimetro(){
return 2*super.x+2*super.y;
}
@Override
public double area(){
return super.x*super.y;
}
As you can see the z and w will appear in the builder, but here I would not use for anything at all. I want to make sure I only ask for the necessary on base / height builder only.
Has anyone ever used know what I should seek to learn and perform this manipulation? Thanks in advance.
Remove these attributes from the constructor, as you have already changed it by adding the targetObj. Go to super something like (x, y, x, y), since both sides are guais.
– Murillo Goulart
Good afternoon Murilo. Face the constructor you mean the Quadrilateral class? Or you say the rectangular class?
– Marcio Henrique
Good afternoon, I speak of the Rectangular class.
– Murillo Goulart