(2) Histogram Program Write a program called HistogramProgram that creates an ar
ID: 3875435 • Letter: #
Question
(2) Histogram Program Write a program called HistogramProgram that creates an array of 100 randomly-generated byte0 values ranging from 0 to 9. The program should1********* then display a histogram showing (as a string of *2 l*************** characters) the number of times that a certain number was generated. The histogram should look like this (although results will vary each time5 l******* that you run it) 4 (3) Image Program O0000.o.O. Write a program called ImageProgram that creates a 10 x 10 two-dimensional array of 100 o0o. .o00.O randomly-generated booleans. The program should then display an image that is representedO0o. 0oo. .o by the array showing (as a grid of 'O' or'.' characters), where O is shown when the boolean ooo. . .O00 is true and ., is shown otherwise. See here Then, your program should determine and print the longest horizontal sequence of O characters OO.O.O0O. . 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):The longest vertical sequence is 8 O0O. .OOOO 0000. .O. .Explanation / Answer
Que1 Create a file named histogramprogram.java and paste given code into it!
histogramprogram.java
import java.io.*;
import java.util.Random;
public class histogramprogram {
public static void main(String[] args)
{
Random randomGenerator = new Random();
int array[] = new int[10];
for(int i = 0;i<10;i++)
{
array[i] = 0;
}
for(int i = 0;i<100;i++)
{
int randomint = randomGenerator.nextInt(10);
array[randomint] += 1 ;
}
for(int i = 0;i<10;i++)
{
System.out.print(i + " |" );
for(int j = 0; j < array[i]; j++)
{
System.out.print("*");
}
System.out.print(" ");
}
}
}
Sample Output:
0 |********
1 |******
2 |***********
3 |****************
4 |***************
5 |****************
6 |*****
7 |**********
8 |*******
9 |******
Que2: Create a file named imageprogram.java and paste given code into it!
imageprogram.java
import java.util.Random;
public class imageprogram {
public static void main(String[] args)
{
Random randomGenerator = new Random();
int[][] matrix =new int[10][10];
for(int i = 0; i<10; i++)
{
for(int j = 0; j<10; j++)
{
int randombool = randomGenerator.nextInt(2);
matrix[i][j] = randombool;
if(randombool == 1)
{
System.out.print("O");
}
else
{
System.out.print(".");
}
}
System.out.println();
}
//Find longest horizontal sequence
int horizontal_max = 0;
for(int i = 0; i<10; i++)
{
int tmp2 = 1;
for(int j = 0; j<9; j++)
{
int tmp1 = 1;
for (int k = j; k<9;k++)
{
if(matrix[i][k] == matrix[i][k+1] & matrix[i][k] == 1)
{
tmp1 = tmp1 + 1;
}
else
{
break;
}
}
tmp2 = Math.max(tmp2,tmp1);
}
horizontal_max = Math.max(horizontal_max,tmp2);
}
System.out.println("The longest horizontal sequence is " + horizontal_max );
//Find longest verticle sequence
int verticle_max = 0;
for(int i = 0; i<10; i++)
{
int tmp2 = 1;
for(int j = 0; j<9; j++)
{
int tmp1 = 1;
for (int k = j; k<9;k++)
{
if(matrix[k][i] == matrix[k+1][i] & matrix[k][i] == 1)
{
tmp1 = tmp1 + 1;
}
else
{
break;
}
}
tmp2 = Math.max(tmp2,tmp1);
}
verticle_max = Math.max(verticle_max,tmp2);
}
System.out.println("The longest verticle sequence is " + verticle_max );
System.out.println();
}
}
Sample Run
.OOO..OO..
.O.OOOO.O.
OOOO...OO.
..OO..O.O.
...OO...OO
O...O.O.OO
O.......O.
OO..OO.OOO
O.OOO...O.
O..O...OOO
The longest horizontal sequence is 4
The longest verticle sequence is 9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.