2011-06-07 83 views
1

问候。除了一项要求外,我已经完成了一所学校的项目。与遗产有关的家庭作业

的要求是:

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

我看不到这个值。分配的常量值是逻辑上的,并且可以方便地使用随机返回方向。如果分配的值是四个非连续的数字,这会使代码真的变得复杂。

下面是在作业中提到的原始SeaCreature类,然后是我的一个使用Random的生物的代码。除了在每个类别中涉及随机移动的NORTH,SOUTH,EAST,WEST分配新值之外,是否有更清晰的方法来返回基本方向而不是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(); 
} 

我的海狮类:

/** 
* 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

我不明白这与继承有什么关系,除了'extends'关键字的存在。 – 2011-06-07 04:22:47

+1

SeaLion延伸SeaCreature。这是继承。 – Nik 2011-06-07 04:23:40

+1

我看不到你在代码中使用'const'的地方。是的,你使用的是int值,但不是const。我*认为*这就是要求所要求的。在这种特殊情况下没有太多价值,因为这是一个简单的概念。但是你的下一个项目可能是那些'常量不那么简单 - 也许他们是字符串 - 你怎么做到随机1-4?在这种情况下,你应该很容易编写一个GetRandomDirection()方法,该方法返回一个INT,并且可以从const中随机选取? – Prescott 2011-06-07 04:35:15

回答

0

我想我会做这样的事情:

private static final int AXES[][] = { 
    {NORTH,SOUTH}, 
    {EAST,WEST} 
}; 

...

private int directionOne; 
private int directionTwo; 
private int count; 

...

public SeaLion(){ 
    super(SEALION); 
    // establishes an initial direction 
    nextCycle(); 
} 

...

public int getMove() { 
    int direction; 
    if (count < DIRECTION_ONE_LENGTH) { 
     direction = directionOne; 
    } else { 
     direction = directionTwo; 
    } 
    ++count; 
    if (count == CYCLE_LENGTH) { 
     nextCycle(); 
    } 
    return direction; 
} 

...

private void nextCycle() { 
    int axis1 = random.nextInt(2); 
    int axis2 = 1 - axis1; 
    directionOne = AXES[axis1][random.nextInt(2)]; 
    directionTwo = AXES[axis2][random.nextInt(2)]; 
    count = 0;   
} 
0

您可以让NORTHSOUTHWEST,并且EAST是任一方向或标题,如下:

标题:

public static final int NORTH = 0; 
public static final int SOUTH = 180; 
public static final int EAST = 90; 
public static final int WEST = 270; // Or -90 

方位:

public static final int NORTH = 90; 
public static final int SOUTH = 270; // Or -90 
public static final int EAST = 0; 
public static final int WEST = 180; 
1

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

你怎么能实现呢?提示:什么简单的Java数据结构从(从零到长度 - 1)映射到其他东西?