Please write in JAVA ONLY Write a method with a void return

Please write in JAVA ONLY!

Write a method with a void return value that inverts all the elements of a two-dimensional array of booleans (true becomes false and false becomes true).

Include code to test your method.

Your output should show all the values of the array prior to inverting them and afterwards.

Solution

BooleanArrayTest.java

import java.util.Scanner;


public class BooleanArrayTest {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter the number of rows: \");
       int rows = scan.nextInt();
       System.out.println(\"Enter the number of columns: \");
       int cols = scan.nextInt();
       boolean b[][] = new boolean[rows][cols];
       for(int i=0; i<rows; i++){
           for(int j=0; j<cols; j++){
               System.out.print(\"Enter value: \");
               b[i][j] = scan.nextBoolean();
           }
       }
       System.out.println(\"Before inverting, Array elements are: \");
       displayArray(b);
       inverts(b);
       System.out.println(\"After inverting, Array elements are: \");
       displayArray(b);
   }
   public static void inverts (boolean b[][]){
       for(int i=0; i<b.length; i++){
           for(int j=0; j<b[i].length; j++){
               b[i][j] = !b[i][j];
           }
       }
   }
   public static void displayArray(boolean b[][]){
       for(int i=0; i<b.length; i++){
           for(int j=0; j<b[i].length; j++){
               System.out.print(b[i][j]+\" \");
           }
           System.out.println();
       }
      
   }

}

Output:

Enter the number of rows:
3
Enter the number of columns:
3
Enter value: true
Enter value: false
Enter value: false
Enter value: true
Enter value: false
Enter value: true
Enter value: false
Enter value: true
Enter value: true
Before inverting, Array elements are:
true false false
true false true
false true true
After inverting, Array elements are:
false true true
false true false
true false false

Please write in JAVA ONLY! Write a method with a void return value that inverts all the elements of a two-dimensional array of booleans (true becomes false and
Please write in JAVA ONLY! Write a method with a void return value that inverts all the elements of a two-dimensional array of booleans (true becomes false and

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site