Java Question (short)

Discussion in 'OT Technology' started by Spur, Dec 10, 2004.

  1. Spur

    Spur Boomer

    Joined:
    Nov 14, 2001
    Messages:
    3,685
    Likes Received:
    5
    Location:
    AZ
    as part of my project (which is due in two hours) I need to find a way for a method to return repeat results every time it comes back to the method.


    Code:
    // 12/10/04
    // TA: J. Carlson
    //
    //
    // This is the Frog critter. The Frog moves random like
    // the bird, but stays in single direction longer.
    
    import java.math.*;
    
    public class Frog implements Critter {
    
    
    	// This defines how the frog looks.
    	public char getChar() {
    		return 'F';
    	}
    
    
    	// This creates the Frog's movements.
    	public int getMove() {
    		int whichWay = 1;
    
    		double randomNum = Math.random();
    
    			if (randomNum <= 0.24)
    				whichWay = NORTH;
    			else if (randomNum > 0.24 && randomNum < 0.5)
    				whichWay = SOUTH;
    			else if (randomNum > 0.49 && randomNum < 0.75)
    				whichWay = EAST;
    			else if (randomNum > 0.74)
    				whichWay = WEST;
    
    		return whichWay;
    
    	}
    }
    

    For this class, I need to make getMove() repeat each direction 3 times.


    I'm stuck!
     
  2. Scn64

    Scn64 Guest

    So you have to use the return to triple the result? You can't just assign, for example, "North North North" to a string and return that?
     
  3. Spur

    Spur Boomer

    Joined:
    Nov 14, 2001
    Messages:
    3,685
    Likes Received:
    5
    Location:
    AZ

    explain
     
  4. Scn64

    Scn64 Guest

    I'm asking if you could just do something like this:

    Code:
                                  if (randomNum <= 0.24)
    				whichWay = "NORTH NORTH NORTH";
    			else if (randomNum > 0.24 && randomNum < 0.5)
    				whichWay = "SOUTH SOUTH SOUTH";
    			else if (randomNum > 0.49 && randomNum < 0.75)
    				whichWay = "EAST EAST EAST";
    			else if (randomNum > 0.74)
    				whichWay = "WEST WEST WEST";
    
    whichWay would have to be a string though, not an int.
     
  5. Spur

    Spur Boomer

    Joined:
    Nov 14, 2001
    Messages:
    3,685
    Likes Received:
    5
    Location:
    AZ

    would that work even if my method is "public int getMove()"?
     
  6. Scn64

    Scn64 Guest

    No, you'd have to change it to "public string getMove()".
     
  7. Spur

    Spur Boomer

    Joined:
    Nov 14, 2001
    Messages:
    3,685
    Likes Received:
    5
    Location:
    AZ

    can't. I'm implementing Critter.java to the class. Inside Critter.java is public int getMove(); I didn't think i could change it.
     
  8. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    I'm not really understanding what you mean by repeat the direction three times. What exactly does the method int getMove() do?
     
  9. Spur

    Spur Boomer

    Joined:
    Nov 14, 2001
    Messages:
    3,685
    Likes Received:
    5
    Location:
    AZ
    See the professor pretty much wrote all the code.


    Except we have to create the 5 classes of critters (bird, turtle, wolf, mouse, frog)

    Within these classes we are implementing class Critter, which only has two methods. One that creates the "animal" look, and one that makes it move (getMove()).
     
  10. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    you could call the method three times, i dont think theres a way to have something return something more than once. once something is returned the method is exited
     

Share This Page