title: Error With Moving Conditional To Class
body: <p>I am working on a HW that involves implementing some refactoring techniques and I am running into an error that I just can't seem to spot.  I have the following method that was originally in the <code>Fish</code> class that I moved into a class called <code>Strategy</code></p>
  
  <pre><code>public class Strategy
  {
      public void move (Fish fish, Pond pond)
      {
          if (fish.getHunger() &lt; 0.2)
          {
              if (fish.getSize() &lt; 7.0)
              {
                  double[] location = pond.findNearestPlant(fish.getX(), fish.getY());
                  fish.swimTowards(location[0], location[1]);
              }
              else
              {
                  double[] location = pond.findNearestSmallFish(fish.getX(), fish.getY());
                  fish.swimTowards(location[0], location[1]);
              }
          }
          else
          {
              // There are more conditionals in this block.  I haven't done anything with them yet so I didn't put them in.
          }
      }
  }
  </code></pre>
  
  <p>and I am trying to transform the conditionals in the <code>if</code> block into 2 separate classes that are also located in my <code>Strategy.java</code> file.</p>
  
  <pre><code>class SmallFish extends Strategy
  {
      public void move (Fish fish, Pond pond)
      {
          double[] location = pond.findNearestPlant(fish.getX(), fish.getY());
          fish.swimTowards(location[0], location[1]);
      }
  }
  
  class BigFish extends Strategy
  {
      public void move (Fish fish, Pond pond)
      {
          double[] location = pond.findNearestSmallFish(fish.getX(), fish.getY());
          fish.swimTowards(location[0], location[1]);
      }
  }
  </code></pre>
  
  <p>Back in the <code>Fish</code> class I added a few lines of code to determine which class to call:</p>
  
  <pre><code>import java.lang.Math;
  import java.util.Random;
  
  public class Fish
  {
      private static Random random = new Random();
      private static int numberOfFish = 0;
  
      private double hunger;
      private double size;
      private double x;
      private double y;
      private int id;
  
      // I added this line
      private Strategy strategy;
  
      private FishReport myFishReport = null;
  
      public Fish(double x, double y, FishReport report)
      {
          hunger = 0.9;
          size = 1.0;
  
          // Put it in the pond
          this.x = x;
          this.y = y;
  
          id = numberOfFish;
          numberOfFish++;
  
          // I added this code
          if (hunger &lt; 0.2 &amp;&amp; size &lt; 7.0)
              this.strategy = new SmallFish();
          else if (hunger &lt; 0.2 &amp;&amp; size &gt;= 7.0)
              this.strategy = new BigFish();
          else
              this.strategy = new Strategy();
  
          myFishReport = report;
          if(myFishReport != null)
          {
              myFishReport.updateHunger(hunger);
              myFishReport.updateSize(size);
              myFishReport.updateLocation(x, y);
          }
      }
  
      public void age(double timePassed)
      {
          double deltaSize = size * (1 + hunger * Math.exp(-size * timePassed));
          size = size + deltaSize;
  
          hunger = hunger * Math.exp(-deltaSize/size);
  
          myFishReport.updateHunger(hunger);
          myFishReport.updateSize(size);   
      }
  
      public void move(Pond pond)
      {
          strategy.move(this, pond);
      }
  
      // More methods below that I did not touch.  Left out to keep code as short as possible.
  }
  </code></pre>
  
  <p>My test suite still passed after I added the <code>SmallFish</code> class, but I had a failure after I added the <code>BigFish</code> class:</p>
  
  <blockquote>
    <p>JUnit Test Results:<br>
    testMoveStarving(FishTests): Starving fish should move expected:&lt;10.2981> but was:&lt;10.3965129880783><br>
    Total number of tests: 8<br>
    Total number of failures: 1</p>
  </blockquote>
  
  <p>Here is the test that fails:</p>
  
  <pre><code>public void testMoveStarving()
  {
      // Create a new fish and report
      FishReport report = new FishReport();
      Fish fish = new Fish(10, 20, report);
  
      // Make a pond
      Pond pond = new Pond();
  
      // Grow the fish until starving
      fish.age(20);
      fish.age(20);
      fish.age(20);
      fish.age(20);
  
      // Move the fish, and check the new location
      fish.move(pond);
  
      // Check the new location
      assertEquals("Starving fish should move", 10.2981, report.getLocation()[0], 0.001);
      assertEquals("Starving fish should move", 20.5963, report.getLocation()[1], 0.001);
  }
  </code></pre>
  
  <p>The only thing I can think of is that my conditional for the <code>BigFish</code> class in the <code>Fish</code> constructor is not accurate.  I've been staring at this for over an hour, so I think I just need another pair of eyes to point out where I am going wrong.  If there is something more extreme then just the conditional in the <code>Fish</code> constructor, please try to give hints as opposed to the answer.  Like I said, this is part of a HW assignment.</p>
  
  <p>Thank you in advanced</p>
  
  <p><strong>EDIT 1 -</strong> I forgot to add the changed <code>move()</code> method in the <code>Fish</code> class.  It's fixed now.</p>
  
  <p><strong>EDIT 2 -</strong> Failing test has been posted. The test calls the <code>age()</code> method in the <code>Fish</code> class so I added that as well.</p>