Question: Consider the provided method randomInteger() which generates a random integer from 1 to 4. As with delay() you can just use the randomInteger() method without

Consider the provided method randomInteger() which generates a random integer from 1 to 4. As with delay() you can just use the randomInteger() method without needing to understand how it works.

public int randomInteger() { java.util.Random r = new java.util.Random(); return r.nextInt(4) + 1; }

Write the public instance method randomWalk() which takes no arguments and returns no value. This method makes the snake move 100 random steps, each of which is randomly right, left, up or down. At each step randomInteger() is used to get an integer from 1 to 4 and the move is decided as follows:

  • 1 - right ()
  • 2 - left ()
  • 3 - up ()
  • 4 - down ()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!