1. This question involves the DirectionDisplay class. You will implement two methods using the specifications and examples below. The helper method is provided; its implementation is not shown.
public class DirectionDisplay
{
/** Consumes and returns the next command, which is exactly "L", "R" or "S".
* There are enough commands for every requested call. */
public String readToken() { /* implementation not shown */ }
/** Implements the behavior and preconditions in part (a). */
public int finalDirection(int moves)
{ /* to be implemented in part (a) */ }
/** Implements the behavior and preconditions in part (b). */
public String rotateLabel(String label, int positions)
{ /* to be implemented in part (b) */ }
}Write finalDirection. Begin facing direction 0. Directions are numbered 0, 1, 2 and 3 in clockwise order. Consume exactly moves commands by calling readToken once for each command. An "R" command turns one direction clockwise; an "L" command turns one direction counterclockwise; an "S" command leaves the direction unchanged. Turning past 3 clockwise reaches 0; turning past 0 counterclockwise reaches 3. Return the final direction. Assume moves >= 0. Each call begins at direction 0 independently of earlier calls. You must use readToken appropriately.
Examples
| Call | Successive commands | Return value |
|---|---|---|
| finalDirection(5) | R, R, L, S, R | 2 |
| finalDirection(3) | L, S, L | 2 |
| finalDirection(0) | No helper calls | 0 |
/** Implements the behavior and preconditions specified in this part. */ public int finalDirection(int moves)
Write rotateLabel. Rotate label to the left by positions characters and return the result. Characters moved past the beginning reappear at the end in the same order. Every full rotation of label.length() positions leaves the label unchanged. Assume label is nonempty and not null, and positions >= 0. Do not change the letter case.
Examples
| Method call | Return value |
|---|---|
| rotateLabel("north", 2) | "rthno" |
| rotateLabel("ABCD", 4) | "ABCD" |
| rotateLabel("level", 7) | "velle" |
| rotateLabel("x", 9) | "x" |
/** Implements the behavior and preconditions specified in this part. */ public String rotateLabel(String label, int positions)
00:00
public class DirectionDisplay
{
/** Consumes and returns the next command, which is exactly "L", "R" or "S".
* There are enough commands for every requested call. */
public String readToken() { /* implementation not shown */ }
/** Implements the behavior and preconditions in part (a). */
public int finalDirection(int moves)
{ /* to be implemented in part (a) */ }
/** Implements the behavior and preconditions in part (b). */
public String rotateLabel(String label, int positions)
{ /* to be implemented in part (b) */ }
}