Advertisement
Omar_Natour

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

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