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

(java) Boolean values can be placed in a 3x3 matrix in one of 512 configurations

ID: 3779662 • Letter: #

Question

(java) Boolean values can be placed in a 3x3 matrix in one of 512 configurations. Examples:

Each configuration can also be represented as a binary number where F is 0 and T is 1. Each matrix in the above example corresponds to the following binary numbers:

Again, there are 512 possible configurations and the decimal numbers 0, 1, 2, 3, … , 511 represent all configurations of the matrix.

Write a program which prompts the user to enter a number between 0 and 511 and displays the corresponding boolean matrix.

NOTE: You must use a 3x3 2D array to solve this problem.

Sample Output:

Explanation / Answer

import java.util.Scanner;


public class matrix_boolean {
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       int a[] = new int[9];
       for(int i=0;i<9;i++)
       {
           a[i]=0;
       }
       Scanner scan = new Scanner(System.in);
       int n,i=8;
       System.out.print("Enter a number between 0 and 511:");
       n = scan.nextInt();
       while(n>0)
       {
           a[i] = n%2;
           n=n/2;
           i--;
       }
       for(i=0;i<9;i++)
       {
           if(a[i]==0)
           {
               System.out.print("F ");
           }
           else
           {
               System.out.print("T ");
           }
           if((i+1)%3==0)
           {
               System.out.println("");
           }
       }
   }

}