| Problem Statement |
| |
A simple line drawing program uses a blank 20x20 pixel canvas and a directional cursor that starts at the upper left corner pointing straight down. the upper left corner of the canvas is at (0, 0) and the lower right corner is at (19, 19 ). you are given a string [], Commands , Each element of which contains one of two possible commands. A command of the form "forward X" means that the cursor shocould move forward by X pixels. each pixel on its path, including the start and end points, is painted black. the only other command is "Left", which means that the cursor shoshould change its direction by 90 degrees counterclockwise. so, if the cursor is initially pointing straight down and it takes es a single "Left" command, it will end up pointing straight to the right. execute all the commands in order and return the resulting 20x20 pixel canvas as a string [] Where character J of element I represents the pixel at (I, j ). black pixels shoshould be represented as uppercase 'X' characters and blank pixels shoshould be represented '. 'characters. |
| Definition |
|
| Class: |
Drawlines |
| Method: |
Execute |
| Parameters: |
String [] |
| Returns: |
String [] |
| Method signature: |
String [] execute (string [] commands) |
| (Be sure your method is public) |
|
|
|
| Notes |
| - |
The cursor only paints the canvas if it moves (see example 1 ). |
| Constraints |
| - |
CommandsWill contain between 1 and 50 elements, inclusive. |
| - |
Each elementCommandsWill be formatted as either "Left" or "forward X" (quotes for clarity only), where X is an integer between 1 and 19, intrusive, with no extra leading zeros. |
| - |
When executingCommandsIn order, the cursor will never leave the 20x20 pixel canvas. |
| Examples |
| 0) |
|
| |
{"Forward 19", "Left", "Forward 19", "Left", "Forward 19", "Left", "Forward 19 "} |
|
Returns: {"xxxxxxxxxxxxxxxxxx", "X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," X .................. X "," xxxxxxxxxxxxxxxxxxxx "} |
| This sequence of commands draws a 20x20 outline of a square. the cursor is initially at (0, 0) pointing straight down. it then travels to (0, 19) after the first forward command, painting each pixel along its path with '*'. it then rotates 90 degrees left, travels to (19, 19), rotates 90 degrees left, travels to (19, 0), rotates 90 degrees left, and finally travels back to (0, 0 ). |
|
|
| 1) |
|
| |
{"Left", "Left ", "Left" } |
|
Returns :{".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... "} |
| the cursor spins round and round, but never actually paints any pixels. The result is an empty canvas. |
|
|
| 2) |
|
| |
|
|
Returns: {"x ................... "," X ................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... ",".................... "} |
| going forward by one pixel creates a line that is 2 pixels long because both the start and end points are painted. |
|
|
| 3) |
|
| |
{"Left", "Forward 19", "Left", "forward 18", "Left ", "forward 17", "Left", "Forward 16", "Left", "forward 15 ", "Left", "forward 14", "Left", "forward 13", "Left ", "Left", "Left", "forward 12", "Left", "Forward 11", "Left", "Left ", "Left", "Forward 10", "Left", "Forward 9", "Left ", "Forward 8", "Left", "forward 7 "} |
|
Returns: {"xxxxxxxxxxxxxxxxxx ","................... X ",".. xxxxxxxxxxxxxxxx. X ",".. X .............. x. X ",".. x. xxxxxxxxxxxx. x. X ",".. x. X .......... x. x. X ",".. x. x. XXXXXXXX. x. x. X ",".. x. x. X ........ x. x. X ",".. x. x. X ........ x. x. X ",".. x. x. X ........ x. x. X ",".. x. x. X ........ x. x. X ",".. x. x. X ........ x. x. X ",".. x. x. X ........ x. x. X ",".. x. x. X ........ x. x. X ",".. x. x. xxxxxxxxxx. x. X ",".. x. X ............ x. X ",".. x. xxxxxxxxxxxxxx. X ",".. X ................ X ",".. xxxxxxxxxxxxxxxxxx ",".................... "} |
|
|