U is upward,  D is downward, L is left and R is right : Using the methods pickDir() and exractInt() as shown below, to write a simple automated game: how long does it take a robot to travel from a left line of a field to the right line? Assume field is 15 units apart (meters, kilometers, it doesn’t matter), and the robot starts on the left line. pickDir(): // return string with random direction and random distance (within maxDist) public static String pickDir (int maxDist) { Random r = new Random();   // random number generator // return direction + distance return ( "" + "LRUD".charAt( r.nextInt(4) ) + (r.nextInt(maxDist) + 1) ); } extractInt(): // returns integer from string,                                                                     from pos p1 to p2: str = "D1" – extractInt(str,1,1); public static int extractInt (String str, int p1, int p2) { return ( Integer.parseInt( str.substring(p1, p2+1) ) ); } Write the main() method that keeps looping until the robot’s random left and right movements have the robot move past the right line, using the following rules: • starting travel distance (Trav) is zero (0)—the robot is starting on the left line • maximum distance, when picking a random direction, is 9 units • robot moves to the left (L) are negative integer distances (-) to the travel distance, and robot moves to the right (R) are positive integer distances (+) to the travel distance • robot moves up (U) and down (D) do not affect the left-right travel distance—but they are still displayed • keep looping while the robot’s travel distance <= 15 units (i.e., stop when travel distance > 15 units) - after the loop, display “robot has crossed the right line!” • in each loop iteration, the robot picks a new random direction (Rand), by calling pickDir() -  hint: .charAt(), and method extractInt() (see previous question) for direction & distance • since the robot is travelling randomly, there is a possibility it may never cross the right line (or take a very, very, very long to cross), and it will run out of power, - add another condition to the loop, stopping if the number of iterations exceeds MAXLOOPS (set MAXLOOPS to 20, 50, or 100, depending on how much power the robot’s battery contains) - if the exceeding MAXLOOPS, display “battery power depleted, robot has stopped.” - In each iteration, display the current travel distance and current random direction.  example run: Robot Travelling Trav: 0 Rand: L1 Trav: -1 Rand: R3 Trav: 2 Rand: L1 Trav: 1 Rand: D4 Trav: 1 Rand: R8 Trav: 9 Rand: U3 Trav: 9 Rand: L3 Trav: 6 Rand: U1 Trav: 6 Rand: R6 Trav: 12 Rand: D1 Trav: 12 Rand: L2 Trav: 10 Rand: R8 Trav: 18 Done—robot has crossed the right line!

Operations Research : Applications and Algorithms
4th Edition
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Wayne L. Winston
Chapter17: Markov Chains
Section: Chapter Questions
Problem 12RP
icon
Related questions
Question

U is upward,  D is downward, L is left and R is right : Using the methods pickDir() and exractInt() as shown below, to write a simple automated game: how long does it take a robot to travel from a left line of a field to the right line?

Assume field is 15 units apart (meters, kilometers, it doesn’t matter), and the robot starts on the left line.

pickDir():

// return string with random direction and random distance (within maxDist)

public static String pickDir (int maxDist)

{

Random r = new Random();   // random number generator

// return direction + distance

return ( "" + "LRUD".charAt( r.nextInt(4) ) + (r.nextInt(maxDist) + 1) );

}

extractInt():

// returns integer from string,                                                                     from pos p1 to p2: str = "D1" – extractInt(str,1,1);

public static int extractInt (String str, int p1, int p2)

{

return ( Integer.parseInt( str.substring(p1, p2+1) ) );

}

Write the main() method that keeps looping until the robot’s random left and right movements have the robot move past the right line, using the following rules:

• starting travel distance (Trav) is zero (0)—the robot is starting on the left line

• maximum distance, when picking a random direction, is 9 units

• robot moves to the left (L) are negative integer distances (-) to the travel distance, and robot moves to the right (R) are positive integer distances (+) to the travel distance

• robot moves up (U) and down (D) do not affect the left-right travel distance—but they are still displayed

• keep looping while the robot’s travel distance <= 15 units (i.e., stop when travel distance > 15 units)

- after the loop, display “robot has crossed the right line!”

• in each loop iteration, the robot picks a new random direction (Rand), by calling pickDir()

hint: .charAt(), and method extractInt() (see previous question) for direction & distance

• since the robot is travelling randomly, there is a possibility it may never cross the right line (or take a very, very, very long to cross), and it will run out of power,

- add another condition to the loop, stopping if the number of iterations exceeds MAXLOOPS (set MAXLOOPS to 20, 50, or 100, depending on how much power the robot’s battery contains)

- if the exceeding MAXLOOPS, display “battery power depleted, robot has stopped.”

- In each iteration, display the current travel distance and current random direction.

 example run:

Robot Travelling

Trav: 0

Rand: L1

Trav: -1

Rand: R3

Trav: 2

Rand: L1

Trav: 1

Rand: D4

Trav: 1

Rand: R8

Trav: 9

Rand: U3

Trav: 9

Rand: L3

Trav: 6

Rand: U1

Trav: 6

Rand: R6

Trav: 12

Rand: D1

Trav: 12

Rand: L2

Trav: 10

Rand: R8

Trav: 18

Done—robot has crossed the right line!

Expert Solution
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Methods of StringBuilder class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Operations Research : Applications and Algorithms
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT