0

How do you call a method with parameters inside another method?

For example, I am trying to call my likes() method inside my goFishingIn() method but it's not compiling. Here's my code:

import java.util.*;

public class Fisher   
{
  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 goFishingIn(Pond pond)
  {
    pond.catchAFish();
    this.likes(Fish fish);
  }
}
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
user3330114
  • 77
  • 1
  • 1
  • 8
  • How do you do invoke your `goFishinIn` method? How do you use `System.out.println(..)`? – Sotirios Delimanolis Feb 19 '14 at 21:58
  • You need a `Fish fish` instance to pass as parameter. Probably `pond.catchAFish` returns a `Fish`, then you can pass this object reference as parameter. – Luiggi Mendoza Feb 19 '14 at 21:58
  • Make an object of Fish first then pass it likes inside goFishingIn(Pond pond) – Devavrata Feb 19 '14 at 22:02
  • You have a problem with `fish.species != "Sunfish"` should be instead `! fish.species.equals("Sunfish")` See more: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Kevin Panko Feb 19 '14 at 22:20

4 Answers4

1

if you already have a fish somewhere, you'd call it like so

this.likes(fish);

If you don't, maybe you have to make one

Fish fish = new Fish();
this.likes(fish);

or pass one in, or do whatever to get one in scope

public void goFishingIn(Pond pond, Fish fish)
{
  pond.catchAFish();
  this.likes(fish);
}
1

From what it looks like, you should be able to change your goFishingIn() method like so:

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

Basically, you just need to pass an instance of fish to the likes() method, so you need to instantiate the fish prior to calling the likes() method.

Or, you could even shorten it up and call the catchAFish() method from inside the parameter, if you do not need to do anything with the fish afterwards.

public void goFishingIn(Pond pond)
{
  this.likes(pond.catchAFish());
}

The first method would be preferable though, as it will allow you to make any future references to the fish object.

dub stylee
  • 3,252
  • 5
  • 38
  • 59
  • Glad to hear it. If you are satisfied with the answer, go ahead and like/accept it so that we know you have solved your question. – dub stylee Feb 19 '14 at 22:09
0

If you are declaring a method, you state parameters with classes, such as

public void goFishingIn(Pond pond)

but when you invoke a method, you only pass a variable, there is no Class identifier.

this.likes(Fish fish);

should be

this.likes(fish);

However fish variable is not defined anywhere inside goFishingIn() method body, therefore you will get an error that it's undefined. You need to obtain the fish value somewhere, or define it.

It seems to me that most reasonable in this case would be to make pond.catchAFish() return type Fish and then store the return value in fish variable.

Fish fish = pond.catchAFish();

Then you will be able to use it.

Warlord
  • 2,798
  • 16
  • 21
0

You should change

   this.likes(Fish fish);

into

this.likes(fish);

When you pass arguments to a method , you dont specify the type. You only specify the type in the method signature itself. Nog when you call this method.

Bgvv1983
  • 1,256
  • 1
  • 13
  • 27