Troubleshoot Help: Darts Hit or Miss using a (for) loop.
Hello, I have an assignment for my AP Computer Science class that is giving me some troubles. I have tried to compile the program, but I have a few pesky bugs that I can't seem to get rid of. I will include the code as well as the location and nature of my error messages. The basic idea of the program is to have darts thrown within a square with a side length of 2. Darts which fall within a circle of radius 1 count as a "hit", and darts outside of this circle count as a "miss". I decided to simplify the program by only having darts thrown in the first quadrant. I am not so much concerned with the results I get from the program as I am with just getting it to compile. Please leave comments with any suggestions you have or if you find any mistakes.
Thank you in advance,
achamb
public class Pi
{
class Pi(double r)
{
double radius= r;
}
public void tossDart()
{
int hit= 0;
int miss= 0;
for (i=1; i<=1000; i++)
{
double x= Math.random();
double y= Math.random();
double t= Math.sqrt(Math.power(x,2)+Math.power(y,2));
if (t<=radius)
{
hit++;
}
else
{
miss++;
}
}
}
public void getData()
{
System.out.println("The number of hits: "+hit);
System.out.println("The number of misses: "+miss);
}
private int hit;
private int miss;
private double radius;
private double x;
private double y;
private double t;
}
Errors:
Pi.java:4: '{' expected
class Pi(double r)
^
Pi.java:4: ';' expected
class Pi(double r)
^
Pi.java:44: reached end of file while parsing
}
^
3 errors
Re: Troubleshoot Help: Darts Hit or Miss using a (for) loop.
Look up your book or tutorial on how to create constructors. They don't start with the key-word "class". That's only used at the start of the class itself (as you've done at the very top of your program code.
Re: Troubleshoot Help: Darts Hit or Miss using a (for) loop.
You were correct in your above suggestion. Thanks!