Handling of JAVA Attributes

Asked

Viewed 179 times

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.

  • 1

    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.

  • Good afternoon Murilo. Face the constructor you mean the Quadrilateral class? Or you say the rectangular class?

  • Good afternoon, I speak of the Rectangular class.

1 answer

1


You can assign the values of X and Y to Z and W respectively by creating a constructor in the Parent class:

public Quadrilateros(double x, double y) {
    this.x = x;
    this.y = y;
    this.z = x;
    this.w = y;
}

Calling in the rectangle class as:

public Retangulo(String objNome, double x, double y){
    super(x, y);
    this.objNome = "Retangulo";
}

That way, in the daughter class Retangulo, you can just overwrite the area calculation method:

@Override
public double area(){
    return super.x * super.y;
}
  • I realized here, it worked I was not understanding what happened in this excerpt: public Rectangle(String objNome, double x, double y){ super(x, y);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.