Write a Java program that will be able to generate the sample table below.
The value 1 will start from the origin, increasing all of its neighbor by 1 until the whole array is filled up.

e.g.

How many rows? 5
How many columns? 4
Enter origin coordinate: 3 2

Note: Use exceptions to handle possible errors in the program.

Write a Java program that will be able to generate the sample table below The value 1 will start from the origin increasing all of its neighbor by 1 until the w class=

Respuesta :

tonb

Answer:

class Main {

 public static void fillTable(int [][] t, int or, int oc) {        

   for(int r=0; r<t.length; r++) {

     for(int c=0; c<t[r].length; c++) {

         t[r][c] = Math.max(1+Math.abs(r-or), 1+Math.abs(c-oc));

       }

   }

 }

 public static void dumpTable(int [][] t) {

   for(int r=0; r<t.length; r++) {

     for(int c=0; c<t[r].length; c++) {

       System.out.printf("%3d", t[r][c]);

     }

     System.out.println();

   }

 }

 public static void main(String[] args) {

   int origin_row = 3;

   int origin_col = 2;

   

   int[][] table = new int[5][4]; // rows|cols

   fillTable(table, origin_row, origin_col);

   dumpTable(table);

   

 }

}

Explanation:

Above program does not contain input handling and exception handling, but it does contain the cleverness of calculating the cell values. I'm hoping you can add the input handling yourself?

Below is the required code of Java programming language.

Java

Program code:

//Creating a main class

class Main

{

   //Using int[] to store integer variables

   public static void fillTable(int [][] t, int or, int oc)

   {        

       //Creating a for loop

       for(int r=0; r<t.length; r++)

       {

           for(int c=0; c<t[r].length; c++)

           {

               //Using Matt.abs to return the number's absolute value

               t[r][c] = Math.max(1+Math.abs(r-or), 1+Math.abs(c-oc));

           }

       }

   }

   public static void dumpTable(int [][] t)

   {

       //Again a for loop

       for(int r=0; r<t.length; r++)

       {

           for(int c=0; c<t[r].length; c++)

           {

               System.out.printf("%3d", t[r][c]);

           }

           //Printing the values or the text

           System.out.println();

       }

   }

   //Main method

   public static void main(String[] args)

   {

       //Declaring the variables

       int origin_row = 3;

       int origin_col = 2;

       //Declaring array

       int[][] table = new int[5][4];

       fillTable(table, origin_row, origin_col);

       dumpTable(table);

   }

}

Explanation:

  • Creating a main class.
  • Using int[] to store integer variables.
  • Creating a for loop.
  • Using Matt.abs to return the number's absolute value.
  • Again using a for loop.
  • Printing the values or the text.
  • Main method.
  • Declaring the variables.
  • Declaring array.

Output:

Please find below the attachment of the program code.

Find out more information about Java here:

https://brainly.com/question/26642771

Ver imagen Cricetus