Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name:Calvin Smith
- * Date:12/3/2018
- * Course Number:CSC-111
- * Course Name:Intro to Java
- * Problem Number:Homework #10
- * Email:csmith0002@student.stcc.edu
- * Short Description of the Problem
- * Let the user enter the dimensions of a 2D matrix. Generate matrices to be filled with
- * 1's and 0's until there are an even amount of 1's in every row and column of the matrix.
- */
- package cscPackage;
- import java.util.Scanner;
- public class TwoDimensionArrayApp {
- private static boolean hasEvenOnes(int[][] dims) {
- for (int i = 0; i < dims.length; i++) {
- int oneCount = 0;
- for (int j = 0; j < dims[i].length; j++) {
- if (dims[i][j] == 1) {
- oneCount++;
- }
- }
- if (oneCount % 2 != 0) {
- return false;
- }
- }
- for (int j = 0; j < dims[0].length; j++) {
- int oneCount = 0;
- for (int i = 0; i < dims.length; i++) {
- if (dims[i][j] == 1) {
- oneCount++;
- }
- }
- if (oneCount % 2 != 0) {
- return false;
- }
- }
- return true;
- }
- //**********************************************
- public static int[][] getUserDimensions(Scanner sc) {
- int rows = 0;
- int columns = 0;
- while (rows < 2) {
- System.out.println("Enter the # of rows you want in your matrix: ");
- rows = sc.nextInt();
- sc.nextLine();
- }
- while (columns < 2) {
- System.out.println("Enter the # of columns you want in your matrix: ");
- columns = sc.nextInt();
- sc.nextLine();
- }
- int[][] dims = new int[rows][columns];
- return dims;
- }
- //**********************************************
- public static void printer(int[][] dims) {
- for (int i = 0; i < dims.length; i++) {
- for (int j = 0; j < dims[i].length; j++) {
- System.out.printf("%6d",dims[i][j]);
- }
- System.out.println();
- }
- }
- //**********************************************
- public static int[][] generateValues(int[][] dims) {
- int rowLength = dims.length;
- int colLength = dims[0].length;
- for (int i = 0; i < colLength; i++) {
- for (int j = 0; j < rowLength; j++) {
- dims[j][i] = (int)(Math.random() * 2);
- }
- }
- return dims;
- }
- //***********************************************
- private static void process(Scanner sc, String args[]) {
- int[][] matrix = getUserDimensions(sc);
- do {
- generateValues(matrix);
- if (hasEvenOnes(matrix)) {
- printer(matrix);
- break;
- }
- }while(true);
- }
- //**********************************************
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- //**********************************************
- public static void main(String args[]) {
- final String TITLE = "2D Array App";
- final String CONTINUE_PROMPT = "Do this again? [y/n] ";
- System.out.println("Welcome to " + TITLE);
- Scanner sc = new Scanner(System.in);
- do {
- process(sc, args);
- } while (doThisAgain(sc, CONTINUE_PROMPT));
- sc.close();
- System.out.println("Thank you for using " + TITLE);
- }
- }
Add Comment
Please, Sign In to add comment