Write a Java program to solve the following problem using modular.docx
1. Write a Java program to solve the following problem using
modularity: Write a method that will initialize a 2-dimensional
array as follows: the array is split into 4 equal quadrants, the
North-West and South-East quadrants are initialized with 1s and
the North-East and the South-West quadrants are initialized
with 0s. Write another method to print a 2-dimensional array.
Your program will invoke the method to initialize the array and
the method to print it. Use dynamic arrays and ask the user for
the row size and the column size - both should be even numbers.
Write your program so that it will generate output similar to the
sample output below: Do you want to start(Y/N): y Enter row
size: 5 ERROR! Should be positive and even. REENTER: 6
Enter column size: 9 ERROR! Should be positive and even.
REENTER: 8 The matrix is: 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0
1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 Do
you want to continue (Y/N): y Enter row size: 4 Enter column
size: 6 The matrix is: 1 1 0 0 1 1 0 0 1 1 0 0 0 0 1 1 0 0 1 1 0 0
1 1 Do you want to continue (Y/N): n
Solution
Test2_1.java
import java.util.Scanner;
public class Test2_1 {
/**
2. * @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int row, col;
char yesNo;
Scanner input = new Scanner(System.in);
System.out.println(" Do you want to start (Y/N)");
yesNo = input.next().charAt(0);
if(yesNo =='y' || yesNo == 'Y'){
while(true){
System.out.println("Enter row size : ");
row = input.nextInt();
while((row % 2 != 0 && row > 1) || row ==0 ){
System.out.println("Error: Should be positive and
even. RENETER : ");
row = input.nextInt();
if(row % 2 == 0) break;
}
System.out.println("Enter Column size : ");
col = input.nextInt();
while((col % 2 != 0 && col > 1) || col ==0 ){
System.out.println("Error: Should be positive and
even. RENETER : ");
3. col = input.nextInt();
if(col % 2 == 0) break;
}
int a[][] = new int[row][col];
int output[][] = readArray(a);
printArray(output);
System.out.println("Do you want to contnue (Y/N)");
yesNo = input.next().charAt(0);
if(yesNo =='n' || yesNo == 'N'){
break;
}
}
}
}
public static int[][] readArray(int a[][]){
int col = a[0].length;
int row = a.length;
for(int i=0; i<a.length; i++){
for(int j=0; j<a[i].length; j++){
if(i < row/2){
if(j < col/2){
a[i][j] = 1;
}
else{