Guest User

CSC-111

a guest
Dec 4th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. /*
  2.  * Name: Joshua Bednaz
  3.  * Date: 12/4/16
  4.  * Course Number: CSC-111
  5.  * Course Name: Intro to Java programming
  6.  * Problem Number: Bones problem
  7.  * This program makes a matrix and then checks if it has an even number of ones in each column and row.
  8.     It will loop and make a new arrays until one is correct.
  9.  */
  10. import java.util.Scanner;
  11.  
  12. public class matrixchallangerevised {
  13.  
  14.     private static boolean doThisAgain(Scanner sc, String prompt) {
  15.         System.out.print(prompt);
  16.         String doOver = sc.nextLine();
  17.         return doOver.equalsIgnoreCase("Y");
  18.     }
  19.  
  20.     // get input row and column
  21.     private static String[] process(Scanner sc) {
  22.         int a;
  23.         int b;
  24.         System.out.print("Enter number of rows: \n");
  25.         a = sc.nextInt();
  26.         System.out.print("Enter number of Columes: \n");
  27.         b = sc.nextInt();
  28.         String s[] = { Integer.toString(a), Integer.toString(b) };
  29.         return s;
  30.     }
  31.  
  32.     // check if matrix is possible
  33.     private static boolean scancheck(String s[]) {
  34.         boolean t = false;
  35.         int a = Integer.parseInt(s[0]);
  36.         int b = Integer.parseInt(s[1]);
  37.         if ( a < 2 || b < 2) {
  38.             t = true;
  39.             System.out.println("The following inputs both arent even.");
  40.             System.out
  41.                     .println("It wil be impossible to generate a materix and comply with the requiments with rows of "
  42.                             + a + " and columes " + b + ".");
  43.         }
  44.         return t;
  45.     }
  46.  
  47.     // initarray=====================================
  48.     private static int[][] initarray(String s[], Scanner input) {
  49.         int b = Integer.parseInt(s[0]);
  50.         int c = Integer.parseInt(s[1]);
  51.         int[][] a = new int[b][c];
  52.         for (int i = 0; i < a.length; i++) {
  53.             for (int j = 0; j < a[i].length; j++) {
  54.                 a[i][j] = (int) (2 * Math.random());
  55.             }
  56.         }
  57.         return a;
  58.     }// ==========================================================
  59.  
  60.     // display array ===========================
  61.     private static String display(int[][] a) {
  62.         String s = "";
  63.         for (int i = 0; i < a.length; i++) {
  64.             for (int j = 0; j < a[i].length; j++) {
  65.                 s = s + Integer.toString(a[i][j]) + " ";
  66.             }
  67.             s = s + "\n";
  68.         }
  69.         return s;
  70.     }
  71.  
  72.     // matrix check horizontal check====================
  73.     private static boolean checkHM(int a[][]) {
  74.         int one = 0;
  75.         for (int i = 0; i < a.length; i++){
  76.             for (int j = 0; j < a[i].length; j++)
  77.                 if (a[i][j] == 1)
  78.                     one++;
  79.             if ( one % 2 != 0)
  80.                 return false;
  81.             one = 0;
  82.         }
  83.         return true;
  84.     }
  85.    
  86.     //Matrix check vertical =====================
  87.     private static boolean checkVM(int a[][]){
  88.         int one = 0;
  89.         for (int k = 0 ; k < a.length ; k++){
  90.         for (int j = 0; j < a[1].length; j++){
  91.             for (int i =0 ; i< a.length; i++)
  92.                     if (a[i][j] == 1)
  93.                         one++;
  94.             if ( one % 2 != 0)
  95.                 return false;
  96.             one = 0;
  97.         }
  98.         }
  99.         return true;
  100.         }
  101.    
  102.     //matrix check both horizontal and vertical
  103.     private static boolean checkM(int a[][]){
  104.         return (checkHM(a)&&checkVM(a));
  105.     }
  106.    
  107.    
  108.     private static void run(Scanner input) {
  109.         String sCheck[]= process(input);
  110.         if (scancheck(sCheck)){
  111.             return;
  112.         }
  113.         int[][] a={};
  114.         int count =0;
  115.         do {
  116.             a =  initarray(sCheck, input);
  117.             count++;
  118.         }while(!checkM(a));
  119.         System.out.println("it took " + count+ " trys to get the followung array");
  120.         System.out.println(display(a));
  121.     }
  122.  
  123.     public static void main(String args[]) {
  124.         final String TITLE = "Matrix Challenge";
  125.         final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  126.         final String rules = "All rows and columns must have the same number of zeros and ones.";
  127.         System.out.println("Welcome to " + TITLE);
  128.         System.out.println(rules);
  129.         Scanner input = new Scanner(System.in);
  130.         do {
  131.             run(input);
  132.             input.nextLine();
  133.         } while (doThisAgain(input, CONTINUE_PROMPT));
  134.         input.close();
  135.         System.out.println("Thank you for using " + TITLE);
  136.     }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment