Concept explainers
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!
Step by stepSolved in 3 steps with 3 images
- In Python, Solve the locker riddle which reads as follows:Imagine 100 lockers numbered 1 to 100 with 100 students lined up in front of those 100lockers:The first student opens every locker.The second student closes every 2nd locker.The 3rd student changes every 3rd locker; if it’s closed, she opens it; if it’s open, she closes it.The 4th student changes every fourth locker (e.g., 4th, 8th, etc.).The 5th student changes every 5th locker (e.g., 5th, 10th, etc.).That same pattern continues for all 100 students. Which lockers are left open after all 100 students have walked the rowof lockers?arrow_forwardusing java language Your program will identify if the shape is a square or a rectangle. Input the length and width of the shape, and display if it’s a rectangle or square, with its area and perimeter. Use the following methods in your program. inputLength() – returns the input length; inputWidth() – returns the input width; analyze(length,width) – based on the parameters length and width, returns a Boolean value if the shape is square or not compute(length,width) – based on the parameters length and width, display the area and the perimeter of the shape.arrow_forwardWrite a code in Java Programming Your task is to simulate a coffee shop using the observer pattern (do not use the deprecate one). The coffee shop serves two types of coffee: espresso and cappuccino. The shop has two baristas, EspressoBarista and CappuccinoBarista. EspressoBarista generates random espresso orders between 1 and 3 every 2 seconds, while CappuccinoBarista generates random cappuccino orders between 1 and 3 every 3 seconds. The UML design diagram is shown below. Property ChangeListenerfac Observablenerfacto Coffeeshop HashMap Sting, integer> ingredients ExpresssCartate The CoffeeShop class holds the current stock of ingredients and the CoffeeShop constructor initializes the count for each ingredient. The required ingredients for each type of coffee are as follows: • Espresso: 1 shot of espresso, 1 ounce of milk Cappuccino: 1 shot of espresso, 2 ounces of milk, 1 ounce of foam If an order cannot be fulfilled because there are not enough ingredients, a warning message…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education