Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Troubleshoot Help: Darts Hit or Miss using a (for) loop.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Location
    MN, USA
    Posts
    4
    My Mood
    Cynical
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question 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


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    achamb (October 22nd, 2012)

  4. #3
    Junior Member
    Join Date
    Oct 2012
    Location
    MN, USA
    Posts
    4
    My Mood
    Cynical
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Troubleshoot Help: Darts Hit or Miss using a (for) loop.

    You were correct in your above suggestion. Thanks!

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. Help me to troubleshoot the errors
    By Siddhantnaik in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2012, 03:47 PM
  3. Game of Life Matrix...Needs troubleshoot, specific output
    By Eriosblood in forum Collections and Generics
    Replies: 12
    Last Post: September 11th, 2012, 10:59 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. Java coding for writing telephone bill calculator program
    By eyeore in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 8th, 2009, 10:06 AM

Tags for this Thread