⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 passengersource.java

📁 现在在国外大学里最流行的java学习软件,同时还有大量的example,在名为project的文件里.安装好后用bluej打开peoject的例子,可以进行你想要的任何变化.同时可以了解大量的源码
💻 JAVA
字号:
import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Random;/** * Periodically generate passengers. * Keep track of the number of passengers for whom * a vehicle cannot be found. *  * @author David J. Barnes and Michael Kolling * @version 2006.03.30 */public class PassengerSource implements Actor{    private City city;    private TaxiCompany company;    private Random rand;    private static final double CREATION_PROBABILITY = 0.06;    private int missedPickups;    /**     * Constructor for objects of class PassengerSource.     * @param city The city. Must not be null.     * @param company The company to be used. Must not be null.     * @throws NullPointerException if city or company is null.     */    public PassengerSource(City city, TaxiCompany company)    {        if(city == null) {            throw new NullPointerException("city");        }        if(company == null) {            throw new NullPointerException("company");        }        this.city = city;        this.company = company;        // Use a fixed random seed for repeatable effects.        // Change this to produce more random effects.        rand = new Random(12345);        missedPickups = 0;    }    /**     * Randomly generate a new passenger.     * Keep a count of missed pickups.     */    public void act()    {        if(rand.nextDouble() <= CREATION_PROBABILITY) {            Passenger passenger = createPassenger();            if(company.requestPickup(passenger)) {                city.addItem(passenger);            }            else {                missedPickups++;            }        }    }    /**     * @return The number of passengers for whom a pickup     *         could not be found.     */    public int getMissedPickups()    {        return missedPickups;    }    /**     * Create a new passenger with distinct pickup and     * destination locations.     * @return The created passenger.     */    private Passenger createPassenger()    {        int cityWidth = city.getWidth();        int cityHeight = city.getHeight();        Location pickupLocation =                    new Location(rand.nextInt(cityWidth),                                 rand.nextInt(cityHeight));        Location destination;        do{            destination =                    new Location(rand.nextInt(cityWidth),                                 rand.nextInt(cityHeight));        } while(pickupLocation.equals(destination));        return new Passenger(pickupLocation, destination);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -