繁体   English   中英

查询有关toOcean()方法的澄清

[英]Query on clarification about toOcean() method

只有在讨论解决方案的第一部分的先前查询的历史记录之后,才能回答此查询。

以下是该解决方案,我写了第IIAPartIIb,我写PartIIc即之前需要澄清toOcean()方法。

/* RunLengthEncoding.java */

package Project1;

/**
 *  The RunLengthEncoding class defines an object that run-length encodes an
 *  Ocean object.  Descriptions of the methods you must implement appear below.
 *  They include constructors of the form
 *
 *      public RunLengthEncoding(int i, int j, int starveTime);
 *      public RunLengthEncoding(int i, int j, int starveTime,
 *                               int[] runTypes, int[] runLengths) {
 *      public RunLengthEncoding(Ocean ocean) {
 *
 *  that create a run-length encoding of an Ocean having width i and height j,
 *  in which sharks starve after starveTime timesteps.
 *
 *  The first constructor creates a run-length encoding of an Ocean in which
 *  every cell is empty.  The second constructor creates a run-length encoding
 *  for which the runs are provided as parameters.  The third constructor
 *  converts an Ocean object into a run-length encoding of that object.
 *
 *  See the README file accompanying this project for additional details.
 */

class RunLengthEncoding {

  /**
   *  Define any variables associated with a RunLengthEncoding object here.
   *  These variables MUST be private.
   */

  private DList2 list;
  private int sizeOfRun;
  private int width;
  private int height;
  private int starveTime;
  /**
   *  The following methods are required for Part II.
   */

  /**
   *  RunLengthEncoding() (with three parameters) is a constructor that creates
   *  a run-length encoding of an empty ocean having width i and height j,
   *  in which sharks starve after starveTime timesteps.
   *  @param i is the width of the ocean.
   *  @param j is the height of the ocean.
   *  @param starveTime is the number of timesteps sharks survive without food.
   */

  public RunLengthEncoding(int i, int j, int starveTime) {
      this.list = new DList2();
      this.list.insertFront(TypeAndSize.Species.EMPTY, i*j);
      this.sizeOfRun = 1;
      this.width = i;
      this.height = j;
      this.starveTime = starveTime;
  }

  /**
   *  RunLengthEncoding() (with five parameters) is a constructor that creates
   *  a run-length encoding of an ocean having width i and height j, in which
   *  sharks starve after starveTime timesteps.  The runs of the run-length
   *  encoding are taken from two input arrays.  Run i has length runLengths[i]
   *  and species runTypes[i].
   *  @param i is the width of the ocean.
   *  @param j is the height of the ocean.
   *  @param starveTime is the number of timesteps sharks survive without food.
   *  @param runTypes is an array that represents the species represented by
   *         each run.  Each element of runTypes is Ocean.EMPTY, Ocean.FISH,
   *         or Ocean.SHARK.  Any run of sharks is treated as a run of newborn
   *         sharks (which are equivalent to sharks that have just eaten).
   *  @param runLengths is an array that represents the length of each run.
   *         The sum of all elements of the runLengths array should be i * j.
   */

  public RunLengthEncoding(int i, int j, int starveTime,
          TypeAndSize.Species[] runTypes, int[] runLengths) {
    this.list = new DList2();
    this.sizeOfRun = 0;
    this.width = i;
    this.height = j;
    this.starveTime = starveTime;
    if(runTypes.length != runLengths.length){
        System.out.println("lengths are unequal");
    }else{
        for(int index=0; index < runTypes.length; index++){
            this.list.insertFront(runTypes[index], runLengths[index]);
            this.sizeOfRun++;
        }
    }
  }

  /**
   *  restartRuns() and nextRun() are two methods that work together to return
   *  all the runs in the run-length encoding, one by one.  Each time
   *  nextRun() is invoked, it returns a different run (represented as a
   *  TypeAndSize object), until every run has been returned.  The first time
   *  nextRun() is invoked, it returns the first run in the encoding, which
   *  contains cell (0, 0).  After every run has been returned, nextRun()
   *  returns null, which lets the calling program know that there are no more
   *  runs in the encoding.
   *
   *  The restartRuns() method resets the enumeration, so that nextRun() will
   *  once again enumerate all the runs as if nextRun() were being invoked for
   *  the first time.
   *
   *  (Note:  Don't worry about what might happen if nextRun() is interleaved
   *  with addFish() or addShark(); it won't happen.)
   */

  /**
   *  restartRuns() resets the enumeration as described above, so that
   *  nextRun() will enumerate all the runs from the beginning.
   */

  public void restartRuns() {
    this.sizeOfRun = 0;
  }

  /**
   *  nextRun() returns the next run in the enumeration, as described above.
   *  If the runs have been exhausted, it returns null.  The return value is
   *  a TypeAndSize object, which is nothing more than a way to return two
   *  integers at once.
   *  @return the next run in the enumeration, represented by a TypeAndSize
   *          object.
   */

  public TypeAndSize nextRun() {
      TypeAndSize obj = null;
      if(this.sizeOfRun > 0){
          obj = this.list.nTh(this.sizeOfRun);
          this.sizeOfRun--;
      }
      return obj;
  }
}

==========

/* DList2.java */

package Project1;

/**
 *  A DList2 is a mutable doubly-linked list.  Its implementation is
 *  circularly-linked and employs a sentinel (dummy) node at the sentinel
 *  of the list.
 */

class DList2 {

  /**
   *  sentinel references the sentinel node.
   *
   *  DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
   */

  protected DListNode2 sentinel;
  protected long size;

  /* DList2 invariants:
   *  1)  sentinel != null.
   *  2)  For any DListNode2 x in a DList2, x.next != null.
   *  3)  For any DListNode2 x in a DList2, x.prev != null.
   *  4)  For any DListNode2 x in a DList2, if x.next == y, then y.prev == x.
   *  5)  For any DListNode2 x in a DList2, if x.prev == y, then y.next == x.
   *  6)  size is the number of DListNode2s, NOT COUNTING the sentinel
   *      (denoted by "sentinel"), that can be accessed from the sentinel by
   *      a sequence of "next" references.
   */

  /**
   *  DList2() constructor for an empty DList2.
   */
  public DList2() {
    this.sentinel = new DListNode2();
    this.sentinel.next = this.sentinel;
    this.sentinel.prev = this.sentinel;
    this.size = 0;
  }



  /**
   *  insertFront() inserts an object of type TypeAndSizeAndHungerAndStarveTime at the front of a DList2.
   */
  void insertFront(TypeAndSize.Species runType, int runLength) {
    DListNode2 newNode = new DListNode2(runType, runLength);
    newNode.next = this.sentinel.next;
    this.sentinel.next.prev = newNode;
    this.sentinel.next = newNode;
    this.sentinel.next.prev = this.sentinel;
    this.size++;
  }

  /**
   * nTh() returns the nTh node
   * @param nTh
   * @return
   */
  TypeAndSize nTh(int nTh){
    DListNode2 node = this.sentinel.prev;
    int index = 1; 
    while(index < nTh ){
        node = node.prev;
    }
    return node.runObject;
  }
}

============================

/* DListNode2.java */

package Project1;
/**
 *  A DListNode2 is a node in a DList2 (doubly-linked list).
 */

class DListNode2 {

  /**
   *  item references the item stored in the current node.
   *  prev references the previous node in the DList.
   *  next references the next node in the DList.
   *
   *  DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
   */

  TypeAndSize runObject;
  DListNode2 prev;
  DListNode2 next;

  /**
   *  DListNode2() constructor.
   */
  DListNode2() {
    this.runObject = null;
    this.prev = null;
    this.next = null;
  }

  DListNode2(TypeAndSize.Species runType, int runLength) {
    this.runObject = new TypeAndSize(runType, runLength);
    this.prev = null;
    this.next = null;
  }

}

===================================

/* TypeAndSize.java */

/* DO NOT CHANGE THIS FILE. */
/* YOUR SUBMISSION MUST WORK CORRECTLY WITH _OUR_ COPY OF THIS FILE. */

package Project1;

/**
 *  Each TypeAndSize object represents a sequence of identical sharks, fish,
 *  or empty cells.  TypeAndSizes are your way of telling the test program
 *  what runs appear in your run-length encoding.  TypeAndSizes exist solely
 *  so that your program can return two integers at once:  one representing
 *  the type (species) of a run, and the other representing the size of a run.
 *
 *  TypeAndSize objects are not appropriate for representing your run-length
 *  encoding, because they do not represent the degree of hunger of a run of
 *  sharks.
 *
 *  @author Jonathan Shewchuk
 */

class TypeAndSize {

  Species type;               // runType EMPTY, SHARK, or FISH
  int size;                   // Number of cells in the run for that runType.

  enum Species{EMPTY,SHARK,FISH}

  /**
   *  Constructor for a TypeAndSize of specified species and run length.
   *  @param species is Ocean.EMPTY, Ocean.SHARK, or Ocean.FISH.
   *  @param runLength is the number of identical cells in this run.
   *  @return the newly constructed Critter.
   */

  TypeAndSize(Species species, int runLength) {
    if (species == null)    {   
      System.out.println("TypeAndSize Error:  Illegal species.");
      System.exit(1);
    }
    if (runLength < 1) {
      System.out.println("TypeAndSize Error:  runLength must be at least 1.");
      System.exit(1);
    }
    this.type = species;
    this.size = runLength;

  }

}

======================================

供参考, 链接中提供了完整的代码分配框架

在给定的链接中 ,下一段说:

第二部分(c):在RunLengthEncoding类中实现toOcean()方法,该方法将游程长度编码转换为Ocean对象。 为此,您将需要在Ocean类中实现一个新的addShark()方法,以便您可以指定添加到海洋中的每个鲨鱼的饥饿感。 这样,您可以将Ocean转换为行程编码,然后再次返回,而不会忘记每个鲨鱼的饥饿程度。

我的问题:

在用RunLenghtEncoding() 5参数构造函数编写的解决方案的PartIIaPartIIb中,由于方法注释中提到的原因,我没有捕获Shark hungerLevel属性-

任何种类的鲨鱼都被视为一系列新生的鲨鱼(相当于刚吃掉的鲨鱼)。

我想知道,什么时候toOcean()方法确切意味着什么。 我不捕获hungerLevelShark runType。 我是否应该将海洋的压缩形式转换为“现有海洋”或“新海洋”? 请帮助我,被困在这里。

注意:这是2006年出版的自学课程。此课程没有导师。 还建议我,如果这是讨论此类查询的正确地方

它说

任何种类的鲨鱼都被视为一系列新生的鲨鱼(相当于刚吃掉的鲨鱼)。

所以,当你不得不重新创建的Ocean ,你可以把鲨鱼作为鲨鱼的任何运行hungerLevel 0。在第三部分必须跟踪的hungerLevel ,但是。 但是对于第二部分c,他们暂时不在此讨论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM