JAVA Attributes Handling

0

Good afternoon, I have a college job and it's the first semester I'm learning java.

The work basically is:

Make an interface that would represent any geometric shape, specify area and perimeter methods;

Make an abstract class that would represent any quadrilatero and that receives 4 attributes in the constructor (x, y, z, w) and already implements the interface perimeter method.

Make a rectangle class, and has only the attributes (base and height) and inherits everything that has in the quadrilateral class, which as I mentioned has implements of the geometric figure.

I'm right about this point:

The parent class gets 4 attributes in the constructor (x, y, z, w) and for the area and perimeter calculations I only need 2 (base and height), then talking to teacher he commented something about manipulating the attributes for each 2 to be 1. Eg: 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 associate base with x, y (I have no idea how, but it was what he suggested) and the height with z, w.

Follow the code snippet of my program, just do not notice why it is very 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 z and w will appear in the constructor, but here I would not use for absolutely nothing. I want to make sure I only get what is needed in the base / height only constructor.

Has anyone used know what I should look for to learn and perform this manipulation? Thank you in advance.

    
asked by anonymous 11.03.2017 / 20:53

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 the rectangle class as:

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

In this way, in the child class Retangulo , you can only override the area calculation method:

@Override
public double area(){
    return super.x * super.y;
}
    
12.03.2017 / 07:23