Loading...
Loading...
A warehouse robot moves on a grid. It can be placed at the origin facing north, at a given point facing north, or at a given point with a given direction. L and R turn it 90 degrees; F moves it one step forward (north is +y, east is +x).
Write a class Robot with fields x, y and facing and three constructors chained with this(...): Robot(), Robot(int x, int y) and Robot(int x, int y, char facing). Add turnLeft(), turnRight() and forward().
DEFAULT, x y or x y D (D is N, E, S or W). The second line is a string of commands.x y D, the final position and direction.Example 1
Input
DEFAULT FFRFF
Output
2 2 E
From (0, 0) facing north: two steps up, turn east, two steps right.
Example 2
Input
3 4 LLF
Output
3 3 S
Two left turns from north face south.
Try the problem first. Open one hint at a time.
Robot() calls this(0, 0) and Robot(int x, int y) calls this(x, y, 'N'): the defaults are in one chain.
Keep the directions in the String "NESW": turning right is the next letter, turning left is the one before.
The number of words on the first line tells you which constructor to call.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor