Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First comment, this isn't really the correct use of an <code>Exception</code>. You should return a status code, maybe an <code>enum</code>.</p> <p>Second comment, <strong>never</strong> <code>throw new Exception()</code>. Create your own <code>Exception</code> class, otherwise you deal with <strong>all</strong> exceptions that could be thrown (<code>NullPointerException</code>, <code>ArrayIndexOutOfBoundsException</code> etc) in exactly the samne way. This is a bug waiting to happen.</p> <p>Now onto your problem. You <em>do</em> remove the item at index <code>i</code> from your <code>List</code>, but you <strong>always</strong> add it back in again with your call to <code>set</code>.</p> <p>Never remove items from a <code>Collection</code> while looping. In fact never loop over a collection by index unless you have to.</p> <p>Use an <code>Iterator</code> which has a <code>remove</code> method. This is the only (almost only) safe way to remove from a collection while looping.</p> <pre><code>final Iterator&lt;Bee&gt; myIter = cells.iterator(); while(myIter.hasNext()) { final Bee bee = myIter.next(); try{ bee.anotherDay(); }catch(Exception e){ myIter.remove(); } } </code></pre> <p>There is no need for you another day method to return <code>this</code> and there is no need to replace the reference in your <code>List</code> with exactly the same reference.</p> <p>Looking at your <code>eat</code> method, there is quite a bit of tidying that can happen; I would recommend this:</p> <pre><code>public boolean eat() throws Exception{ if(hive.honey &gt;= 2){ hive.takeHoney(2); if(health &lt; 3){ health++; } return true; } health -= 1; if(health == 0){ throw new BeeOutOfHealthException(); } return false; } </code></pre> <p>I.e. many of your assignments are rather confused. You don't need the <code>else</code> if you <code>return</code> in the <code>if</code>. Your nested <code>if</code> is a little odd - having an empty statement is certainly code smell.</p> <p>Further the logic of taking honey from the <code>hive</code> should really be <em>in the hive</em> for <code>takeHoney</code> should probably return a <code>boolean</code> if that amount of honey cannot be taken. That then reduces your method to:</p> <pre><code>public boolean eat() throws Exception{ if(hive.takeHoney(2)){ if(health &lt; 3){ health++; } return true; } health -= 1; if(health == 0){ throw new BeeOutOfHealthException(); } return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload