Advertisement
Guest User

CSC-111

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