subject

Language: Java I'd like for my code to print "O" 30% of the time, but it prints "O" 100% of the time. How can I fix that?

Code:

public class PetriDish {
private boolean[][] grid;

public PetriDish()
{
// Formula: (int)(Math. random() * (max - min + 1) + min);
int row = (int)(Math. random() * (30 - 5 + 1) + 5);
int column = (int)(Math. random() * (30 - 5 + 1) + 5);
boolean[][] grid2 = new boolean [row][column];
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{

grid2[i][j] = Math. random() < 0.30;
}
}
grid = grid2;
}

public void showGrid()
{
for(int row = 0; row < grid. length; row++)
{
for(int column = 0; column < grid[0].length; column++)
{
if (grid[row][column] = true)
System. out. print("O"); //true state
else
System. out. print("."); //False State
}
System. out. println();
}

}

public int numNeighbors(int row, int col)
{
int count = 0;
for(int i = row - 1; i <= row + 1 ; i++)
{
for(int j = col - 1; j <= col + 1; j++)
{
if ((i >= 0) && (i < grid. length))
{
if ((j >= 0) && (j < grid. length))
{
if ((i != row ) && (j != col))
{
if (grid[i][j] = true)
count++;

}
}
}
}
}
return count;
}

public static void main(String args[])
{
PetriDish p = new PetriDish();
p. showGrid();
/*Change the (4,4) to be a random valid value within the grid*/
System. out. println(p. numNeighbors(0,0));
}
}

​

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 18:30
Kto rozmawia z clamentain przez krótkofalówke w the walking dead która śledzi lee w 4 epizodzie
Answers: 1
question
Computers and Technology, 23.06.2019 13:30
Select the correct answer from each drop-down menu. which types of computer networks are bigger as well as smaller than a man? a man is a network of computers that covers an area bigger than a , but smaller than a .
Answers: 1
question
Computers and Technology, 23.06.2019 18:00
File account.java (see previous exercise) contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and return a string representation. note that the constructor for this class creates a random account number. save this class to your directory and study it to see how it works. then write the following additional code: 1. suppose the bank wants to keep track of how many accounts exist. a. declare a private static integer variable numaccounts to hold this value. like all instance and static variables, it will be initialized (to 0, since it’s an int) automatically. b. add code to the constructor to increment this variable every time an account is created. c. add a static method getnumaccounts that returns the total number of accounts. think about why this method should be static - its information is not related to any particular account. d. file testaccounts1.java contains a simple program that creates the specified number of bank accounts then uses the getnumaccounts method to find how many accounts were created. save it to your directory, then use it to test your modified account class.
Answers: 3
question
Computers and Technology, 23.06.2019 22:30
The output voltage of a power supply is assumed to be normally distributed. sixteen observations are taken at random on voltage are as follows: 10.35, 9.30, 10.00, 9.96, 11.65, 12.00, 11.25, 9.58, 11.54, 9.95, 10.28, 8.37, 10.44, 9.25, 9.38, and 10.85
Answers: 1
You know the right answer?
Language: Java I'd like for my code to print "O" 30% of the time, but it prints "O" 100% of the tim...
Questions
question
Mathematics, 18.03.2021 02:30
question
Mathematics, 18.03.2021 02:30
question
English, 18.03.2021 02:30
Questions on the website: 13722360