Omar_Natour

Natour, O. 12/3/15 Csc-111-D01 Extracredit(Exam 4)

Dec 3rd, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. /*
  2.  * Name:Omar Natour
  3.  * Date: 12/3/15
  4.  * Course Number: Csc-111-D01
  5.  * Course Name: Intro to Java
  6.  * Problem Number: Extra Credit(Exam 4)
  7.  * Short Description of the Problem: Generate and array with an even number of ones in ever row and column.
  8.  */
  9.  
  10. import java.util.Scanner;
  11. import java.lang.Math;
  12.  
  13. public class ExtraCred {
  14.     public static void main(String[] args) {
  15.         final String TITLE = "Even Number of Ones Array Generator";
  16.         final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  17.  
  18.         System.out.println("Welcome to " + TITLE + "!");
  19.         Scanner sc = new Scanner(System.in);
  20.         String doover;
  21.         do {
  22.             print(correct(generate(collect())));
  23.  
  24.             System.out.print(CONTINUE_PROMPT);
  25.             doover = sc.next();
  26.         } while (doover.equalsIgnoreCase("Y"));
  27.  
  28.         sc.close();
  29.         System.out.println("Thank you for using " + TITLE + ".");
  30.     }
  31.  
  32.     public static int[] collect() {
  33.         Scanner input = new Scanner(System.in);
  34.         int[] size = new int[2];
  35.  
  36.         System.out.print("Please enter a value of the rows and column length of your matrix:");
  37.  
  38.         for (int i = 0; i < size.length; i++)
  39.             size[i] = input.nextInt();
  40.  
  41.         // input.close(); // this seems to close sc as well. why is that?
  42.         return size;
  43.     }
  44.  
  45.     public static int[][] generate(int[] spec) {
  46.         int[][] matrix = new int[spec[0]][spec[1]];
  47.  
  48.         for (int i = 0; i < (spec[0]); i++) {
  49.             for (int j = 0; j < (spec[1]); j++) {
  50.                 matrix[i][j] = (int) (Math.random() * 2);
  51.             }
  52.         }
  53.         return matrix;
  54.     }
  55.  
  56.     public static int[][] correct(int[][] inc) {
  57.         int[][] correct = inc;
  58.         int[] size1 = { inc.length, inc[0].length };
  59.         int[] rowC = new int[inc.length];
  60.         int[] columnC = new int[inc[0].length];
  61.  
  62.         boolean row = false;
  63.         boolean column = false;
  64.  
  65.         while (!(row && column)) {
  66.             for (int i = 0; i < rowC.length; i++)
  67.                 rowC[i] = 0;
  68.             for (int i = 0; i < columnC.length; i++)
  69.                 columnC[i] = 0;
  70.  
  71.             for (int i = 0; i < inc.length; i++) {
  72.                 for (int j = 0; j < inc[0].length; j++) {
  73.                     if (inc[i][j] == 1)
  74.                         rowC[i]++;
  75.                 }
  76.             }
  77.  
  78.             for (int i = 0; i < inc[0].length; i++) {
  79.                 for (int j = 0; j < inc.length; j++) {
  80.                     if (inc[j][i] == 1)
  81.                         columnC[i]++;
  82.                 }
  83.             }
  84.  
  85.             for (int i = 0; i < inc.length; i++) {
  86.                 if (rowC[i] % 2 != 0) {
  87.                     row = false;
  88.                     break;
  89.                 } else
  90.                     row = true;
  91.             }
  92.  
  93.             for (int i = 0; i < inc[0].length; i++) {
  94.                 if (columnC[i] % 2 != 0) {
  95.                     column = false;
  96.                     break;
  97.                 } else
  98.                     column = true;
  99.             }
  100.  
  101.             if (!(row))
  102.                 inc = generate(size1);
  103.             else if (!(column))
  104.                 inc = generate(size1);
  105.             else
  106.                 correct = inc;
  107.         }
  108.         return correct;
  109.     }
  110.  
  111.     public static void print(int[][] p) {
  112.         for (int i = 0; i < p.length; i++) {
  113.             for (int j = 0; j < p[0].length; j++)
  114.                 System.out.print(p[i][j]);
  115.             System.out.println();
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment