Question

In: Computer Science

The input will be a String with multiple not-always-lowercase directions separated by underscores. The last character...

The input will be a String with multiple not-always-lowercase directions separated by underscores. The last character of the input is always an underscore. You must output the final (x, y) coordinates of a robot, starting at (0, 0), after it has followed all of the directions. Print the result in the format “Result: (x, y)”

USE THE SKELETON PROGRAM PROVIDED.

Example #1:

INPUT: up_down_left_down_down_

Result: (-1, -2)

up --move→ y + 1 = 1

down --move→ y - 1 = 0

left --move→ x - 1 = -1

down --move→ y - 1 = -1

down --move→ y - 1 = -2

x = -1, y = -2 at the end

Example #2:

INPUT: up_dOwn_riGHt_LefT_

Result: (0, 0)

/*

* Notice the globally-scoped x and y variables at the top of the class */

import java.util.Scanner;

public class Robot {

public static int x = 0;

public static int y = 0;

public static void main(String[] args) {

Scanner console = new Scanner(System.in);

String directions = console.nextLine();

readDirections(directions);

}

// Isolates each direction as a String in a loop

public static void readDirections(String dirs) {

}

// Moves the robot by editing the x and y variables

public static void move(String dir) {

}

}

Solutions

Expert Solution

Program: In this program, given an input string of directions, we calculate the final (x, y) coordinates of a robot, starting at (0, 0), after it has followed all of the directions. Inside the function readDirections(String dirs) we process the string- convert it to lowercase, remove the ending underscore, then split it into an array based on the regex underscore and then to update the coordinates, traverse each direction in a loop and call move(String dirs) function. Indside the move() function, we update x,y based on direction value.

Code:

import java.util.Scanner;

public class Robot {

    public static int x = 0;

    public static int y = 0;

    public static void main(String[] args) {

        Scanner console = new Scanner(System.in);
        String directions = console.nextLine();

        readDirections(directions);

        System.out.println("(" + x + ", " + y + ")");
    }

// Isolates each direction as a String in a loop

    public static void readDirections(String dirs) {

        // Convert string to lowercase
        dirs = dirs.toLowerCase();

        // Remove ending underscore
        dirs = dirs.substring(0, dirs.length() - 1);

        // Split array based on underscore
        // Example: dirs = "up_down_left_down_down" -> dirArray = {"up","down","left","down","down"}
        String[] dirArray = dirs.split("_");

        // Traverse each directions and call move() function
        for (int i = 0; i < dirArray.length; i++) {
            move(dirArray[i]);
        }
    }

// Moves the robot by editing the x and y variables

    public static void move(String dir) {

        // Check if direction is up, down, left or right
        if (dir.equals("up")) {
            y++;
        } else if (dir.equals("down")) {
            y--;
        } else if (dir.equals("left")) {
            x--;
        } else {
            x++;
        }
    }
}

Output 1:

Output 2:


Related Solutions

A password is a string of ten characters, where each character is a lowercase letter, a...
A password is a string of ten characters, where each character is a lowercase letter, a digit, or one of the eight special characters !, @, #, $, %, &, (, and ). A password is called awesome, if it contains at least one digit or at least one special character. Determine the number of awesome passwords.
Write a program that accepts a string and character as input, then counts and displays the...
Write a program that accepts a string and character as input, then counts and displays the number of times that character appears (in upper- or lowercase) in the string. Use C++ Enter a string: mallet Enter a character: a "A" appears 1 time(s) Enter a string: Racecar Enter a character: R "R" appears 2 time(s)
Write a program whose input is a string which contains a character and a phrase, and...
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. Ex: If the input is: n Nobody the output is: 0...
Write a basic C++ program with function, whose input is a character and a string, and...
Write a basic C++ program with function, whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex: If the input is: n...
In objective-C Task: Write a program whose input is a character and a string, and whose...
In objective-C Task: Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1.You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: n Monday the output is:...
convert this program that is for string into character input data #include "stdafx.h" #include <string.h> #include...
convert this program that is for string into character input data #include "stdafx.h" #include <string.h> #include <stdlib.h> unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength); // function to add to hash table unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength); // function to retrieve data from hash table #define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value) #define HASH_SIZE 100 // size of hash table to...
How To Print Nice Padding Character in a String in C++? 1. User input the width...
How To Print Nice Padding Character in a String in C++? 1. User input the width = 14 2. User input the padding character = + Example output: ++Hello World++ Thanks in advance.
Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.
# PYTHONWrite a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase.Ex: If the input is:n Mondaythe output is:1Ex: If the input is:z Today is Mondaythe output is:0Ex: If the input is:n It's a sunny daythe output is:2Case matters.Ex: If the input is:n Nobodythe output is:0n is different than N.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT