luliu

Generate Even Number Ones Matrix

Dec 3rd, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. /*
  2.  * Lu Liu
  3.  * 12/03/2015
  4.  * CSCI-111-D01
  5.  * Project:
  6.  * Generate Even Number Ones Matrix
  7.  * Description:
  8.  * 1)Ask the user for the number of rows and the number of columns in the matrix
  9.  * 2)Repeatedly randomly generate a matrix of ones and zeros with those dimensions
  10.  * 3)Find the matrix that all rows and all columns have an even number of ones
  11.  */
  12. import java.util.Scanner;
  13.  
  14. public class EvenOnes {
  15.  
  16.     public static void main(String[] args) {
  17.         final String TITLE = "Generate Even Number Ones Matrix";
  18.         final String CONTINUE_PROMPT = "Do This Again?[y/N]";
  19.         System.out.println("Welcome to " + TITLE);
  20.         Scanner sc = new Scanner(System.in);
  21.  
  22.         System.out.println("Enter the number as rows: ");
  23.         int rows = sc.nextInt();
  24.         System.out.println("Enter the number as columns: ");
  25.         int columns = sc.nextInt();
  26.         int matrix[][] = new int[rows][columns];
  27.         do {
  28.             evenOnesMatrix(matrix);
  29.         } while (doThisAgain(sc, CONTINUE_PROMPT));
  30.         sc.close();
  31.         System.out.println("\nThank you for using " + TITLE);
  32.     }
  33.    
  34.     // ----------------------------------------------------------------------------
  35.     private static boolean doThisAgain(Scanner sc, String prompt) {
  36.         System.out.print(prompt);
  37.         String doOver = sc.nextLine();
  38.         return doOver.equalsIgnoreCase("Y");
  39.     }
  40.  
  41.     // ----------------------------------------------------------------------------
  42.     public static void evenOnesMatrix(int matrix[][]) {
  43.          do {
  44.          getRandomMatrix(matrix);
  45.          System.out.println();
  46.          } while (!hasEvenOnes(getRandomMatrix(matrix)));
  47.         System.out.println("All rows and columns have even ones");
  48.     }
  49.  
  50.     // ----------------------------------------------------------------------------
  51.     public static int[][] getRandomMatrix(int[][] matrix) {
  52.         for (int i = 0; i < matrix.length; i++) {
  53.             for (int j = 0; j < matrix[i].length; j++) {
  54.                 matrix[i][j] = (int) (Math.random() * 2);
  55.                 System.out.print(matrix[i][j] + " ");
  56.             }
  57.             System.out.println();
  58.         }
  59.         return matrix;
  60.     }
  61.  
  62.     // ----------------------------------------------------------------------------
  63.     public static boolean hasEvenOnes(int matrix[][]) {
  64.         for (int i = 0; i < matrix.length; i++) {
  65.             int count = 0;
  66.             for (int j = 0; j < matrix[i].length; j++) {
  67.                 if (matrix[i][j] == 1)
  68.                     count++;
  69.             }
  70.             if (count % 2 != 0)
  71.                 return false;
  72.         }
  73.         for (int j = 0; j < matrix[0].length; j++) {
  74.             int count = 0;
  75.             for (int i = 0; i < matrix.length; i++) {
  76.                 if (matrix[i][j] == 1)
  77.                     count++;
  78.             }
  79.             if (count % 2 != 0)
  80.                 return false;
  81.         }
  82.         return true;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment