(3) Image Program oo000.o.O Write a program called ImageProgram that creates a 1
ID: 3878700 • Letter: #
Question
(3) Image Program oo000.o.O Write a program called ImageProgram that creates a 10 x 10 two-dimensional array of 100 0oo. .000.O randomly-generated booleans. The progranm should then display an image that is represented oo.000. .O by the array showing (as a grid of 'O' or'.' characters), where O is shown when the boolean0000. . .000 is true and '' is shown otherwise. See here > Then, your program should determine and print the longest horizontal sequence of O characters 00.0.00o. as well as the longest vertical sequence of O characters by examining the two-dimensional array. Here is some sample output (although The longest horizontal sequence is results will vary each time that you run it.. and the highlighting was only added for emphasis): 000. .0o00 . .O.00..0 o000. .O oo00O.O The longest vertical sequence is 8Explanation / Answer
As per your requirement the below one is solution please follow it
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.