Advertisement
luliu

Generate Even Number Ones Matrix V2

Dec 4th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. *
  2.  * Lu Liu
  3.  * 12/04/2015
  4.  * CSCI-111-D01
  5.  * Email: lliu0001@student.stcc.edu
  6.  * Project:
  7.  * Generate Even Number Ones Matrix V2
  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.             sc.nextLine();
  31.         } while (doThisAgain(sc, CONTINUE_PROMPT));
  32.         sc.close();
  33.         System.out.println("\nThank you for using " + TITLE);
  34.     }
  35.    
  36.     // ----------------------------------------------------------------------------
  37.     private static boolean doThisAgain(Scanner sc, String prompt) {
  38.         System.out.print(prompt);
  39.         String doOver = sc.nextLine();
  40.         return doOver.equalsIgnoreCase("Y");
  41.     }
  42.  
  43.     // ----------------------------------------------------------------------------
  44.     public static void evenOnesMatrix(int matrix[][]) {
  45.          do {
  46.          getRandomMatrix(matrix);
  47.          System.out.println();
  48.          } while (!hasEvenOnes(matrix));
  49.          
  50.         System.out.println("All rows and columns have even ones");
  51.     }
  52.  
  53.     // ----------------------------------------------------------------------------
  54.     public static int[][] getRandomMatrix(int[][] matrix) {
  55.         for (int i = 0; i < matrix.length; i++) {
  56.             for (int j = 0; j < matrix[i].length; j++) {
  57.                 matrix[i][j] = (int) (Math.random() * 2);
  58.                 System.out.print(matrix[i][j] + " ");
  59.             }
  60.             System.out.println();
  61.         }
  62.         return matrix;
  63.     }
  64.  
  65.     // ----------------------------------------------------------------------------
  66.     public static boolean hasEvenOnes(int matrix[][]) {
  67.         for (int i = 0; i < matrix.length; i++) {
  68.             int count = 0;
  69.             for (int j = 0; j < matrix[i].length; j++) {
  70.                 if (matrix[i][j] == 1)
  71.                     count++;
  72.             }
  73.             if (count % 2 != 0)
  74.                 return false;
  75.         }
  76.         for (int j = 0; j < matrix[0].length; j++) {
  77.             int count = 0;
  78.             for (int i = 0; i < matrix.length; i++) {
  79.                 if (matrix[i][j] == 1)
  80.                     count++;
  81.             }
  82.             if (count % 2 != 0)
  83.                 return false;
  84.         }
  85.         return true;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement