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

1 Board The computer will simulate a rectangular mxn board. You are required to

ID: 3915995 • Letter: 1

Question

1 Board The computer will simulate a rectangular mxn board. You are required to use a 2-dimensional array to represent the board. The type of this array is up to the programmer. ns m and n at the beginning of the game, and the program should The user will input the di create a board of m rows by n columns. Errors such as nonpositive input should be checked, but you can assume the input is always two integers. The minimum board size is 3x3 and the maximum is 12 x 12. Assume that the points in the board range from (0.0) to (m-1, n-1) inclusive. Hint: You may find it useful to make your own BattleboatsBoard class that contains a constructor to set up the array and all the methods for setting up boats.

Explanation / Answer

Find the required program:

//==============================================================

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class myClass
{
   public static void main (String[] args) throws java.lang.Exception
   {

       // initialize here.
       int m, n, i, j;
       int arr[][] = new int[10][10];
       Scanner scan = new Scanner(System.in);
     
       // enter row and column for array.
       System.out.print(" Enter number of rows for the array : ");
       m = scan.nextInt();
       System.out.print(" Enter number of columns for the array : ");
       n = scan.nextInt();
     
       // enter array elements.
       for(i=0; i<m; i++)
       {
           for(j=0; j<n; j++)
           {
               System.out.println(" Enter array element at ("+i+","+j+"): ");
               arr[i][j] = scan.nextInt();
               while(arr[i][j]<=0)
               {
                System.out.print(" Enter a valid +ve Number: ");
                arr[i][j] = scan.nextInt();
               }
           }
       }
     
       // the 2D array is here.
       System.out.print(" The Array is : ");
       for(i=0; i<m; i++)
       {
           for(j=0; j<n; j++)
           {
               System.out.print(arr[i][j]+ " ");
           }
           System.out.println();
       }
    }
}


//=============================================================