java - 与继承相关的家庭作业

标签 java

问候。我已经完成了一个学校项目,除了一个要求外,其他所有项目都已完成。

要求是:

请注意,SeaCreature 类为各个方向定义了四个常量。确保使用它们。您的代码不应依赖于分配给这些常量的特定值,尽管您可能假设它们始终是 int 类型。

我看不出这有什么值(value)。分配的常量值是合乎逻辑的,并且允许轻松使用 Random 返回方向。如果分配的值是四个不连续的数字,那么代码就会变得非常复杂。

下面是作业中提到的原始 SeaCreature 类,然后是我的一个使用 Random 的生物的代码。除了在涉及随机移动的每个类别中为北、南、东、西分配新值之外,是否有更干净的方法来返回基本方向而不是 0-3 的值?

预先感谢您的建议,如果需要任何说明,请询问。

import java.util.*;
/**
 * Defines the attributes and behaviors common to all SeaCreatures in order to participate
 * in the SeaCreature simulation.
 * 
 * Each SeaCreature is represented by a char.
 * Each SeaCreature can answer back its char when asked.
 * Each SeaCreature can answer its next move, NORTH, SOUTH, EAST, WEST.
 * 
 */

public abstract class SeaCreature {
    private char ch;

    /** defined constant to facilitate random movement*/
    public static final Random rand = new Random();

    /** defined constant to move one unit NORTH */
    public static final int NORTH = 0;

     /** defined constant to move one unit SOUTH */
    public static final int SOUTH = 1;

     /** defined constant to move one unit EAST */
    public static final int EAST = 2;

     /** defined constant to move one unit WEST */
    public static final int WEST = 3;


    /**
     * Construct a SeaCreature object with the given character representation
     * @param c  the character for this SeaCreature
     */
    public SeaCreature (char c){
        ch = c;
    }

    /** 
     * Answers back the character representation for this SeaCreature
     * @return this SeaCreature's initial
     */
    public char getChar(){
        return ch;
    }

    /** Answers back the next move for this SeaCreature.
     *  Must be overridden by subclasses
     * @return NORTH, SOUTH, EAST, or WEST
     */
    public abstract int getMove(); 
}

我的海狮类(class):

/**
 * Defines the attributes and behaviors specific to SeaLions in order to participate 
 * in the SeaCreature simulation.
 * 
 * @author Justin Ashburn
 * @version 6/5/2011
 */
public class SeaLion extends SeaCreature {

    /** defined char to represent a SeaLion*/
    private static final char SEALION = 'L';

    /** defined steps before SeaLion cycle repeats*/    
    private static final int CYCLE_LENGTH = 3;

    /** defined steps before SeaLion's first direction*/
    private static final int DIRECTION_ONE_LENGTH = 2;

    private int directionOne;

    private int count;

    /**
     * Construct a SeaLion object with the given character representation
     */
    public SeaLion(){
        super(SEALION);
        // establishes an initial direction
        directionOne = rand.nextInt(4);
        count = 0;        

    }

    /** Moves like the 'Knight' chess piece. ie. Picks a random direction, moves 2 times
     * in that direction, and then randomly chooses a direction perpendicular to the first
     * and moves in that direction once.
     * 
     * @return 0, 1, 2, or 3
     */
    public int getMove() {
        int direction;
        int directionTwo;
        // continues with direction one for the appropriate length
        if (count < DIRECTION_ONE_LENGTH) {
            count ++;
            direction = directionOne;
        }
        // if directionOne was east or west chooses a new north or south direction for last move
        else if (count < CYCLE_LENGTH && directionOne > 1) {
            directionTwo = rand.nextInt(2);
            direction = directionTwo;
            // resets count and establishes new direction in next cycle
            count = 0;
            directionOne = rand.nextInt(4);
        }
        // if directionOne was north or south chooses a new east or west direction for last move        
        else { 
            directionTwo = rand.nextInt(2) + 2;
            direction = directionTwo;            
            // resets count and establishes new direction in next cycle
            count = 0;           
            directionOne = rand.nextInt(4);
        }
        return direction;
    }
}

最佳答案

您需要的是从 0 到 3 之间的整数到 4 个命名常量的值的简单映射...而不对这些值做出任何假设。

你如何实现这一点?

提示:什么简单的 Java 数据结构从(零到长度 - 1)映射到其他内容?

关于java - 与继承相关的家庭作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6260548/

相关文章:

java - Android 从服务中打开浏览器避免多个选项卡

java - 官方文档哪里说Java的并行流操作使用fork/join?

java - 使用 Axis2 创建 Web 服务的步骤 - 客户端代码

java - 如何在 ActionListener 中关闭按钮后打开按钮

java - 在 Spinner 更改时加载不同的 strings.xml 资源

java - 大对象的空间索引

java - 编译器未调用自定义 JavaFileManager 的 getJavaFileForOutput(...) 方法

java - 统计java创建的对象个数

java - 从 URL 内容获取 java 变量的值

java - Pane 中的填充宽度