Results 1 to 4 of 4
  1. #1
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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;
        }
    }

  3. #3
    SCS17 is offline Member
    Join Date
    Nov 2007
    Posts
    20
    Rep Power
    0

    Default

    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.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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

  1. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 02:25 AM
  2. assignment problem help needed
    By tiggz1980 in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 11:14 PM
  3. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:08 AM
  4. Problem with array Copy
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:46 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •