天天看點

黑馬程式員——Java面試題之交通燈排程系統

面對對象設計

面對對象設計思想:把方法定義在合适的對象上。

人在黑闆上畫圓,其實是調用圓的構造方法。

司機刹車,實際上刹車的方法在車上面,我們隻是調用刹車的方法,刹車的方法在車上。

class Circle1{

           double r ;

           public Circle1( double r) {

                    this. r = r;

           }

}

new對象就能建立一個圓,計算機的世界也不等同現實世界,不能說徒手畫得不圓,不圓的是曲線就不是圓了。

路上的汽車減少,是路增加一個汽車或者減少一個汽車

小球怎樣從繩子一端移動到另一端

//假設繩子由n點Point組成,小球是point0

class wire{

           int len ;

           ArrayList al= new ArrayList() ;

//假設繩子由n點Point組成,小球是point0          

 wire ( int len ){

                    this. len= len;

                    for (int i= 0; i< len; i++ )

                              al .add ("point" + i) ;

           //小球移動就是從這點到下一點移動直到走完全程

           public void move (){

                    for (int m= 0; m< len- 1; m++ ){

                             Collections .swap (al , m , m +1 );

                    }

public class thinkinginoop {

           public static void main ( String[] args ) {

                    // TODO Auto-generated method

stub

wire ww =new wire (10 );

ww .move ();

System .out .println ( ww. al) ;

/*      [point1,

point2, point3, point4, point5, point6, point7, point8, point9,

point0]*/

對象本身無法使自己變成另一個對象。

石頭磨成石刀或者木頭變成椅子

class Stone{

          String name= "Stone" ;

class StoneKnife{

          String name= "StoneKnife" ;

class StoneFactory{

           public StoneKnife

makeKinfe( Stone stone ){

                   StoneKnife

sk = new StoneKnife ();

                   stone = null;

                    return sk;

                   Stone

st = new Stone ();

System .out .println ( st. name) ;

StoneFactory sf =new StoneFactory ();

//石頭不存在了,隻剩下StoneKnife

System .out .println ( sf. makeKinfe( st ). name) ;

/*Stone

Stoneknife*/

交通燈排程系統

十字路口上總共有12條線路,每條路上的汽車數是随機添加的,當該路線上綠燈亮時,該線路上的汽車按一定時間減少,表示其通過路口,紅燈時不減少,但是添加會一直進行。

黑馬程式員——Java面試題之交通燈排程系統

張孝祥老師這道題做得挺漂亮的,下面學習一下張老師的代碼。

交通燈類的實作

public enum Lamp {

           S2N1 ("N2S5" ,"S2W2" , false) ,S2W2 ( "N2E6", "E2W3", false ), E2W3( "W2E7", "E2S4", false ), E2S4( "W2N8", "S2N1", false ),

           N2S5 (null , null, false) , N2E6( null, null, false) , W2E7( null, null, false) , W2N8( null, null, false) ,

           S2E9 (null , null, true) , E2N10( null, null, true) , N2W11( null, null, true) , W2S12( null, null, true) ;

           private Lamp( String

opposite, String

next, boolean lighted ) {

                    this. next = next;

                    this. opposite = opposite;

                    this. lighted = lighted;

           private String next ;

           private String opposite ;

           private boolean lighted;

           public boolean isLighted (){

                    return lighted ;

           public void lightOn (){

                    this. lighted= true;

                    if (! this. opposite. equals( null ))

                             Lamp .valueOf (opposite ). lighted= true;

                   System . out. println( name () + "

lamp is green,下面總共應該有6個方向能看到汽車穿過!" );

           public Lamp

lightOff(){

                    this. lighted= false;

                             Lamp .valueOf (opposite ). lighted= false;

                    if (! this. next. equals( null ))

                             Lamp .valueOf (next ). lightOn ();

                    return Lamp. valueOf( next) ;

首先這是一個枚舉類,十二條線路的交通燈,分成三種構造,現實的交通燈也是這樣的,分成4種情況。開燈表示綠燈亮,同時把該線路的相反方向的燈也打開,關閉也同理,關閉的同時打開下一條線路的燈,如此循環不斷。

Lamp系統

用于控制線路等的開關間隔。

public class LampSystem {

           private Lamp currentlamp ;

          LampSystem (){

                    currentlamp= Lamp. S2N1;

                    currentlamp. lightOn ();

                   ScheduledExecutorService

contrl =Executors .newScheduledThreadPool ( 1) ;

                   contrl .scheduleAtFixedRate ( new Runnable (){

               public void run () {

                                       currentlamp= currentlamp. lightOff ();     

                                       // TODO Auto-generated method

                              }}, 10, 10, TimeUnit. SECONDS );

這裡采用的是調控線程,每隔一段時間就會執行一次run,非常好用的時間控制器。因為Lamp對象隻知道自己怎麼關燈和怎麼開燈,但是什麼時候開,什麼時候關,它是不知道的,這個事情Lamp系統才是最清楚的。

Road類

用于描述線路的車輛添加和移除。

public class Road {

private List< String> Vechicles = new ArrayList <String >() ;

private String name ;

public Road( String

name ) {

           super ();

           this. name = name;

          ExecutorService

addcar =Executors .newSingleThreadExecutor ();

          addcar .execute ( new Runnable (){

                    public void run () {

                              for (int i= 1; i< 1000; i++ ){

                                       try {

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

                                       } catch (Exception e ) {

                                                 // TODO: handle exception

                                                e .printStackTrace ();

                                       }

                             Road . this. Vechicles. add( "vechicle" +"i" );          

                              }

           });

          ScheduledExecutorService

cross =Executors .newScheduledThreadPool ( 1) ;

          cross .scheduleAtFixedRate ( new Runnable (){

                              // TODO Auto-generated method

                              if (Road . this. Vechicles. size() >0 ){

                              if (Lamp .valueOf (Road . this. name) .isLighted ())   

                                       System . out. println( Road. this. name+ ">>>" +Road . this. Vechicles. remove( 0 ) + "

is traversing !" );

                    }, 1, 1, TimeUnit. SECONDS ); }

這裡用的是兩條獨立的線程,一條線程用于添加車輛,每條線路都能随機時間(1到10秒)添加汽車。另一條線程是用來描述汽車通過路口的情形,當綠燈時,檢測線路上是否有車,有車就移除前面一輛,沒有就等待,下一秒繼續檢測。

測試類

測試系統。

      // TODO Auto-generated method

String [] directions= {"S2N1" ,"S2W2" ,"E2W3" ,"E2S4" ,"N2S5" ,"N2E6" ,"W2E7" ,"W2N8" ,"S2E9" ,"E2N10" ,"N2W11" ,"W2S12" };

for (int i= 0; i< directions. length; i++ )         

new Road( directions [i ]);

new LampSystem() ;

/*      S2N1

lamp is green,下面總共應該有6個方向能看到汽車穿過!

          W2S12>>> vechiclei is

traversing !

          N2S5>>> vechiclei is

          S2N1>>> vechiclei is

          S2E9>>> vechiclei is

          E2N10>>> vechiclei is

          N2W11>>> vechiclei is

          S2W2

lamp is green,下面總共應該有6個方向能看到汽車穿過!*/

把線路對象和Lamp系統對象建構好就行。會自動執行構造函數裡的線程。