Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Create a 2D character array with 5 rows and 5 columns To start, fill every

ID: 3915072 • Letter: J

Question

 JAVA Create a 2D character array with 5 rows and 5 columns To start, fill every position with an O. Then, fill the [0][0] position with an X. This X represents the user's "avatar".  Create a loop which does the following: -Starts by printing the array -Asks the user for what direction they would like to go -Waits for user input -Moves the avatar in that direction, if it exists (ie: up=row-1, right=column+1) -If it does not exist, tell the user and exit the program -Fill the avatar's new position with an X -Fill the avatar's old position with an O  Example Output:  Current Map: ========= X O O O O O O O O O O O O O O O O O O O O O O O O ========= Which direction would you like to move? >down  Current Map: ========= O O O O O X O O O O O O O O O O O O O O O O O O O ========= Which direction would you like to move? >right  Current Map: ========= O O O O O O X O O O O O O O O O O O O O O O O O O ========= Which direction would you like to move? >up  Current Map: ========= O X O O O O O O O O O O O O O O O O O O O O O O O ========= Which direction would you like to move? >up  Error: You cannot move there from your current position Exiting program...

Explanation / Answer

Program:   MAP.java

Source code :

//import statement to import scanner class
import java.util.Scanner;
class MAP{
   //row and column declaration
   static int row=0,column=0;
  
   //main method
   public static void main(String[] args){
  
       //2d array declaration
       char[][] map2d=new char[5][5];
      
       //user input variable
       String direction;
      
       Scanner sc=new Scanner(System.in);
      
       //initialization array with 'o' character
       for(int i=0;i<5;i++){
           for(int j=0;j<5;j++){
               map2d[i][j]='o';
           }
       }
       map2d[0][0]='x';
       //infinite loop that executes the logic
       while(true){
           displayMap(map2d);
           System.out.println("========= Which direction would columnou like to move?");
           direction=sc.nextLine();
           if(direction.equals("up")){
               if(row<=0){
                   System.out.println("Up movement can't be performed so,exiting the program");
                   break;
               }
               else{
                   map2d[row--][column]='o';
                   map2d[row][column]='x';
                  
               }
           }
           else if(direction.equals("down")){
               if(row>=4){
                   System.out.println("Down movement can't be performed so,exiting the program");
                   break;
               }
               else{
                   map2d[row++][column]='o';
                   map2d[row][column]='x';
                  
               }
           }
           else if(direction.equals("right")){
               if(column>=4){
                   System.out.println("Right movement can't be performed so,exiting the program");
                   break;
               }
               else{
                   map2d[row][column++]='o';
                   map2d[row][column]='x';
                  
               }
           }
           else if(direction.equals("left")){
               if(column<=0){
                   System.out.println("Left movement can't be performed so,exiting the program");
                   break;
               }
               else{
                   map2d[row][column--]='o';
                   map2d[row][column]='x';
                  
               }
           }
           else{
               System.out.println("Error:You cannot move there from your current position Exiting program");  
               break;
           }
       }  
   }
  
   //displaying the map elements
   static void displayMap(char[][] map2d){
       System.out.println("Current Map: =========");
       for(int i=0;i<5;i++){
           for(int j=0;j<5;j++){
               System.out.print(map2d[i][j]);
           }
           System.out.println();
       }  
   }
}