交通模拟系统 (看到网上的题目自己用简单的方法实现了一下)

原帖地址

http://blog.csdn.net/zhangxiaoxiang/archive/2011/03/24/6273384.aspx

 

 

BlueJ 结构示意图

交通模拟系统 (看到网上的题目自己用简单的方法实现了一下)_第1张图片

 

/** * 在这里给出对类 Demo 的描述。 * * @作者(dada) * @版本(1.0) */ public class Demo { CarManager manager ; TrafficLight light ; public static final int RUNNINGTIME = 10 ; public Demo(){ manager = new CarManager(); light = new TrafficLight(); } public static void main(String[] args){ Demo demo = new Demo(); demo.start(RUNNINGTIME); } public void start(int time){ for(int i = 0 ;i < time ; i++){ try { Thread.sleep(1000); manager.run(light.getDirection()); System.out.println("========================1 Second=========================="); light.oneSecond(); } catch(Exception e){ System.out.print(e.getMessage()); } } } //public void wait }
/** * 在这里给出对类 TrafficLight 的描述。 * * @作者(dada) * @版本(1.0) */ public class TrafficLight { // 实例变量 - 用你自己的变量替换下面的例子 public static final int INTERVAL = 3 ; public static final int INTIDIRECTION = 0; private int direction ; private int time ; /** * 类 TrafficLight 的对象的构造函数 */ public TrafficLight() { direction = INTIDIRECTION; time = 0 ; } public void setDirection(int direction){ this.direction = direction; } public int getDirection(){ return this.direction; } public void change(){ if (direction == 0){ direction = 1; } else{ direction = 0; } } public void oneSecond(){ time++; if ((time % INTERVAL) == 0){ change(); System.out.println("Light Changed!"); } } }
import java.util.*; /** * 在这里给出对类 CarManager 的描述。 * * @作者(dada) * @版本(1.0) */ public class CarManager { // 实例变量 - 用你自己的变量替换下面的例子 public static final double POSSIBILITY = 0.8; public List<Car> turnRight; public List<Car> turnLeft; public List<Car> goStraight; /** * 类 CarManager 的对象的构造函数 */ public CarManager() { turnRight = new ArrayList<Car>(); turnLeft = new ArrayList<Car>(); goStraight = new ArrayList<Car>(); } public void run(int direction){ generateCar(); carGo(direction); } private void generateCar(){ Random r = new Random(); while(r.nextDouble() < POSSIBILITY){ Car temp = new Car(); int i = (temp.getCarGo() - temp.getCarFrom() + 4) % 4; if (i == 1){ turnRight.add(temp); } else if(i == 3){ turnLeft.add(temp); } else{ goStraight.add(temp); } } } private void carGo(int direction){ Boolean[] carDrive = new Boolean[12]; for (int i = 0; i < 12 ; i++){ carDrive[i] = false; } Iterator itRight = turnRight.iterator(); while (itRight.hasNext()){ Car car = (Car)itRight.next(); if(carDrive[car.getCarFrom()] == false){ carDrive[car.getCarFrom()] = true; System.out.println("Turn Right! "); car.go(); itRight.remove(); } } Iterator itStraight = goStraight.iterator(); boolean straight = false; while (itStraight.hasNext()){ Car car = (Car)itStraight.next(); if(car.getCarFrom() % 2 == direction){ if(carDrive[car.getCarFrom() + 4] == false){ carDrive[car.getCarFrom() + 4] = true; straight = true; System.out.println("Go Straight! "); car.go(); itStraight.remove(); } } } if (!straight){ Iterator itLeft = turnLeft.iterator(); while (itLeft.hasNext()){ Car car = (Car)itLeft.next(); if(car.getCarFrom() % 2 == direction){ if(carDrive[car.getCarFrom() + 8] == false){ carDrive[car.getCarFrom() + 8] = true; System.out.println("Turn Left! "); car.go(); itLeft.remove(); } } } } } }
import java.util.*; /** * 在这里给出对类 Car 的描述。 * * @作者(dada) * @版本(1.0) */ public class Car { // 实例变量 - 用你自己的变量替换下面的例子 private int carFrom; private int carGo; private Date time; Random r = new Random(); /** * 类 Car 的对象的构造函数 */ public Car() { setCarFrom(r.nextInt(4)); setCarGo(r.nextInt(4)); time = new Date(); } public void setCarFrom(int i){ carFrom = i; } public void setCarGo(int i){ while (i == getCarFrom()){ i = r.nextInt(4); } carGo = i; } public int getCarFrom(){ return carFrom; } public int getCarGo(){ return carGo; } public void go(){ System.out.println("at (" + time +") A car go through the crossing !" + " From: " + getCarFrom() + " To:" + getCarGo()); } }

你可能感兴趣的:(exception,manager,Random,iterator,Class,交通)