Results 1 to 4 of 4
- 03-06-2008, 08:24 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
Array problem.. help needed please!
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 !!!
}
}Last edited by SCS17; 03-06-2008 at 08:27 PM.
- 03-06-2008, 09:00 PM #2
Java Code:public class LakeRx { 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() { return capacity > numFish; } /** * 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()) 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() { for(int j = 0; j < fish.length; j++) { if(fish[j].isHungry) return fish[j]; } return null; } } class Fish { int location; boolean isHungry; public Fish(int location, boolean isHungry) { this.location = location; this.isHungry = isHungry; } }
- 03-06-2008, 10:02 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 20
- Rep Power
- 0
hey HardWired..
thanks for the help! but I dont think that I was clear when I described the assignment.. you should return the first hungry fish and thats it! In the for loop that you made its gonna keep looping and returning every hungry fish found.. thats why I mentioned that I tried doing a for loop and then breaking it and it didnt work out!. Plus.. how can you change the array ?? The code that you just did doesnt change the array.. and thats why I said that I thought of doing a new array but it didnt work.
Another thing.. I didnt get your last piece of code about the public Fish.. what is this code supposed to do ??
Thanks alot for the help.
- 03-06-2008, 10:30 PM #4
In the for loop that you made its gonna keep looping and returning every hungry fish found..
When the first hungry fish is found the method returns a reference to the fish, ie, program execution exits/leaves the for loop and returns fish[j] to the caller.
I didnt get your last piece of code about the public Fish.. what is this code supposed to do
You didn't post a Fish class so I made one up.
Similar Threads
-
array problem
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-05-2008, 02:25 AM -
assignment problem help needed
By tiggz1980 in forum New To JavaReplies: 2Last Post: 02-06-2008, 11:14 PM -
array problem
By wats in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:08 AM -
Problem with array Copy
By coco in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:46 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks