hello there,
I have a problem regarding arrays in an assignment that I'm doing.. I'm not asking anybody to write me the whole assignment or something!! No.. I just need some help in a particulat part!.
This class represents a lake of fish..
It has a an array of Fish (Fish fish[]) and some other instance variables.
Theres a method called isFull().. which checks whether or not the lake has reached its capacity.. the methods works fine!.
If you look in theere you'll find a method called getHungryFish().. this method
is supposed to go through the Fish array and find the first hungry fish.. only the first!! and then returns it.. and replace the location of the hungry fish with (null) and thats it.. youre not supposed to find the other hungry fish.. just the first one!!!! If there are no hungry fish in the array then null should be returned from the method.!
And heres the problem.. I know what I'm being asked but I just cant write it!!!
Should I write an if statement inside the for loop and then break the loop !! I tried that several times and it didnt work!! Should I make a new array and copy the old one into the new one!! ??? I tried that too but I dont seem to get the syntax right!
I removed the constructors from the code below.. just to make it shorter!
Any help would be appreciated if you are willing to help.
Thank you very much !!
public class Lake {
private String name; // name of the lake
private Fish fish[]; // an array for the fish objects
private int capacity; // the capacity of the lake
private int numFish; // the number of fishes in there
// These are the get methods
public String getName() {return name;}
public int getNumFish() { return numFish;}
public int getCapacity() {return capacity;}
// This is the isFull method.
public boolean isFull() {
if (capacity>numFish)
return true;
else
return false; }
/* This is the add method.. it checks the isFull methods. If it returns true it adds the fish.. if false it does nothing. Thats why I put */
public void add(Fish aFish) {
if (isFull()==true)
fish[numFish++] = aFish;
else
System.out.println("Sorry you cant add more fish because the lake is full.");
}
// This is the getHungry method
public Fish getHungryFish() {
// HERE IS THE PROBLEM !!!
}
}