Advertisement
luliu

Generate Even Number Ones Matrix

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