EjemploInterface.java 2.71 KB
Newer Older
Joel Florentin committed
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
public class EjemploInterface {
    public static void main(String[] args) {
        Forma cir = new Circulo( "Rojo",5.f);
        Forma cuad = new Rectangulo("Amarillo", 5.f, 3.f);
        Cartel cartel1 = new Cartel(cir, "Peligro");
        Cartel cartel2 = new Cartel(cuad, "Atencion");
        System.out.println(cartel1.cuadraTexto());
        System.out.println(cartel2.cuadraTexto());

    }
}

interface Forma{

    public String getColor();

    public void setColor(String color);

    public float getPerimetro();

    public float getArea();

    public float getLongitud();
}


class Cartel{

    private Forma forma;
    private String texto;

    public Cartel(Forma forma, String texto) {
        this.forma = forma;
        this.texto = texto;
    }

    public Forma getForma(){
        return this.forma;
    }

    public void setForma(Forma forma){
        this.forma = forma;
    }

    public String getTexto(){
        return this.texto;
    }

    public void setTexto(String texto){
        this.texto = texto;
    }

    public boolean cuadraTexto(){
        return this.texto.length() < this.forma.getLongitud();
    }
}

class Circulo implements Forma{
    private String color;
    private float radio;

    public Circulo(String color, float radio) {
        this.color = color;
        this.radio = radio;
    }

    @Override
    public String getColor() {
        
        return color;
    }

    @Override
    public void setColor(String color) {
        this.color = color;
        
    }

    @Override
    public float getPerimetro() {
        
        return (float)Math.PI*2*radio;
    }

    @Override
    public float getArea() {
        
        return (float)Math.PI*radio*radio;
    }

    @Override
    public float getLongitud() {
        // TODO Auto-generated method stub
        return 2*radio;
    }
    
} 

class Rectangulo implements Forma{
    private String color;
    private float largo;
    private float ancho;

    public Rectangulo(String color, float largo, float ancho) {
        this.color = color;
        this.largo = largo;
        this.ancho = ancho;
    }

    @Override
    public String getColor() {
        
        return color;
    }

    @Override
    public void setColor(String color) {
        this.color = color;
        
    }

    @Override
    public float getPerimetro() {
        
        return 2*(largo+ancho);
    }

    @Override
    public float getArea() {
        
        return largo*ancho;
    }

    public float getAncho() {
        return ancho;
    }
    public float getLongitud() {
        return largo;
    }
    public void setAncho(float ancho) {
        this.ancho = ancho;
    }
    public void setLongitud(float largo) {
        this.largo = largo;
    }
    
}