Guest User

Untitled

a guest
Dec 10th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. /*
  2. * Name:Calvin Smith
  3. * Date:12/3/2018
  4. * Course Number:CSC-111
  5. * Course Name:Intro to Java
  6. * Problem Number:Homework #10
  7. * Email:csmith0002@student.stcc.edu
  8. * Short Description of the Problem
  9. * Let the user enter the dimensions of a 2D matrix. Generate matrices to be filled with
  10. * 1's and 0's until there are an even amount of 1's in every row and column of the matrix.
  11. */
  12.  
  13. package cscPackage;
  14.  
  15. import java.util.Scanner;
  16.  
  17. public class TwoDimensionArrayApp {
  18.  
  19. private static boolean hasEvenOnes(int[][] dims) {
  20. for (int i = 0; i < dims.length; i++) {
  21. int oneCount = 0;
  22. for (int j = 0; j < dims[i].length; j++) {
  23. if (dims[i][j] == 1) {
  24. oneCount++;
  25. }
  26. }
  27. if (oneCount % 2 != 0) {
  28. return false;
  29. }
  30. }
  31.  
  32. for (int j = 0; j < dims[0].length; j++) {
  33. int oneCount = 0;
  34. for (int i = 0; i < dims.length; i++) {
  35. if (dims[i][j] == 1) {
  36. oneCount++;
  37. }
  38. }
  39. if (oneCount % 2 != 0) {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45.  
  46. //**********************************************
  47.  
  48. public static int[][] getUserDimensions(Scanner sc) {
  49. int rows = 0;
  50. int columns = 0;
  51. while (rows < 2) {
  52. System.out.println("Enter the # of rows you want in your matrix: ");
  53. rows = sc.nextInt();
  54. sc.nextLine();
  55. }
  56.  
  57. while (columns < 2) {
  58. System.out.println("Enter the # of columns you want in your matrix: ");
  59. columns = sc.nextInt();
  60. sc.nextLine();
  61. }
  62.  
  63. int[][] dims = new int[rows][columns];
  64. return dims;
  65. }
  66.  
  67. //**********************************************
  68.  
  69. public static void printer(int[][] dims) {
  70. for (int i = 0; i < dims.length; i++) {
  71. for (int j = 0; j < dims[i].length; j++) {
  72. System.out.printf("%6d",dims[i][j]);
  73. }
  74. System.out.println();
  75. }
  76. }
  77.  
  78. //**********************************************
  79.  
  80. public static int[][] generateValues(int[][] dims) {
  81. int rowLength = dims.length;
  82. int colLength = dims[0].length;
  83. for (int i = 0; i < colLength; i++) {
  84. for (int j = 0; j < rowLength; j++) {
  85. dims[j][i] = (int)(Math.random() * 2);
  86. }
  87. }
  88.  
  89. return dims;
  90. }
  91.  
  92. //***********************************************
  93.  
  94. private static void process(Scanner sc, String args[]) {
  95. int[][] matrix = getUserDimensions(sc);
  96. do {
  97. generateValues(matrix);
  98. if (hasEvenOnes(matrix)) {
  99. printer(matrix);
  100. break;
  101. }
  102. }while(true);
  103.  
  104. }
  105.  
  106. //**********************************************
  107.  
  108. private static boolean doThisAgain(Scanner sc, String prompt) {
  109. System.out.print(prompt);
  110. String doOver = sc.nextLine();
  111. return doOver.equalsIgnoreCase("Y");
  112. }
  113.  
  114. //**********************************************
  115.  
  116. public static void main(String args[]) {
  117. final String TITLE = "2D Array App";
  118. final String CONTINUE_PROMPT = "Do this again? [y/n] ";
  119.  
  120. System.out.println("Welcome to " + TITLE);
  121. Scanner sc = new Scanner(System.in);
  122. do {
  123. process(sc, args);
  124. } while (doThisAgain(sc, CONTINUE_PROMPT));
  125. sc.close();
  126. System.out.println("Thank you for using " + TITLE);
  127. }
  128.  
  129. }
Add Comment
Please, Sign In to add comment