# Thread: Basic Java Programming Problems

1. First Cup of Ubuntu
Join Date
May 2011
Beans
8

## Basic Java Programming Problems

Hi everyone,

I'm stuck on arrays in a program I'm trying to create to measure the area of an annulus. Can someone help me? I can't figure out how to loop my first array to repeat 100 times, and I think my 2D array is also looking dodgy.

Here is my pseudocode to show what I'm supposed to be doing, and my actual program is beneath it. Sorry it's so long!!!! :

BEGIN Area of Annulus
Set gridSize = 20 (we could use 20 by 20 grid cells)
Create a 2D array, hits, to count how many points are on the shape
Set samples = 100 (let’s use 100 random points inside each cell)
Get r1 from user (outer radius)
Get r2 from user (inner radius)
Set minx = -r1
Set maxx = r1
Set miny = -r1
Set maxy = r1
Set counter = 0 (will count how many grid cells are on the annulus)
For column = 0 to gridSize-1
For row = 0 to gridSize-1
Set hits[column][row] to 0
Repeat samples times
Set x = minx + (column + random)*((maxx–minx)/gridSize)
Set y = miny + (row + random)*((maxy–miny)/gridSize)
Set test = x*x + y*y
if test < r1*r1 and test > r2*r2
Set hits[column][row] = hits[column][row] + 1
Set hits[column][row] = hits[column][row]/samples
Set counter = counter + hits[column][row]
Set area = (maxx-minx)*(maxy-miny)*counter/(gridSize * gridSize)
Output “Area : ” area
END Area of Annulus

import java.util.Scanner;

public class AreaOfAnnulus
{
public static void main (String args[])

{

// set grid size
int gridSize = 20;

//declare a new array
final int samples = 100;
int []numbers= new int [samples];

//create Scanner object
Scanner keyboard = new Scanner (System.in);

double r1 = keyboard.nextDouble();

double r2 = keyboard.nextDouble();

//set column = x

//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 <samples.length(); column++) { I'm getting an 'int can not be dereferenced eroor on these lines!!
for (int row = 0; row <samples.length(); row++){

//set 2D array

int [][] hits = new int [column][row];

hits [column][row] = hits [0][0];

//set inner nested loop

double y = miny +(row + Math.random())*((maxy-miny)/gridSize);

double x = minx +(column +Math.random())*((maxx-minx)/gridSize);

//set x and y

//set test

double test = x*x + y*y;

if(test < r1*r1 && test > r2*r2)
{

hits [0][0] = hits [0][0]+1;

hits [0][0] = hits [0][0]/samples;

counter = counter + hits [0][0];
}
//set area
}
}
double area = (maxx-minx)*(maxy-miny)*counter/(gridSize*gridSize);

System.out.println ("Area: " + area);

{
System.out.println ("What is the outer radius of the annulus?");

System.out.println ("What is the inner radius of the annulus?");

//calculate the area based on the measurements provided

System.out.println ("The area of the annulus is" + area);
}
}
}

2. Iced Almond Soy Ubuntu, No Foam
Join Date
Nov 2009
Beans
1,081

## Re: Basic Java Programming Problems

2. If you pay attention the actual error message, it should be clear what's wrong.

An 'int' cannot be dereferenced, because it's not a reference; it's a primitive. 'samples' is an int. It's not even clear WHY you're trying to dereference it, instead of simply treating it like an int; e.g. "column < samples". What is the length of an int?

3. First Cup of Ubuntu
Join Date
May 2011
Beans
8

## Re: Basic Java Programming Problems

1. I DON'T KNOW WHAT CODE TAGS ARE.

2. Yeah I had actually figured that bit out, but it's not solving the issue of how to get the pseudocode to translate into programmable code.

Not even remotely helpful: I'm not a programmer, I don't like programming, and I'm forced to teach myself because I'm learning externally and it's a compulsory subject.

4. First Cup of Ubuntu
Join Date
May 2011
Beans
8

## Re: Basic Java Programming Problems

sorry; my tutor just came back to me with something that sounded like Yoda talking; it's due tomorrow and I got stressed.

Almost done. Thanks for your help

5. ## Re: Basic Java Programming Problems

1. I DON'T KNOW WHAT CODE TAGS ARE.
Code tags allow you to put code into a post and retain the formatting. It makes your code snippets much easier to read and you are more likely to get help.

You can:

1. Enclose your code with tags like these (no spaces inside the tags): [code] and [ /code]

or

2. When you reply or post, select your code with the mouse and press the shortcut button '#' in the toolbar that appears above it.

For example:

Code:
```int main(int argc, char **argv)
{
printf("hello, world");
}```
as opposed to:

int main(int argc, char **argv)
{
printf("hello, world");
}

6. 5 Cups of Ubuntu
Join Date
Feb 2010
Beans
24

## Re: Basic Java Programming Problems

samples is neither an array nor an object
Last edited by dr_saaron; May 14th, 2011 at 02:10 PM.