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

📄 location.java

📁 现在在国外大学里最流行的java学习软件,同时还有大量的example,在名为project的文件里.安装好后用bluej打开peoject的例子,可以进行你想要的任何变化.同时可以了解大量的源码
💻 JAVA
字号:
/** * Model a location in a city. *  * @author David J. Barnes and Michael Kolling * @version 2006.03.30 */public class Location{    private int x;    private int y;    /**     * Model a location in the city.     * @param x The x coordinate. Must be positive.     * @param y The y coordinate. Must be positive.     * @throws IllegalArgumentException If a coordinate is negative.     */    public Location(int x, int y)    {        if(x < 0) {            throw new IllegalArgumentException(                        "Negative x-coordinate: " + x);        }        if(y < 0) {            throw new IllegalArgumentException(                        "Negative y-coordinate: " + y);        }        this.x = x;        this.y = y;    }        /**     * Generate the next location to visit in order to     * reach the destination.     * @param destination Where we want to get to.     * @return A location in a direct line from this to     *         destination.     */    public Location nextLocation(Location destination)    {        int destX = destination.getX();        int destY = destination.getY();        int offsetX = x < destX ? 1 : x > destX ? -1 : 0;        int offsetY = y < destY ? 1 : y > destY ? -1 : 0;        if(offsetX != 0 || offsetY != 0) {            return new Location(x + offsetX, y + offsetY);        }        else {            return destination;        }    }    /**     * Determine the number of movements required to get     * from here to the destination.     * @param destination The required destination.     * @return The number of movement steps.     */    public int distance(Location destination)    {        int xDist = Math.abs(destination.getX() - x);        int yDist = Math.abs(destination.getY() - y);        return Math.max(xDist, yDist);    }        /**     * Implement content equality for locations.     * @return true if this location matches the other,     *         false otherwise.     */    public boolean equals(Object other)    {        if(other instanceof Location) {            Location otherLocation = (Location) other;            return x == otherLocation.getX() &&                   y == otherLocation.getY();        }        else {            return false;        }    }        /**     * @return A representation of the location.     */    public String toString()    {        return "location " + x + "," + y;    }    /**     * Use the top 16 bits for the y value and the bottom for the x.     * Except for very big grids, this should give a unique hash code     * for each (x, y) pair.     * @return A hashcode for the location.     */    public int hashCode()    {        return (y << 16) + x;    }    /**     * @return The x coordinate.     */    public int getX()    {        return x;    }    /**     * @return The y coordinate.     */    public int getY()    {        return y;    }}

⌨️ 快捷键说明

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