日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

黑马程序员_面试题(一)交通灯管理系统

發布時間:2024/1/1 windows 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 黑马程序员_面试题(一)交通灯管理系统 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

android培訓、java培訓、期待與您交流!










【Road類】:

每個Road對象都有一個name成員變量來代表方向,有一個vehicles成員變量來代表方向上的車輛集合。

在Road對象的構造方法中啟動一個線程每隔一個隨機的時間向vehicles集合中增加一輛車(用一個“路線名_id”形式的字符串進行表示)。

在Road對象的構造方法中啟動一個定時器,每隔一秒檢查該方向上的燈是否為綠,是則打印車輛集合和將集合中的第一輛車移除掉。

?

public class Road {

????? privateArrayList<String> vehicles = new ArrayList<String>();

????? private String name;

????

????? public Road(Stringname){

?????????? this.name = name;

?????????? ExecutorServicepool = Executors.newSingleThreadExecutor();

?????????? pool.execute(newRunnable(){

???????????????? public voidrun(){

????????????????????? for(inti=1;i<1000;i++){

???????????????????????????try {

?????????????????????????????????Thread.sleep((new Random().nextInt(10) + 1) * 1000);

??????????????????????????? }catch (InterruptedException e) {

?????????????????????????????????e.printStackTrace();

???????? ???????????????????}

???????????????????????????vehicles.add(Road.this.name + "_" + i);

????????????????????? }???????????????????

???????????????? }????????

?????????? });

?????????

??????????ScheduledExecutorService timer =?Executors.newScheduledThreadPool(1);

??????????timer.scheduleAtFixedRate(

????????????????????? newRunnable(){

???????????????????????????public void run(){

?????????????????????????????????if(vehicles.size()>0){

?????????????????????????????????????? booleanstatus = Lamp.valueOf(Road.this.name).getStatus();

??????????????????????????????????????if(status){

????????????????????????????????????????????System.out.println(vehicles.remove(0) + " is traversing !");

?????????????????????????????????????? }

?????????????????????? ???????????}

????????????????????????????????

??????????????????????????? }

????????????????????? },

????????????????????? 1,

????????????????????? 1,

?????????????????????TimeUnit.SECONDS);

????? }

}

?

【Lamp類】:

系統中有12個方向上的燈,在程序的其他地方要根據燈的名稱就可以獲得對應的燈的實例對象,綜合這些因素,將Lamp類用java5中的枚舉形式定義更為簡單。

每個Lamp對象中的亮黑狀態用lighted變量表示,選用S2N、S2W、E2W、E2N這四個方向上的Lamp對象依次輪詢變亮,Lamp對象中還要有一個oppositeLampName變量來表示它們相反方向的燈,再用一個nextLampName變量來表示此燈變亮后的下一個變亮的燈。這三個變量用構造方法的形式進行賦值,因為枚舉元素必須在定義之后引用,所以無法再構造方法中彼此相互引用,所以,相反方向和下一個方向的燈用字符串形式表示。?

增加讓Lamp變亮和變黑的方法:light和blackOut,對于S2N、S2W、E2W、E2N這四個方向上的Lamp對象,這兩個方法內部要讓相反方向的燈隨之變亮和變黑,blackOut方法還要讓下一個燈變亮。

除了S2N、S2W、E2W、E2N這四個方向上的Lamp對象之外,其他方向上的Lamp對象的nextLampName和oppositeLampName屬性設置為null即可,并且S2N、S2W、E2W、E2N這四個方向上的Lamp對象的nextLampName和oppositeLampName屬性必須設置為null,以便防止light和blackOut進入死循環。

public enum Lamp {

?????S2N("N2S","S2W",false),S2W("N2E","E2W",false),E2W("W2E","E2S",false),E2S("W2N","S2N",false),

?????N2S(null,null,false),N2E(null,null,false),W2E(null,null,false),W2N(null,null,false),????

?????S2E(null,null,true),E2N(null,null,true),N2W(null,null,true),W2S(null,null,true);

????

????? private Lamp(Stringcomposite,String nextLamp,boolean status){

?????????? this.opposite =composite;

?????????? this.nextLamp =nextLamp;

?????????? this.status =status;

????? }

????? public booleangetStatus(){

?????????? return status;

????? }

????? public voidsetStatus(boolean status){

?????????? this.status =status;

????? }

????? public LampgetNextLamp(){

???? ??????return Lamp.valueOf(this.nextLamp);

????? }

????

????? public voidturnStatus(){

?????????? if (getStatus()) {

????????????????setStatus(false);

????????????????System.out.println(this.name()+"turns from green to red");

???????????????? if (opposite!= null) {//一定要判斷,否則進入無限遞歸

?????????????????????Lamp.valueOf(opposite).turnStatus();

???????????????? }

???????????????? if(nextLamp!= null){ //一定要判斷,否則進入無限遞歸

?????????????????????Lamp.valueOf(nextLamp).turnStatus();

???????????????? }

????????????? ?????????????

?????????? }else{

????????????????setStatus(true);

????????????????System.out.println(this.name()+"turns from red to green");

???????????????? if (opposite!= null) {//一定要判斷,否則進入無限遞歸

?????????????????????Lamp.valueOf(opposite).turnStatus();???????

???????????????? }

?????????? }

????? }

?

????? //注釋掉的部分是張老師的原來代碼,我講light和blackOut方法整合成了turnStatus函數

???? / *public void light(){

???????? this.status = true;

???????? if(opposite != null){

?????????????Lamp.valueOf(opposite).light();

???????? }

????????System.out.println(name() + " lamp is green");

???????

???? }

???

?

???? public Lamp blackOut(){

???????? this.status = false;

???????? if(opposite != null){

?????????????Lamp.valueOf(opposite).blackOut();

???????? }??????

???????

???????? Lamp tempnextLamp=null;

???????? if(nextLamp != null){

????????????? tempnextLamp =Lamp.valueOf(nextLamp);

?????????????System.out.println("" + name() + "-------->" +nextLamp);???????????

?????????????tempnextLamp.light();

???????? }

???????? return tempnextLamp;

???? }* /

????

????? private String opposite=null;

????? private String nextLamp= null;

????? private boolean status =false;

}

?

【LampController類】:

整個系統中只能有一套交通燈控制系統,所以,LampController類最好是設計成單例。

LampController構造方法中要設定第一個為綠的燈。

LampController對象的start方法中將當前燈變綠,然后啟動一個定時器,每隔10秒將當前燈變紅和將下一個燈變綠。

public class LampController {

????? private LampcurrentLamp;

????

????? public LampController(){

?????????? currentLamp =Lamp.S2N;

??????????currentLamp.turnStatus();

??????????//currentLamp.light();

?????????

??????????ScheduledExecutorService timer =?Executors.newScheduledThreadPool(1);

??????????timer.scheduleAtFixedRate(

????????????????????? newRunnable(){

???????????????????????????public? void run(){

????????????????????????????????currentLamp.turnStatus();

?????????????????????????????????currentLamp = currentLamp.getNextLamp();

?????????????????????????????????//currentLamp = currentLamp.blackOut();

????????????????????? }

????????????????????? },

????????????????????? 10,

?????????? ???????????10,

?????????????????????TimeUnit.SECONDS);

????? }

}

?

【MainClass類】:

l

用for循環創建出代表12條路線的對象。

l

接著再獲得LampController對象并調用其start方法。

public class MainClass {

?

????? public static voidmain(String[] args) {

?????????? String[] directions= new String[]{

?????????????????????"S2N","S2W","E2W","E2S",

?????????????????????"N2S","N2E","W2E","W2N",??

?????????????????????"S2E","E2N","N2W","W2S"

?????????? };

?????????? for (String str:directions) {

???????????????? newRoad(str);

?????????? }

??? ????????new LampController();

????? }

}


總結

以上是生活随笔為你收集整理的黑马程序员_面试题(一)交通灯管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。