Answered You can buy a ready-made answer or pick a professional tutor to order an original one.
Convert User Input to an Array of KivaCommands The RemoteControl class currently takes in a String of commands from the user that looks like "FFFTRF". However, our Kiva robots cannot understand ins
Convert User Input to an Array of KivaCommands
The RemoteControl class currently takes in a String of commands from the user that looks like "FFFTRF". However, our Kiva robots cannot understand instructions in that form. They can simply make one move at a time, accepting a single KivaCommand. So, we'll need to take the user inputted commands and convert them into a more Kiva friendly format, a list of KivaCommands that we can send to the robot's move method one at a time.
Write the Convert Method Using continue and/or break
Create a helper method in the RemoteControl class called convertToKivaCommands(). This method should take a String as a parameter, which will be the commands that the user types into the console (for example "FFFTRF"). It should return an array of KivaCommands (in this case FORWARD, FORWARD, FORWARD, TAKE, TURN_RIGHT, FORWARD). We recommend doing this by using the values() method of the KivaCommand enum to get an array of all of the KivaCommands. You can then use the getDirectionKey() method to determine which KivaCommand each char in the String should be converted to. If the user enters a character that does not correspond to a command throw an IllegalArgumentException with a useful error message.
Note: When you want to print out the contents of a KivaCommand[] array variable called commands, for example, you would call Arrays.toString(commands) to turn the array into a single String. You'll need to import java.util.Arrays to use this method. Here is some sample code that you can play with in BlueJ to demonstrate this, then use the approach for your needs:
- @
- ANSWER
import java.lang.Math;
import java.util.Scanner;
import java.util.Arrays ;
public class RemoteControl {
public KivaCommands[] convertToKivaCommands(String parameter){
KivaCommands[] GOTOORDERS = new KivaCommands[parameter.length()];
KivaCommands[] DETERMINE = KivaCommands.values();
int i = 0;
for (char directionKey : parameter.toCharArray()){
for (KivaCommands command : DETERMINE){
if (directionKey == command.getDirectionKey()){
GOTOORDERS[i] = command;
break;
}
}
if (GOTOORDERS[i] == null){
throw new IllegalArgumentException("Character \'"+directionKey+"\' does not correspond to a command!");
}
i++;
}
return GOTOORDERS;
}
}// end class
// enum
enum KivaCommands
{
FORWARD('F'),
TURN_LEFT('L'),
TURN_RIGHT('R'),
TAKE('T'),
DROP('D');
private char action;
KivaCommands(char directionKey)
{
action = directionKey;
}
public char getDirectionKey()
{
return action;
}
}// end enum
import java.lang.Math;
import java.util.Scanner;
import java.util.Arrays ;
public class RemoteControlTest {
public static void main(String [] args){
// keyboard from console
Scanner input = new Scanner(System.in);
// instance object class from RemoteControl
RemoteControl x = new RemoteControl();
System.out.print ( "Enter a String of commands : " );
String orders = input.next();
// array of KivaCommands with length of orders
KivaCommands commands[] = new KivaCommands[orders.length()];
// calling method
commands = x.convertToKivaCommands(orders);
System.out.print(Arrays.toString(commands ) );
}
}