Advertisement
Guest User

Fixed Extra Credit Even NUmber of Ones

a guest
Dec 7th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. /*
  2. * Name: Chris Wyckoff
  3. * Date: 12/7/16
  4. * Course Number: CSC-111
  5. * Course Name: Intro to Java
  6. * Problem Number: Extra Credit
  7. * Email: crwyckoff0001@student.stcc.edu
  8. * Generate a Matrix that has an even numbers of ones in all rows and columns.
  9. */
  10. package csc111;
  11.  
  12. import java.util.Scanner;
  13.  
  14. public class EvenNumberOfOnes {
  15.  
  16. // **********************************************
  17.  
  18. public static void main(String args[]) {
  19. final String TITLE = "Even Numer of Ones Matrix Generator";
  20. final String CONTINUE_PROMPT = "Genrate Another Matrix? [y/N] ";
  21. System.out.println("Welcome to " + TITLE);
  22. Scanner sc = new Scanner(System.in);
  23. do {
  24. process(sc);
  25. } while (doThisAgain(sc, CONTINUE_PROMPT));
  26. sc.close();
  27. System.out.println("Thank you for using " + TITLE);
  28. }
  29. // **********************************************
  30.  
  31. private static void process(Scanner sc) {
  32. boolean isEven;
  33. int rows = getMatrixRows(sc);
  34. int cols = getMatrixCols(sc);
  35. int[][] matrix;
  36. do {
  37. matrix = initMatrix(rows, cols);
  38. if (checkForEvenRows(matrix) && checkForEvenCols(matrix))
  39. isEven = true;
  40. else
  41. isEven = false;
  42. } while (!isEven);
  43. printEvenMatrix(matrix);
  44. }
  45. // **********************************************
  46.  
  47. private static int getMatrixRows(Scanner sc) {
  48. System.out.println("Enter numbers of rows for matrix ");
  49. int rows = sc.nextInt();
  50. return rows;
  51. }
  52. // **********************************************
  53.  
  54. private static int getMatrixCols(Scanner sc) {
  55. System.out.println("Enter numbers of columns for matrix ");
  56. int cols = sc.nextInt();
  57. return cols;
  58. }
  59. // **********************************************
  60.  
  61. private static int[][] initMatrix(int rows, int cols) {
  62. int[][] matrix = new int[rows][cols];
  63. for (int i = 0; i < matrix.length; i++)
  64. for (int j = 0; j < matrix[i].length; j++)
  65. matrix[i][j] = (int) (Math.random() * 2);
  66. return matrix;
  67. }
  68. // **********************************************
  69.  
  70. private static boolean checkForEvenRows(int[][] matrix) {
  71. for (int i = 0; i < matrix.length; i++) {
  72. int oneCount = 0;
  73. for (int j = 0; j < matrix[i].length; j++)
  74. if (matrix[i][j] == 1)
  75. oneCount++;
  76. if (oneCount % 2 != 0)
  77. return false;
  78. }
  79. return true;
  80. }
  81. // **********************************************
  82.  
  83. private static boolean checkForEvenCols(int[][] matrix) {
  84. for (int j = 0; j < matrix[0].length; j++) {
  85. int oneCount = 0;
  86. for (int i = 0; i < matrix.length; i++)
  87. if (matrix[i][j] == 1)
  88. oneCount++;
  89. if (oneCount % 2 != 0)
  90. return false;
  91. }
  92. return true;
  93. }
  94. // **********************************************
  95.  
  96. private static void printEvenMatrix(int[][] matrix) {
  97. System.out.println("Your Even Matrix is");
  98. for (int i = 0; i < matrix.length; i++) {
  99. for (int j = 0; j < matrix[i].length; j++)
  100. System.out.printf("%2d", matrix[i][j]);
  101. System.out.println();
  102. }
  103. }
  104. // **********************************************
  105.  
  106. private static boolean doThisAgain(Scanner sc, String prompt) {
  107. System.out.println(prompt);
  108. sc.nextLine();
  109. String doOver = sc.nextLine();
  110. return doOver.equalsIgnoreCase("Y");
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement