adsense

martes, 13 de agosto de 2013

crear un semaforo en java



public class ColorAmarillo {
    private String luz ;
    public ColorAmarillo()
    {
        this.luz = "amarillo"; 
    }
    public String getLuz()
    {
        return this.luz;
    }
}

// vamos a dividir todo en clases para poderlo manejar como objetos

public class ColorRojo {
    private String luz ;
    public ColorRojo()
    {
        this.luz = "rojo"; 
    }
    public String getLuz()
    {
        return this.luz;
    }
    
}

//separemos los colores

public class ColorVerde {
    private String luz ;
    public ColorVerde()
    {
        this.luz = "Verde"; 
    }
    public String getLuz()
    {
        return this.luz;
    }
    
}

//crearemos una clase temporizador que viene a ser el funcionamiento interno del semaforo

public class Temporizador {
    public void semaforoprendido(int velocidad) throws InterruptedException
    {
        ColorAmarillo a = new ColorAmarillo();
        ColorRojo r= new ColorRojo();
        ColorVerde v = new ColorVerde();
        while (true)
        {
            System.out.println(r.getLuz());
            Thread.sleep(velocidad);
            System.out.println(a.getLuz());;
            Thread.sleep(velocidad/2);
            System.out.println(v.getLuz());
            Thread.sleep(velocidad);
            Thread.sleep(velocidad/2);
        }
    }    
}


// la calse semaforo que es la principal o raiz

import javax.swing.JOptionPane;
public class Semaforo {
    public static void main(String[] args) throws InterruptedException
    {
        int vel=3000;
        try
        {
        vel = Integer.parseInt(JOptionPane.showInputDialog("digite la velocidad y ok para iniciar el semaforo"));
        }
        catch(Exception e)
        {
            vel=300;
        }
        Temporizador s = new Temporizador();
        s.semaforoprendido(vel);
    }
    
}

No hay comentarios:

Publicar un comentario