车{
next();开往下一站
stop();下客,上客,报站名,算盈利
main(){
  while(bus.next())
    stop();
}
}乘客{
始发
目的
车费
}

解决方案 »

  1.   

    这道题有点意思,我调试了一下,贴出来大家瞧一眼。//Source file: F:\myJava\bus\sharetop\Bus.javapackage sharetop;import java.util.*;public class Bus 
    {
       public static int MAXSN=20;
      // public static final int MAXCOUNT=60;
       
       private double cost=0.0;
       public Vector passengers=null;
       public Vector stations=null;
       
       public String name=null;
       
       public Bus(String n,Vector sta) 
       {
        this.name=n;
        passengers=new Vector();
        stations=sta;
        if( sta!=null )this.MAXSN=sta.size();
       }
       
       /**
    run on the line of bus.and report station name
       @roseuid 3C495D0000AB
       */
       public void run() 
       {
        for(int i=0;i<stations.size();i++){
        Station s = (Station)stations.get(i);
        System.out.println("==current station is "+s.report());
        System.out.println("  current passengers count 000 is"+passengers.size());
        down(i);
        System.out.println("  current passengers count 111 is"+passengers.size());
        up(i);
        System.out.println("  cost="+cost);
        }
       }
       
       /**

       @roseuid 3C495D3C0129
       */
       private void down(int sn) 
       {
        if( passengers==null || passengers.size()==0 ) return;

    Vector downpassengers = new Vector();
        for(Enumeration e=passengers.elements();e.hasMoreElements();){
        Passenger p = (Passenger)e.nextElement();
        if( p.end==sn ){
        int d = ((Station)(stations.get(p.end))).getDistance()
            - ((Station)(stations.get(p.start))).getDistance();
       
        if( d<10 ) cost+=1;
        else {
        int dd = d-10;
        cost += 1+Math.round(dd/5.0)*0.5; 
        }
        System.out.println("  passenger down cost = "+cost);
        downpassengers.add(p);
        }
        }
        passengers.removeAll(downpassengers);
       }  private void up(int sn)
      {
       Station s = (Station)stations.get(sn);
       passengers.addAll(s.up());
      }
       
    }
    //Source file: F:\myJava\bus\sharetop\Passenger.javapackage sharetop;
    public class Passenger 
    {
       public int start=-1;
       public int end=-1;
       
       public Passenger() 
       {
       }
       
       /**
       when the passenger up bus,he decide the target station.
       so the up method initalized the start- and end- station sn.
       @roseuid 3C49586F023A
       */
       public void up(int s)
       {
        this.start=s;
        this.end = new java.util.Random().nextInt(Bus.MAXSN);
       
        //only forward.
        while(this.end<=this.start)
        this.end = new java.util.Random().nextInt(Bus.MAXSN);
       
        System.out.println("  passenger up from "+this.start +"===>"+this.end);
       }
    }
    //Source file: F:\myJava\bus\sharetop\Station.javapackage sharetop;import java.util.*;public class Station 
    {
       private String name=null;
       private int distance=0;
       private int sn=0;
       private boolean isEnd = false;
       
       public Station(int s,String n,int d) 
       {
        this.sn=s;
        this.name=n;
        this.distance=d;
       }
       
       public void setFinish(){this.isEnd=true;}
       /**
       @roseuid 3C4956EF0102
       */
       public Vector up() 
       {
        int num = new java.util.Random().nextInt(12);
       
        if( this.isEnd ) num=0;
       
        Vector res = new Vector();
       
        for(int i=0;i<num;i++){
        Passenger p = new Passenger();
        p.up(this.sn);
        res.add(p);
        }
        return res;
       }
       
       /**
       @roseuid 3C49571C0341
       */
       public String report() 
       {
        return this.name;
       }
       
       public int getDistance()
       {
        return this.distance;
       }
    }//source file ABus.javaimport java.util.*;
    import sharetop.*;public class ABus 
    {
    public static void main(String[] args) {

    Vector allStations = new Vector();

    Station s1 = new Station(0,"qinghe",0);
    allStations.add(s1);

    Station s2 = new Station(1,"majagou",2);
    allStations.add(s2);

    Station s3 = new Station(2,"xuezhiyuan",6);
    allStations.add(s3); Station s4 = new Station(3,"xuefuyuan",7);
    allStations.add(s4); Station s5 = new Station(4,"qinghuadongru",8);
    allStations.add(s5); Station s6 = new Station(5,"beihang",9);
    allStations.add(s6); Station s7 = new Station(6,"xueyuangru",10);
    allStations.add(s7); Station s8 = new Station(7,"mingguangchueng",13);
    allStations.add(s8); Station s9 = new Station(8,"xizhimeng",19);
    s9.setFinish();
    allStations.add(s9);


    Bus b = new Bus("902",allStations);
    b.run();
    }
    }
      

  2.   


    还有个问题,这辆车不管上多少人全部照收,北京的公共汽车:)
    可以在Bus类的up方法中对此加以处理。我用了三个类:车站、乘客和汽车来处理这个问题。
      

  3.   

    to 天生不笨:清河哪里呀,我对清河很熟哟,说来听听,不过我现不在那边,另外,评论一下北京的it环境如何
      

  4.   

    紧急插播请各位高手帮忙给看看,题目很简短的,很急呀,麻烦大家了http://www.csdn.net/expert/topic/490/490173.shtmhttp://www.csdn.net/expert/topic/490/490039.shtmhttp://www.csdn.net/expert/topic/490/490022.shtm 
      

  5.   

    to sharetop(天生不笨):    看过您的代码,总感觉有些欠缺(当然是我自己的偏见)
        似乎面向对象的概念体现的不明显,是不是这个课题有些
        简单?体现不出来?还是设计的问题?哦,对了,分我是一定会给的,这对我并不重要~~~~
      

  6.   

    to wobuzhidao_yes:
    这么说话就是您老兄的不对了,人家sharetop调试代码容易吗?难道就为了你那点分呀?
    分不就是一个形式吗?好像谁很在乎似的!!
      

  7.   

    是啊,分不是很重要,但是对我来说还是有点用处的,来的少,赚的少,如果再不来点分,就做不成斑竹了,做不成斑竹,就更不想来了……,总之恶性循环。这个题目的OO思想不足,是吧,我也没有特意去想如何OO,实在是………我的想法就是:车:
    属性:全部乘客、收的车费、要停的站
    动作:跑、下乘客、上乘客乘客:
    属性:从哪上的、要去哪
    动作:上车以上好象没什么问题,车站的定义就不太好了。车站:
    属性:站名、离始发距离、是否终点 
    动作:收集乘客送上车因为只有一趟车一条线,所以车站的功能就专一了,其实现实中一个车站不应知道它与始发站的距离,也不知道它是否终点。而车站能产生(汇集)乘客,但不能站乘客上车。是吧?