2014-11-04 65 views
1

只有在讨论了解决方案的第一部分前面的query的历史记录之后,才能够回答该查询。关于toOcean()方法的澄清查询

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

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

    } 

} 

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

作为参考,用于分配的代码完整的骨架在link

在给定的link给出,以下段落说:

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

我的问题:

PartIIaPartIIb写在RunLenghtEncoding() 5参数的构造解决方案的,我没有捕捉SharkhungerLevel财产由于方法的意见中提到的原因 -

任何鲨鱼游戏都被视为新生鲨鱼的游戏(相当于刚刚吃过的鲨鱼)。

我想知道,究竟什么toOcean()方法的意思是什么时候。我不捕获Shark runType的hungerLevel。我是否想将海洋的压缩形式转换为“现有海洋”或“新海洋”?请帮助我,我卡在这里。

注:这是2006年出版的自学课程。没有导师可用于此课程。也建议我,如果这是讨论这样的查询

+0

@Pimgd如果你可以通过这个查询,因为你已经是以前的查询中的讨论的一部分,它会更有效? – overexchange 2014-11-04 03:43:54

回答

1

它说,

任何鲨鱼运行作为新生鲨鱼(这等同于刚吃鲨鱼的运行处理正确的地方)。

所以,当你不得不重新创建Ocean,你可以用hungerLevel 0当作鲨鱼鲨鱼的任何运行在第三部分,你必须跟踪hungerLevel的,但是。但是对于第二部分C,他们暂时将这一点留给了他们。

+0

正努力想出将双重链接的游程长度编码列表转换为二维数组的逻辑。任何提示? – overexchange 2014-11-04 16:07:01

+1

@overexchange使它成为一维数组,稍后将其转换为二维数组。 – Pimgd 2014-11-04 16:19:16

+0

哦,我认为一些数学逻辑会直接将此Dlist转换为2darray。没关系。 – overexchange 2014-11-05 02:36:39