Results 1 to 2 of 2
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default helping on method..Turning a fishy

    I am not sure how to do this method. Here is my attempt and it was was not working. Need help on structure of this method.


    Java Code:
    }
    	/**
    	 * Iterate through list of Fish.  For each fish that isAlive, do the following:
    	 * <p>
    	 * 1. If this fishIsSurroundedByRocks, DO NOTHING, and move on to the next fish.
    	 *     (This fish will not turn.)
    	 * <p>
    	 * 2.  If this fish's direction is not equal to one of the codes UP, DOWN, LEFT, or
    	 *     RIGHT, then throw an IllegalFishDirectionException, passing this fish's 
    	 *     direction to the constructor.
    	 * <p>    
    	 * 3.  Check whether or not this fish is about to hit a rock if it moves in it's 
    	 *     current direction.  If it is about to hit a rock, call the fish's 
    	 *     setRandomDirection method.  Repeat this step until the fish is no longer
    	 *     about to hit a rock.  Do not make any EXTRA calls to setRandomDirection or 
    	 *     you will fail our tests!
    	 */
    	public void turnFish() {
    
    
    
    		Iterator<Fish> i = fish.iterator();
    		while(i.hasNext()) {
    			Fish curr = i.next();
    			if (curr.isAlive() == true){
    				while (fishIsSurroundedByRocks(curr) == true){
    					curr.move();
    					while ((curr.getDirection() != Fish.UP) 
    							&&
    							(curr.getDirection() != Fish.DOWN)
    							&&
    							(curr.getDirection() != Fish.RIGHT)
    							&&
    							(curr.getDirection() != Fish.LEFT)){
    
    						throw new IllegalFishDirectionException(curr.getDirection());
    
    					}
    					if (landscape[curr.getRow()][curr.getCol()] == ROCK){
    						curr.setRandomDirection();
    
    					}
    				}
    			}
    		}
    	}

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: helping on method..Turning a fishy

    Look at your code and think about what it's saying. Read it aloud in pseudocode. Line 26 & 27: "While the current fish is surrounded by rocks, move." That sounds like you've already broken rule #1.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. No one is helping
    By anithajerome in forum Suggestions & Feedback
    Replies: 3
    Last Post: 12-06-2010, 12:52 PM
  2. Turning Recursion Method into Iterative method
    By mattakuevan in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 06:46 AM

Tags for this Thread

Posting Permissions

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