Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-06-2008, 10:24 PM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
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 !!


Quote:
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 10:27 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-06-2008, 11:00 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-07-2008, 12:02 AM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-07-2008, 12:30 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
array problem oceansdepth New To Java 3 04-05-2008 04:25 AM
assignment problem help needed tiggz1980 New To Java 2 02-07-2008 01:14 AM
array problem wats New To Java 1 12-12-2007 09:08 AM
Problem with array Copy coco New To Java 1 08-07-2007 09:46 AM
array problem Albert Advanced Java 2 07-01-2007 03:13 AM


All times are GMT +3. The time now is 04:45 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org