Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use Java code and Strings, the String class (format), arrays, Math.random

ID: 3875410 • Letter: P

Question

Please use Java code and Strings, the String class (format), arrays, Math.random() and Scanner objects to answer, do not use random class and arrayLists class, thank you

(3) Image Program o0000.o.O Write a program called ImageProgram that creates a 10 x 10 two-dimensional array of 100 ooo..o00.o randomly-generated booleans. The program .0o.o0o..O should then display an image that is represented 0o.000.O by the array showing (as a grid of O' or. characters), where O is shown when the boolean 000. .0oo is true and ..' is shown otherwise. See here Then, your program should determine and print | .oooo..o. the longest horizontal sequence of O characters .0.000. as well as the longest vertical sequence of O characters by examining the two-dimensional . .O.0o..o array. Here is some sample output (although The longest horizontal sequence is 5 results will vary each time that you run it. and the highlighting was only added for emphasis): The longest vertical sequence is 8

Explanation / Answer


public class ImageProgram {

   public static void main(String[] args) {

       boolean[][] array = new boolean[10][10];
       String[][] grid = new String[10][10];

       // Calling fill method
       fillBooleanArray(array);

       // Printing the Boolean Array
       printBooleanArray(array);

       // Creating String Grid Using boolean array.
       createStringGridUsingBooleanArray(array, grid);

       printGrid(grid);

       // Horizontal Length
       int rowWiseLength = findLongestHorizontalSequence(grid);

       System.out.println("The Longest Horizontal Sequence is " + rowWiseLength);

       // Vertical Length
       int columnWiseLength = findLongestVerticalSequence(grid);

       System.out.println("The Longest Vertical Sequence is " + columnWiseLength);

   }

   // To generate random value
   public static boolean getRandomBooleanValue() {
       return Math.random() < 0.5;
   }

   public static void fillBooleanArray(boolean[][] array) {
       for (int i = 0; i < 10; i++) {
           for (int j = 0; j < 10; j++) {
               array[i][j] = getRandomBooleanValue();
           }
       }
   }

   public static void printBooleanArray(boolean[][] array) {
       for (int i = 0; i < 10; i++) {
           for (int j = 0; j < 10; j++) {
               System.out.print(array[i][j] + "   ");
           }
           System.out.println();
       }
   }

   public static void createStringGridUsingBooleanArray(boolean[][] array, String[][] grid) {
       for (int i = 0; i < 10; i++) {
           for (int j = 0; j < 10; j++) {
               grid[i][j] = array[i][j] ? "O" : ".";
               // if true value then "O" else "." ,I have used condition operator here ,
               /*
               * it can be re written as if(array[i][j]) { grid[i][j]="O"; } else {
               * grid[i][j]="."; }
               */
           }
       }
   }

   public static void printGrid(String[][] grid) {
       for (int i = 0; i < 10; i++) {
           for (int j = 0; j < 10; j++) {
               System.out.print(grid[i][j] + " ");
           }
           System.out.println();
       }
   }

   public static int findLongestHorizontalSequence(String[][] grid) {
       String str = "O";
       String str1;
       int longestHorizonatlSequenceLength = 0;

       for (int i = 0; i < 10; i++) {
           int consecutive = 0;
           for (int j = 0; j < 10; j++) {
               str1 = grid[i][j];
               if (str1.equals(str))
                   consecutive++;
               else
                   consecutive = 0;

               if (longestHorizonatlSequenceLength < consecutive)
                   longestHorizonatlSequenceLength = consecutive;
           }
       }

       return longestHorizonatlSequenceLength;
   }
   public static int findLongestVerticalSequence(String[][] grid) {
       String str = "O";
       String str1;
       int longestVerticalSequenceLength = 0;

       for (int i = 0; i < 10; i++) {
          
           int consecutive = 0;
           for (int j = 0; j < 10; j++) {
               str1 = grid[j][i];
               if (str1.equals(str))
                   consecutive++;
               else
                   consecutive = 0;

               if (longestVerticalSequenceLength < consecutive)
                   longestVerticalSequenceLength = consecutive;
           }
       }

       return longestVerticalSequenceLength;
   }

}

//Sample OutPut :

false   true   true   false   true   false   false   false   true   true
true   true   true   false   true   true   true   true   true   false
true   false   true   false   false   true   true   true   false   true
true   true   true   false   false   true   false   false   true   false
false   true   true   true   true   false   false   false   false   true
true   true   false   true   false   false   false   false   false   true
false   true   true   false   true   true   false   false   false   true
true   false   false   true   true   true   true   false   true   true
false   false   false   true   true   false   true   false   false   true
true   false   true   false   false   false   true   false   true   true
. O O . O . . . O O
O O O . O O O O O .
O . O . . O O O . O
O O O . . O . . O .
. O O O O . . . . O
O O . O . . . . . O
. O O . O O . . . O
O . . O O O O . O O
. . . O O . O . . O
O . O . . . O . O O
The Longest Horizontal Sequence is 5
The Longest Vertical Sequence is 6