Question: Please help with the following: how would you answer this question Using the class Robot, write a java program to simulate a robot race. Your

Please help with the following: how would you answer this question

Using the class Robot, write a java program to simulate a robot race. Your program should ask the user the size of the grid, and the number of robots that will race, as well as their names. The robot objects must be stored in an array. The winner is the 1st robot to reach the top right corner of the grid.

Your program must work for any size grid and any number of robots. Make sure the number of robots is at least 1 and that the grid size is at least 2x2. Make use of the static methods you wrote for the driver with the code provided. (Write a static method to read and validate the number of robots.)

With the following code: In Java

public class Robot { //instance variables private String name; //stores name of the robot private int x; //x coordinate of robot position private int y; //y coordinate of robot position private char direction; //E W N S //constructors public Robot(){ name = "noName"; x=0; y=0; direction = 'E'; } public Robot(String name){ this.name = name; x=0; y=0; direction = 'E'; } //copy constructor public Robot(Robot r){ name = r.name; x=r.x; y=r.y; direction = r.direction; } //getters public String getName(){ return name; } public char getDirection(){ return direction; } public int getX(){ return x; } public int getY(){ return y; } public void setName(String name){ this.name = name; } @Override public String toString(){ return name+" is facing "+direction +" and at position ("+x+", "+y+")"; } public boolean equals(Object o){ if(o == null) return false; if(!(o instanceof Robot)) return false; Robot r = (Robot)o; return((direction == r.direction) && (x == r.x) && (y == r.y)); } public void changeDirection(){ if(direction=='E') direction = 'N'; else if(direction=='W') direction = 'S'; else if(direction=='N') direction = 'W'; else if(direction=='S') direction = 'E'; } public void move(int noOfSteps,int gridSize){ switch(direction){ case 'E': if( x+noOfSteps = 0){ x = x-noOfSteps; }else{ int xold = x; x = 0; changeDirection(); move((noOfSteps - xold),gridSize); } break; case 'N': if( y+noOfSteps = 0){ y = y-noOfSteps; }else{ int yold = y; y = 0; changeDirection(); move((noOfSteps - yold),gridSize); } break; } } //end of method move() public boolean won(int gridSize){ return((x==gridSize) && (y == gridSize)); } //end of method won() }

RobotDriver.java

import java.util.Scanner; import java.util.Random; public class RobotDriver { public static void displayHeader(){ System.out.println(" = 0 = 0 = 0 = 0 = 0 = 0 = 0 ="); System.out.println(" = 0 0 ="); System.out.println(" = 0 Robot Walk 0 ="); System.out.println(" = 0 0 ="); System.out.println(" = 0 = 0 = 0 = 0 = 0 = 0 = 0 ="); } public static int readGridSize(Scanner sc){ int s; System.out.print("What is the size of your grid ? "); s = sc.nextInt(); sc.nextLine(); return s; } public static void displayClosingMessage(Robot r,int moves){ System.out.println(r.getName()+" reached its final destination in "+moves+" moves."); } public static void main(String[] args) { displayHeader(); Scanner sc = new Scanner(System.in); Random rnd = new Random(); String name; int gridSize; int moves=0; System.out.print("What is name of your Robot ? "); name = sc.nextLine(); gridSize = readGridSize(sc); Robot myRobot = new Robot(name); System.out.println("Time for "+name+" to start walking !!!"); System.out.println("At start "+name+" is facing E and at position (0,0)"); while (!myRobot.won(gridSize)) { moves++; int noOfSteps = rnd.nextInt(gridSize)+1; System.out.println("==> Number of steps to take: "+noOfSteps+"."); myRobot.move(noOfSteps, gridSize); System.out.println(" Result: "+name+" is facing "+myRobot.getDirection()+" and at position ("+myRobot.getX()+", "+myRobot.getY()+")."); } displayClosingMessage(myRobot,moves); } } 

It should look like this

Please help with the following: how would you answer this question Using

the class Robot, write a java program to simulate a robot race.

e Console X RobotRace [Java Application] C:\Program Files\Java\jrel.8.0_121\bin\javaw.exe (1 = @= @= @= @= @= @= @= @= @= @= @ Welcome to the Robot Race = @= @= @= @= @= @= @= @= @= @= @= What is the size of your grid? (Must be at least 2) 1 What is the size of your grid? (Must be at least 2) -2 What is the size of your grid? (Must be at least 2) 2 How many robots will race? (Must have at least one robot in the race) How many robots will race? (Must have at least one robot in the race) 1 Name of robot 1: Solo Move Number 1 * Robot Solo is facing E and at position (0,0) need to take 1 steps. Result: Solo is facing E and at position (1,0) Move Number 2 * Robot Solo is facing E and at position (1,0) need to take 2 steps. New direction: N Result: Solo is facing N and at position (2,1) Move Number 3 * Robot Solo is facing N and at position (2,1) need to take 2 steps. New direction: W Result: Solo is facing W and at position (1,2) Move Number 4 * Robot Solo is facing W and at position (1,2) need to take 1 steps. Result: Solo is facing W and at position (0,2) Move Number 5 * Robot Solo is facing W and at position (0,2) need to take 2 steps. New direction: S Result: Solo is facing S and at position (0,0) Move Number 6 * Robot Solo is facing S and at position (0,0) need to take 1 steps. New direction: E Result: Solo is facing E and at position (1,0) Move Number 7 * Robot Solo is facing E and at position (1,0) need to take 2 steps. New direction: N Result: Solo is facing N and at position (2,1) Move Number 8 ============= * Robot Solo is facing N and at position (2,1) need to take 1 steps. Result: Solo is facing N and at position (2,2) ==> It took 8 rounds for Solo to win the race!!! Hope you didn't loose too much money ... Console x Beh 23 RobotRace [Java Application] C:\Program Files\Java\jrel.8.0_121\bin\javaw.exe (Mar 28, 2017, = @= @= @= @= @= @= @= @= @= @= Welcome to the Robot Race = @= @= @= @= @= @= @= @= @= @= @= What is the size of your grid? (Must be at least 2) 4 How many robots will race? (Must have at least one robot in the race) 2 Name of robot 1: Chuba Name of robot 2: Sparki the Terror Move Number 1 Robot Chuba is facing E and at position (0,0) need to take 1 steps. Result: Chuba is facing E and at position (1,2) * Robot Sparki the Terror is facing E and at position (0,0) need to take 1 steps. Result: Sparki the Terror is facing E and at position (1,0) ess Move Number 2 * Robot Chuba is facing E and at position (1,0) need to take 1 steps. Result: Chuba is facing E and at position (2,0) * Robot Sparki the Terror is facing E and at position (1,0) need to take 4 steps. New direction: N Result: Sparki the Terror is facing N and at position (4,1) Move Number 3 * Robot Chuba is facing E and at position (2,0) need to take 1 steps. Result: Chuba is facing E and at position (3,0) Robot Sparki the Terror is facing N and at position (4,1) need to take 4 steps. New direction: W Result: Sparki the Terror is facing W and at position (3,4) Move Number 4 Robot Chuba is facing E and at position (3,0) need to take 3 steps. New direction: N Result: Chuba is facing N and at position (4,2) * Robot Sparki the Terror is facing W and at position (3,4) need to take 4 steps. New direction: S Result: Sparki the Terror is facing S and at position (0,3) Move Number 5 * Robot Chuba is facing N and at position (4,2) need to take 3 steps. New direction: W Result: Chuba is facing W and at position (3,4) * Robot Sparki the Terror is facing 5 and at position (0,3) need to take 3 steps. Result: Sparki the Terror is facing S and at position (0,0) Move Number 6 Robot Chuba is facing w and at position (3,4) need to take 1 steps. Result: Chuba is facing W and at position (2,4) Robot Sparki the Terror is facing S and at position (0,0) need to take 1 steps. New direction: E Result: Sparki the Terror is facing E and at position (1,0)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!