Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Name:Omar Natour
- * Date: 12/3/15
- * Course Number: Csc-111-D01
- * Course Name: Intro to Java
- * Problem Number: Extra Credit(Exam 4)
- * Email:ojnatour0001@student.stcc.edu
- * Short Description of the Problem: Generate and array with an even number of ones in ever row and column.
- */
- import java.util.Scanner;
- import java.lang.Math;
- public class ExtraCred {
- public static void main(String[] args) {
- final String TITLE = "Even Number of Ones Array Generator";
- final String CONTINUE_PROMPT = "Do this again? [y/N] ";
- System.out.println("Welcome to " + TITLE + "!");
- Scanner sc = new Scanner(System.in);
- String doover;
- do {
- print(correct(generate(collect())));
- System.out.print(CONTINUE_PROMPT);
- doover = sc.next();
- } while (doover.equalsIgnoreCase("Y"));
- sc.close();
- System.out.println("Thank you for using " + TITLE + ".");
- }
- public static int[] collect() {
- Scanner input = new Scanner(System.in);
- int[] size = new int[2];
- System.out.print("Please enter a value of the rows and column length of your matrix:");
- for (int i = 0; i < size.length; i++)
- size[i] = input.nextInt();
- // input.close(); // this seems to close sc as well. why is that?
- return size;
- }
- public static int[][] generate(int[] spec) {
- int[][] matrix = new int[spec[0]][spec[1]];
- for (int i = 0; i < (spec[0]); i++) {
- for (int j = 0; j < (spec[1]); j++) {
- matrix[i][j] = (int) (Math.random() * 2);
- }
- }
- return matrix;
- }
- public static int[][] correct(int[][] inc) {
- int[][] correct = inc;
- int[] size1 = { inc.length, inc[0].length };
- int[] rowC = new int[inc.length];
- int[] columnC = new int[inc[0].length];
- boolean row = false;
- boolean column = false;
- while (!(row && column)) {
- for (int i = 0; i < rowC.length; i++)
- rowC[i] = 0;
- for (int i = 0; i < columnC.length; i++)
- columnC[i] = 0;
- for (int i = 0; i < inc.length; i++) {
- for (int j = 0; j < inc[0].length; j++) {
- if (inc[i][j] == 1)
- rowC[i]++;
- }
- }
- for (int i = 0; i < inc[0].length; i++) {
- for (int j = 0; j < inc.length; j++) {
- if (inc[j][i] == 1)
- columnC[i]++;
- }
- }
- for (int i = 0; i < inc.length; i++) {
- if (rowC[i] % 2 != 0) {
- row = false;
- break;
- } else
- row = true;
- }
- for (int i = 0; i < inc[0].length; i++) {
- if (columnC[i] % 2 != 0) {
- column = false;
- break;
- } else
- column = true;
- }
- if (!(row))
- inc = generate(size1);
- else if (!(column))
- inc = generate(size1);
- else
- correct = inc;
- }
- return correct;
- }
- public static void print(int[][] p) {
- for (int i = 0; i < p.length; i++) {
- for (int j = 0; j < p[0].length; j++)
- System.out.print(p[i][j]);
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement