//Ejercicio //--------- //Diseñar un TAD que represente el comportamiento de un Semaforo Vehicular //UML : Unified Modelling Language class TSemaforo{ // Atributos private int estado; private int tr, ta, tv; // Constructor paramètrico public TSemaforo(int r, int a, int v){ tr = (r>0 && r<600)? r: 0; ta = (a>0 && a<600)? a: 0; tv = (v>0 && v<600)? v: 0; estado = 0; // Inicia en rojo } // Metodos examinadores public int getEstado(){ return estado;} public int getTr(){ return tr;} public int getTa(){ return ta;} public int getTv(){ return tv;} public String toString(){ String luz = "Luz :"; if (estado==0) luz+="Roja"; else if (estado==1) luz+="Amarilla"; else luz+="Verde"; luz+="\n"; return luz; } // Metodos modificadores // ... // Operaciones interesantes public void cambiar(){ switch(estado){ case 0: // Rojo try{ Thread.sleep(tr*1000); } catch(Exception e){} estado++; break; case 1:// Amarillo try{ Thread.sleep(ta*1000); } catch(Exception e){} estado++; break; case 2:// Verde try{ Thread.sleep(tv*1000); } catch(Exception e){} estado = 0; break; } } } public class Programa{ public static void main(String pars[]){ TSemaforo semaforo; semaforo = new TSemaforo(-4,1000,-2); for(int i=0; i<9; i++){ System.out.println( semaforo.toString() ); semaforo.cambiar(); } } }