x.Hmive walking robot is able to move only in four directions - North, East, Wes
ID: 3616864 • Letter: X
Question
x.Hmive walking robot is able to move only in four directions - North, East, West, and South.Instructions to move are given to this robot in the form DirDist wherein the robot is meant to move
a distance Dist feet towards the direction Dir -- N, E, W, or S for North, South, East, and West,
respectively. For example, the instruction E22 means that the robot should move 22 feet towards
the East.
Create a program (named Robot.java) that will read instructions from the user during runtime in the format described
above. Your program should output the distance that the robot will be from its initial position.
Execution should terminate when an empty instruction is entered.
Sample Run
C:java Robot
ROBOT
Please enter instruction: N4.0
Distance from initial position: 4.0 feet
Please enter instruction: E3.0
Distance from initial position: 5.0 feet
Please enter instruction: W8.0
Distance from initial position: 6.4031242374328485 feet
Please enter instruction: S16.0
Distance from initial position: 13.0 feet
Please enter instruction:
THE END
The code:
import java.io.*;
public class Cursor{
public static void main(String[] args)throws Exception {
BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
char[][] pad = new char[8][8];
int currentrow = 0;
int currentcolumn = 0;
for(int i=0;i<8;i++)
{
for(int j=0;j<pad.length;j++)
{
if(currentcolumn==j && currentrow==i)
{
pad[i][j]= 'X';
System.out.print(" "+pad[i][j]);
pad[i][j]= '|';
}
else
{
pad[i][j] = '|';
System.out.print(" "+pad[i][j]);
}
}
System.out.println();
}
int cursorbuttons;
boolean Quit=false;
int i=0,j=0;
while(Quit==false)
{
System.out.print("Move: ");
cursorbuttons = Integer.parseInt(dataIn.readLine());
switch(cursorbuttons)
{
case 1: currentrow = currentrow+1;
currentcolumn = currentcolumn-1;
break;
case 2: currentrow = currentrow+1;
break;
case 3: currentrow = currentrow+1;
currentcolumn = currentcolumn+1;
break;
case 4: currentcolumn = currentcolumn-1;
break;
case 5: Quit=true;
break;
case 6: currentcolumn = currentcolumn+1;
break;
case 7: currentrow = currentrow-1;
&nb
Explanation / Answer
This is a simple vector problem. publicstatic void main(String[]args) { // you can change thesource of the input easily here Scanner keyboard= newScanner(System.in); double x= 0.0; double y= 0.0; while(true) { System.out.print("Pleaseenter instruction: "); String line =keyboard.nextLine().trim(); if(line.equalsIgnoreCase("THE END")) { break; } else { char c =Character.toLowerCase(line.charAt(0)); double dist = Double.parseDouble(line.substring(1)); switch(c) { case 'e': x += dist; break; case 'w': x -= dist; break; case 's': y -= dist; break; case 'n': y += dist; break; } // calculate distance double distance = Math.sqrt(x*x+ y*y); System.out.println("Distance from initial position: "+distance+" feet"); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.