1

Hello i am writing a program that creates a method that can remove items from an arraylist and add them to another ArrayList (under certain conditions). This is the method I am supposed to create: 

A method called giveAwayFish() which represents a person returning his fish to the pond and/or giving them away to another fisher.

It will go through all of this person's fish ( the one giving the fish away) and see if the other fisher ( the one who will be receiving the fish) is willing to keep any. If the other fisher wants any, they are to be given to that fisher. If the fisher is unwilling to keep the fish, then these fish must be returned to the pond.

I tried writing out this method about a hundred times and I can not for the life of me figure out what to do. I was able to remove all the fish from the persons array but I do not know how to add them back. This is what I need help with.

Here is my code if it helps:

import java.util.*;

public class Fisher   
{
  private String name;
  private Fish [] fishCaught;
  private int numFishCaught; 
  private int keepSize;
  public static int LIMIT = 10;

  public String getName() 
  {
    return this.name;
  }

  public int getNumFishCaught()
  {
    return this.numFishCaught;
  }

  public int getKeepSize()
  {
    return this.keepSize;
  }

  public Fisher(String n, int k)
  {
    name = n;
    keepSize = k;
  }

  public String toString()
  {
    return(this.name + " with " + this.numFishCaught + " fish as follows:");
  }
  private ArrayList<Fish> fishesCaught = new ArrayList<Fish>();


  public void keep(Fish fish) 
  {
    if(this.numFishCaught < LIMIT)
    {
      fishesCaught.add(fish);
      numFishCaught++;
    }
  }

  public boolean likes(Fish fish)
  {
    if(fish.size >= this.keepSize && fish.species != "Sunfish")
    {
      return true;
    }

    else 
    {
      return false;
    }
  }

  public void listFish() 
  {
    System.out.println(this.toString());

    for(Fish fish : fishesCaught)
    {
      System.out.println(fish.toString());
    }
  }

  public void goFishingIn(Pond pond)
  {
    Fish fish = pond.catchAFish();

    if(likes(fish))
    {
      this.keep(fish);
    }
    else
    {
      pond.add(fish);
    }
  }

  public void giveAwayFish(Fisher fisher, Pond pond)
  {
    Fish fish = fishesCaught;
     if(fisher.likes(fish))
     {
      fishesCaught.clear();
      this.numFishCaught = 0;
     }

  }


} 
5
  • This fish.species != "Sunfish" is not a good start Commented Feb 20, 2014 at 4:00
  • Why you have Fish[] fishCaught and ArrayList<Fish> fishesCaught? They seems duplicated (and fishCaught is not used). I think you better clean up your code a bit Commented Feb 20, 2014 at 4:08
  • Fish fish = fishesCaught; in giveAwayFish should give you a compilation error. Commented Feb 20, 2014 at 4:12
  • why is it not a good idea @MadProgrammer ?it worked when i tested it Commented Feb 20, 2014 at 4:25
  • You are comparing two object references, not the contents of the Strings. String comparison in Java is done through the equals or equalsIgnoreCase methods, which compares the contents of the two Strings for equality, which is what you are trying to do... Commented Feb 20, 2014 at 4:28

1 Answer 1

1

Biggest problem here is (yes, there are lots of other problems), in your giveAwayFish(), you wrote

Fish fish = fishesCaught;

However fishesCaught is a List<Fish>. That can't even compile.

I believe what you want to do is something like (in psuedo code):

for (Fish fish : fishesCaught) {
   if (fisher.like(fish)) {
       fisher.keep(fish);
   } else {
       pond.addFish(fish);
   }
}
fishesCaught.clear();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.