Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name: Joshua Bednaz
- * Date: 12/4/16
- * Course Number: CSC-111
- * Course Name: Intro to Java programming
- * Problem Number: Bones problem
- * Email: jabednaz0001@student.stcc.edu
- * This program makes a matrix and then checks if it has an even number of ones in each column and row.
- It will loop and make a new arrays until one is correct.
- */
- import java.util.Scanner;
- public class matrixchallangerevised {
- private static boolean doThisAgain(Scanner sc, String prompt) {
- System.out.print(prompt);
- String doOver = sc.nextLine();
- return doOver.equalsIgnoreCase("Y");
- }
- // get input row and column
- private static String[] process(Scanner sc) {
- int a;
- int b;
- System.out.print("Enter number of rows: \n");
- a = sc.nextInt();
- System.out.print("Enter number of Columes: \n");
- b = sc.nextInt();
- String s[] = { Integer.toString(a), Integer.toString(b) };
- return s;
- }
- // check if matrix is possible
- private static boolean scancheck(String s[]) {
- boolean t = false;
- int a = Integer.parseInt(s[0]);
- int b = Integer.parseInt(s[1]);
- if ( a < 2 || b < 2) {
- t = true;
- System.out.println("The following inputs both arent even.");
- System.out
- .println("It wil be impossible to generate a materix and comply with the requiments with rows of "
- + a + " and columes " + b + ".");
- }
- return t;
- }
- // initarray=====================================
- private static int[][] initarray(String s[], Scanner input) {
- int b = Integer.parseInt(s[0]);
- int c = Integer.parseInt(s[1]);
- int[][] a = new int[b][c];
- for (int i = 0; i < a.length; i++) {
- for (int j = 0; j < a[i].length; j++) {
- a[i][j] = (int) (2 * Math.random());
- }
- }
- return a;
- }// ==========================================================
- // display array ===========================
- private static String display(int[][] a) {
- String s = "";
- for (int i = 0; i < a.length; i++) {
- for (int j = 0; j < a[i].length; j++) {
- s = s + Integer.toString(a[i][j]) + " ";
- }
- s = s + "\n";
- }
- return s;
- }
- // matrix check horizontal check====================
- private static boolean checkHM(int a[][]) {
- int one = 0;
- for (int i = 0; i < a.length; i++){
- for (int j = 0; j < a[i].length; j++)
- if (a[i][j] == 1)
- one++;
- if ( one % 2 != 0)
- return false;
- one = 0;
- }
- return true;
- }
- //Matrix check vertical =====================
- private static boolean checkVM(int a[][]){
- int one = 0;
- for (int k = 0 ; k < a.length ; k++){
- for (int j = 0; j < a[1].length; j++){
- for (int i =0 ; i< a.length; i++)
- if (a[i][j] == 1)
- one++;
- if ( one % 2 != 0)
- return false;
- one = 0;
- }
- }
- return true;
- }
- //matrix check both horizontal and vertical
- private static boolean checkM(int a[][]){
- return (checkHM(a)&&checkVM(a));
- }
- private static void run(Scanner input) {
- String sCheck[]= process(input);
- if (scancheck(sCheck)){
- return;
- }
- int[][] a={};
- int count =0;
- do {
- a = initarray(sCheck, input);
- count++;
- }while(!checkM(a));
- System.out.println("it took " + count+ " trys to get the followung array");
- System.out.println(display(a));
- }
- public static void main(String args[]) {
- final String TITLE = "Matrix Challenge";
- final String CONTINUE_PROMPT = "Do this again? [y/N] ";
- final String rules = "All rows and columns must have the same number of zeros and ones.";
- System.out.println("Welcome to " + TITLE);
- System.out.println(rules);
- Scanner input = new Scanner(System.in);
- do {
- run(input);
- input.nextLine();
- } while (doThisAgain(input, CONTINUE_PROMPT));
- input.close();
- System.out.println("Thank you for using " + TITLE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement