1 Attachment(s)

Would appreciate help (Beginner programmer)

Hello everyone,

Im new to java programming and especially new to this forum. Im taking my first college computer science course. I did very well on my first exam (loops, methods, keyboard input, types, etc). However, this question seriously has me stumped. How do I make the quarter circle/square? I attached a picture of the question.

Heres what I know:

- I know I will need a loop

- Need to declare Random integer- how do I do this?

- I have no idea where to start

Code for program:

Code :

import java.util.Random;
class Dogs
{
public static void main (String[] args)
{
Random integer r = new Random // Completely wrong probably.

Any help or suggestions will be greatly appreciated. Thank you in advance.

Re: Would appreciate help (Beginner programmer)

Not random integer, random double as specified by the problem. Read carefully. To accomplish, you can use the Math.random() method or a method of the Random class. Review the APIs for both and decide which you prefer for this assignment.

Re: Would appreciate help (Beginner programmer)

Quote:

How do I make the quarter circle/square?

Not sure what you are asking. Are you trying to draw an arc inside a square where the center of the circle is at one of the corners and the radius of the circle equals the length of a side of the square?

Re: Would appreciate help (Beginner programmer)

I agree the directions are a bit confusing. You really don't make a geometric shape of any kind, though it would be helpful to your understanding if you drew the dartboard and the quarter circle as described.

The coordinates randomly generated are used to describe the dart's location on the dartboard with (0, 0) specified as the lower left corner of the dartboard, x increases across the dartboard, and y increases UP the dartboard, therefore for (x, y):

(0, 0) is lower left corner of the dartboard

(1, 0) is the lower right corner of the dartboard

(0, 1) is the upper left corner of the dartboard, and

(1, 1) is the upper right corner of the dartboard.

Throws inside the described quarter circle with a radius <= 1 are "hits." Throws outside are "misses." To determine the radius of each throw (the distance from the origin), use the formula

r = sqrt( x^2 + y^2 )

Let us know if you have questions.

Re: Would appreciate help (Beginner programmer)

Thanks for the help guys. It's coming along... slowly but surely :cool:

Code :

import java.util.Random;
class Dogs
{
public static void main (String[] args)
{
Random r = new Random();
int c = 1;
while (c <= 1000000)
{
double x = r.nextDouble();
System.out.println(x);
double y = r.nextDouble();
System.out.println(y);
if (x*x + y*y <= 1)
c++;
}
double p = Math.PI;
System.out.println(p);
}
}

Re: Would appreciate help (Beginner programmer)

Please learn to post your code in code tags. Read the Announcement topic at the top of the sub-forum.

Yes, you're making progress. Compare your if statement to the equation I gave earlier.