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

I need help writing a JAVA code with these instructions. Thanks! Write a program

ID: 3735163 • Letter: I

Question

I need help writing a JAVA code with these instructions. Thanks! Write a program that can perform the following tasks: 1. Ask user to input an integer N, which will be the side length of a 2D square board. 2. Create a 1D empty array (with appropriate size) that will later be used to represent the 2D square board. 3. Fill the 1D array with character ‘O’. 4. Ask user to enter 2 integer numbers: x-coordinate and y-coordinate. a. These 2 numbers will be entered one at a time (see example in sample output). b. If either of these number is larger than N, or equals to 0, show error message and ask user to re-enter the coordinate. 5. Put a ‘X’ character at location (x, y) of the 2D board. a. If the corresponding location has already been marked as ‘X’. Output message to tell user that location has already been marked. 6. Print out the current 2D board. 7. Repeat from step 4 until user enter “-1” at any point.

Explanation / Answer

ANSWER:

import java.util.Scanner;

public class GameBoard {

   * @param args

   public static void main(String[] args) {

       int n;

       char[][] board;

       Scanner scanner;

       try {

           scanner = new Scanner(System.in);

           System.out.println("The BOARD VERSION 1.0- (enter -1 to quit)");

           System.out.print("Please enter the side length of the board:");

           n = scanner.nextInt();

           board = new char[n][n];

           System.out.println("Creating " + n + "X" + n + " board...");

           for (int i = 0; i < n; i++) {

               for (int j = 0; j < n; j++) {

                   board[i][j] = 'O';

               }

           }

           System.out.println("Done.");

           do {

               System.out.print("Please enter the x-coordinate:");

               int x = scanner.nextInt();

               if (x == -1) {

                   System.out.println("Goodbye! :)");

                   break;

               }

               if (x > n) {

                   System.out.println("Input Error!");

                   System.out.println("Please Renter");

                   continue;

               }

               System.out.print("Please enter the y-coordinate:");

               int y = scanner.nextInt();

               if (y == -1) {

                   System.out.println("Goodbye! :)");

                   break;

               }

               if (y > n) {

                   System.out.println("Input Error!");

                   System.out.println("Please Renter");

                   continue;

               }

               if (board[y - 1][x - 1] == 'O') {

                   System.out.println("Marking location (" + x + " , " + y

                           + ")...");

                   board[y - 1][x - 1] = 'X';

               } else {

                   System.out.println("Location (" + x + " , " + y

                           + ") already marked...");

               }

               for (int i = 0; i < n; i++) {

                   for (int j = 0; j < n; j++) {

                       System.out.print(board[i][j]);

                   }

                   System.out.println();

               }

           } while (true);

           System.out.println("End of processing...");

       } catch (Exception e) {

           // TODO: handle exception

       }

   }

}

OUTPUT:

The BOARD VERSION 1.0- (enter -1 to quit)

Please enter the side length of the board:5

Creating 5X5 board...

Done.

Please enter the x-coordinate:3

Please enter the y-coordinate:2

Marking location (3 , 2)...

OOOOO

OOXOO

OOOOO

OOOOO

OOOOO

Please enter the x-coordinate:4

Please enter the y-coordinate:1

Marking location (4 , 1)...

OOOXO

OOXOO

OOOOO

OOOOO

OOOOO

Please enter the x-coordinate:3

Please enter the y-coordinate:2

Location (3 , 2) already marked...

OOOXO

OOXOO

OOOOO

OOOOO

OOOOO

Please enter the x-coordinate:6

Input Error!

Please Renter

Please enter the x-coordinate:5

Please enter the y-coordinate:5

Marking location (5 , 5)...

OOOXO

OOXOO

OOOOO

OOOOO

OOOOX

Please enter the x-coordinate:-1