简体   繁体   中英

Java: cannot find symbol

in this class I'm making a catchFish method, it basically returns a random fish from the pond, the fish should be removed from the pond and returned from the method, If there are no fish then null will be returned

this is my code

import java.util.Random;

public class Pond {

  private int     MAX_FISH = 10;
  private Fish[]  fish = new Fish[MAX_FISH];
  private int     numFish;


  public Pond (int numFish, Fish[] fish) {
    this.numFish = numFish;
    this.fish = fish;
  }


  public int getNumFish() {
    return numFish;
  }


  boolean isFull(){
    if (numFish < MAX_FISH) {
      return false;
    } else {
      return true;
    }
  }

  public String toString(){
    return "Pond with " + numFish + " fish";
  }

  public void listFish() {
    System.out.println("Pond with " + numFish + " as follows:");
    for (int i = 0; i < numFish ; i++) {
      Fish f = fish[i];
      System.out.println("A " + f.getSize() + " cm " + f.getSpecies());
    }
  }

  public void add(Fish f) {
    if (isFull()) {
      System.out.println("Sorry, the pond is full!");
    } else {
      numFish++;
      fish[numFish-1] = f;
    }
  }

  public Fish catchAFish() {
    if (numFish == 0) {
      System.out.println("Sorry, the pond is empty!");
      return null;
    } else {
      Fish f = new Fish();
      int r = (int)Math.random()*(numFish-1);
      f = fish[r];
      if (r == (numFish -1)) {
        fish[r] = null;
      } else {
        fish[r] = fish[numFish-1];
      }
      numFish--;
      return f;
    }
  }
}

and in catchAFish method the line

Fish f = new Fish(); gives an error:

java:55: cannot find symbol

symbol : constructor Fish()

location: class Fish

and I don't understand what I'm doing wrong?

EDIT:

fish class

public class Fish {

  private String      species;
  private int         size;


  public Fish(int size, String species) {
    this.size = size;
    this.species = species;
  }


   public String toString() {
          return " A " + size + " cm " + species;
    }

  public String getSpecies() {
    return species;
  }

  public int getSize() {
    return size;
  }


}

Basically... Fish must have a non-empty constructor, requiring you to provide one or parameters when you create an instance of Fish .

Looking at the Fish code, the only constructor you provide is...

 public Fish(int size, String species) {

There is no "default" constructor (which would allow you to use new Fish() ).

But I'm not convinced that you actually need to create a new instance anyway, as you re-assign it almost immediately.

 Fish f = new Fish();
 int r = (int)Math.random()*(numFish-1);
 // Overridden...
 f = fish[r];

Instead, you could simply use...

 int r = (int)Math.random()*(numFish-1);
 Fish f = fish[r];

Java will create a default constructor, with no arguments, if you don't supply a constructor . So, if you cannot call a no-argument constructor, then you must have created a constructor that does take at least one argument.

Either supply the necessary arguments to call your existing Fish constructor, or create an explicit no-argument Fish constructor.

Since you haven't posted your Fish class I'm going to assume that it's the same as it was in your previous post.

You've defined a constructor for Fish with two parameters. Because you've supplied your own constructor Java will not supply the default no-args constructor. Either define a no-args constructor of your own or use your existing one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM