How to implement Enum in java, poo

0

I'm starting to see the issue of enum in java and I have an exercise in which I'm asked to check which quadrant of the Cartesian plane are certain points, these points are stored in an ArrayList and the teacher told me to Through a enum I could find out in which quadrant the point that is being analyzed could be and I could also tell if one of those points does not have a quadrant (one or two of the X and Y coordinates has a value of 0). The problem is that I can not think of how to use enum , someone to help me? I'm starting with that of la la poo.

I pass the classes:

package arraylist;

public class Punto {

    private Double x;
    private Double y;

    public Punto(Double x, Double y) {
        this.setX(x);
        this.setY(y);
    }

    public Punto(Double xy) {
        this(xy, xy);
    }

    public Punto() {
        this(0.0);
    }

    public Double getX() {
        return this.x;
    }

    public void setX(Double x) {
        this.x = x;
    }

    public Double getY() {
        return this.y;
    }

    public void setY(Double y) {
        this.y = y;
    }

    public Double calcularDistancia(Punto punto) {
        return Math.hypot(this.getX() - punto.getX(), this.getY() - punto.getY());
    }

    public String toString() {
        return this.getX() + "," + this.getY();
    }

    public int getcuadrante() {

        int cuadrante = 0;

        if(x>0 && y>0) {
            cuadrante = 1;
        }
        if(x<0 && y>0) {
            cuadrante = 2;
        }
        if(x<0 && y<0) {
            cuadrante = 3;
        }
        if(x>0 && y<0) {
            cuadrante = 4;
        }
        return cuadrante;
    }

}
package arraylist;

import java.util.ArrayList;

public class Plano {


    private ArrayList<Punto> Puntos;



    public ArrayList<Punto> getPuntos() {
        return Puntos;
    }

    public Plano() {
        this.setPuntos(new ArrayList<Punto>());
    }

    public void agregarPunto(Punto punto) {
        this.getPuntos().add(punto);
    }

    public Integer getCantPuntos(Cuadrante cua) {
        Integer cont = 0;
        for(Punto punto: this.getPuntos()) {
            if(punto.getcuadrante(cua.getX(),cua.getY()).equals(cua)) {
                cont++;
            }
            return cont;
        }
    }

    public void setPuntos(ArrayList<Punto> puntos) {
        Puntos = puntos;
    }


}
package arraylist;

import java.util.ArrayList;

public class UsoArrayList {

    public static void main(String[] args ) {

        ArrayList<Punto> vector = new ArrayList<Punto>();

        System.out.println("Tamaño: " + vector.size());



    }
}

What would be missing would be the enumerator, but I do not know how to implement it.

    
asked by Jorge DeSpringfield 27.09.2018 в 05:50
source

1 answer

2

The first thing would be to declare the enumeration:

enum Cuadrante {
    UNO, DOS, TRES, CUATRO
}

Then you would change the getCuadrante function:

public Cuadrante getCuadrante() {
    if (x > 0 && y > 0) {
        return Cuadrante.UNO;
    }

    if (x < 0 && y > 0) {
        return Cuadrante.DOS;
    }

    if (x < 0 && y < 0) {
        return Cuadrante.TRES;
    }

    return Cuadrante.CUATRO;
}

I have removed the last if of the function since it is not necessary

    
answered by 27.09.2018 в 08:01