hadenkl
May 16th, 2011, 10:41 AM
I keep getting an ArrayIsOutOfBoundsException error on this code below. I can't figure out how to fix it. Could someone tell me what needs to be done to sort it out? It's driving me crazy! I'm not very good at arrays........
import java.util.Scanner;
public class Shape
{
public static void main (String args[])
{
// set grid size
int gridSize = 20;
//create a 2D array called hits
final int COLS = 20;
final int ROWS = 20;
double hits [][] = new double[COLS][ROWS];
//set samples
final int samples = 100;
//create Scanner object
Scanner keyboard = new Scanner (System.in);
//ask user for outer radius
System.out.println ("Please enter the outer radius of the annulus");
double r1 = keyboard.nextDouble();
//Ask user for the inner radius
System.out.println ("Please enter the inner radius of the annulus");
double r2 = keyboard.nextDouble();
//set minx = 1r1
double minx = -r1;
//set maxx = r1
double maxx = r1;
//set miny -= -r1
double miny = -r1;
//set maxy = r1
double maxy = r1;
//set counter = 0
double counter = 0;
//set column and row for loops
for (int column = 0; column < gridSize-1; column++) {
double x = minx + (column + Math.random()) * ((maxx-minx)/gridSize);
for (int row = 0; row < gridSize-1; row++){
double y = miny + (row + Math.random()) * ((maxy-miny)/gridSize);
//set hits to 0
for (int i= 0; i<COLS; i++){
for (int j= 0; j<ROWS; j++){
//set test
double test = x*x + y*y;
if (test < r1*r1 && test > r2*r2)
{
//set hits = hits +1
hits [COLS][ROWS] = hits [i][j] +1;
}
//set hits = hits/samples
hits [COLS][ROWS] = hits [COLS][ROWS] /samples; error message shows here
//set counter = counter +hits
counter = counter + hits [COLS][ROWS]; or here
}
}
}
}
//set area
double area = (maxx-minx)*(maxy-miny)*counter/(gridSize*gridSize);
//output area
System.out.println ("Area: " + area);
}
}
import java.util.Scanner;
public class Shape
{
public static void main (String args[])
{
// set grid size
int gridSize = 20;
//create a 2D array called hits
final int COLS = 20;
final int ROWS = 20;
double hits [][] = new double[COLS][ROWS];
//set samples
final int samples = 100;
//create Scanner object
Scanner keyboard = new Scanner (System.in);
//ask user for outer radius
System.out.println ("Please enter the outer radius of the annulus");
double r1 = keyboard.nextDouble();
//Ask user for the inner radius
System.out.println ("Please enter the inner radius of the annulus");
double r2 = keyboard.nextDouble();
//set minx = 1r1
double minx = -r1;
//set maxx = r1
double maxx = r1;
//set miny -= -r1
double miny = -r1;
//set maxy = r1
double maxy = r1;
//set counter = 0
double counter = 0;
//set column and row for loops
for (int column = 0; column < gridSize-1; column++) {
double x = minx + (column + Math.random()) * ((maxx-minx)/gridSize);
for (int row = 0; row < gridSize-1; row++){
double y = miny + (row + Math.random()) * ((maxy-miny)/gridSize);
//set hits to 0
for (int i= 0; i<COLS; i++){
for (int j= 0; j<ROWS; j++){
//set test
double test = x*x + y*y;
if (test < r1*r1 && test > r2*r2)
{
//set hits = hits +1
hits [COLS][ROWS] = hits [i][j] +1;
}
//set hits = hits/samples
hits [COLS][ROWS] = hits [COLS][ROWS] /samples; error message shows here
//set counter = counter +hits
counter = counter + hits [COLS][ROWS]; or here
}
}
}
}
//set area
double area = (maxx-minx)*(maxy-miny)*counter/(gridSize*gridSize);
//output area
System.out.println ("Area: " + area);
}
}