Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Chris Wyckoff
- * Date: 12/7/16
- * Course Number: CSC-111
- * Course Name: Intro to Java
- * Problem Number: Extra Credit
- * Email: crwyckoff0001@student.stcc.edu
- * Generate a Matrix that has an even numbers of ones in all rows and columns.
- */
- package csc111;
- import java.util.Scanner;
- public class EvenNumberOfOnes {
- // **********************************************
- public static void main(String args[]) {
- final String TITLE = "Even Numer of Ones Matrix Generator";
- final String CONTINUE_PROMPT = "Genrate Another Matrix? [y/N] ";
- System.out.println("Welcome to " + TITLE);
- Scanner sc = new Scanner(System.in);
- do {
- process(sc);
- } while (doThisAgain(sc, CONTINUE_PROMPT));
- sc.close();
- System.out.println("Thank you for using " + TITLE);
- }
- // **********************************************
- private static void process(Scanner sc) {
- boolean isEven;
- int rows = getMatrixRows(sc);
- int cols = getMatrixCols(sc);
- int[][] matrix;
- do {
- matrix = initMatrix(rows, cols);
- if (checkForEvenRows(matrix) && checkForEvenCols(matrix))
- isEven = true;
- else
- isEven = false;
- } while (!isEven);
- printEvenMatrix(matrix);
- }
- // **********************************************
- private static int getMatrixRows(Scanner sc) {
- System.out.println("Enter numbers of rows for matrix ");
- int rows = sc.nextInt();
- return rows;
- }
- // **********************************************
- private static int getMatrixCols(Scanner sc) {
- System.out.println("Enter numbers of columns for matrix ");
- int cols = sc.nextInt();
- return cols;
- }
- // **********************************************
- private static int[][] initMatrix(int rows, int cols) {
- int[][] matrix = new int[rows][cols];
- for (int i = 0; i < matrix.length; i++)
- for (int j = 0; j < matrix[i].length; j++)
- matrix[i][j] = (int) (Math.random() * 2);
- return matrix;
- }
- // **********************************************
- private static boolean checkForEvenRows(int[][] matrix) {
- for (int i = 0; i < matrix.length; i++) {
- int oneCount = 0;
- for (int j = 0; j < matrix[i].length; j++)
- if (matrix[i][j] == 1)
- oneCount++;
- if (oneCount % 2 != 0)
- return false;
- }
- return true;
- }
- // **********************************************
- private static boolean checkForEvenCols(int[][] matrix) {
- for (int j = 0; j < matrix[0].length; j++) {
- int oneCount = 0;
- for (int i = 0; i < matrix.length; i++)
- if (matrix[i][j] == 1)
- oneCount++;
- if (oneCount % 2 != 0)
- return false;
- }
- return true;
- }
- // **********************************************
- private static void printEvenMatrix(int[][] matrix) {
- System.out.println("Your Even Matrix is");
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++)
- System.out.printf("%2d", matrix[i][j]);
- System.out.println();
- }
- }
- // **********************************************
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.println(prompt);
- sc.nextLine();
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement