Variation of the Monte Carlo form of finding Pi... I'm having a bit of trouble

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 205
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Variation of the Monte Carlo form of finding Pi... I'm having a bit of trouble

 
0
  #1
Sep 15th, 2008
Ok, so, for school, we have to write a program that calculates the value of Pi as accurately as possible. Earlier in the year, we worked with a "Turtle" program, it drew lines and circles, ect. it started in the center of the JFrame, and moved to where you told it to move to in the code. I'm using that here. If you've already messed with it (I don't know if you have or not) you can skip this part, it just describes them. basicly, the turtle has 5 commands in this program, sleep, move, paint, swingAround, and switchTo. sleep makes it do nothing for x number of milliseconds, move makes it turn x number of degrees to the left, and y number of pixels forwards, with out leaving a mark looks like this -> (x, y). paint does the same thing, but leaves a trail of color behind it. swingAround makes a circle with a radius of x.

sorry that took so long, if you want to take a look at the classes that you need to run my program, they can be found here:

http://www.cs.ccsu.edu/~jones/Turtlet.java
http://www.cs.ccsu.edu/~jones/Turtle.java

both are required to run the program.

now. what i have is this. The whole thing is commented pretty well, but if you have any questions, please ask. I really need help with this. the thing is, double pi will only equal a whole number, i can't get anything except for 0 in the decimal place...

  1.  
  2. /**
  3.  * Draw two circles. One has an area of Pi (3.1415...)
  4.  * and the other has an area of 4. Place random points
  5.  * in the circles to determin Pi.
  6.  *
  7.  * @author (Neil)
  8.  * @version (9/15/08)
  9.  */
  10.  
  11. import java.util.Random;
  12. import javax.swing.JOptionPane;
  13. import java.lang.String;
  14. import java.util.Scanner;
  15.  
  16. public class PiFinder
  17. {
  18. public static void main(String[] args)
  19. {
  20. Turtle sam = new Turtle(); // Create an object that can draw
  21.  
  22. Random rand1 = new Random(); // Random objects for later, used to
  23. Random rand2 = new Random(); // randomize points in circles.
  24.  
  25. int angle = 0; // Stores the angle that the Turtle rotated
  26. double distance = 0; // Stores the distance traveled by the Turtle
  27.  
  28. int numInside = 0; // Number of points in the inner circle
  29. int numOutside = 0; // Number of points in the outter part of the bigger circle
  30.  
  31. String pointsToDraw = JOptionPane.showInputDialog(null, "How many points would you like to place?");
  32.  
  33. int pointsUser = Integer.parseInt( pointsToDraw ); // The number that the user chaose
  34. int pointsPlaced = numInside + numOutside; // The total number of points placed so far
  35.  
  36. sam.swingAround (100); // Create the circle that has an area of Pi (for this, 100 = 1)
  37. sam.swingAround ((4/3.141592654)*100); // Create the circle with an area of 4 (for this, 400 = 4) (the math is approx. 127.323)
  38.  
  39. while(pointsPlaced != pointsUser)
  40. {
  41. angle = rand1.nextInt(361); // Store the angle turned
  42. distance = rand2.nextInt(127); // Store the distance moved
  43.  
  44. sam.move(angle, distance);
  45.  
  46. if( distance < 100)
  47. {
  48. numInside = numInside+1;
  49. sam.switchTo(Turtle.RED); // Change color to red to distinguish sections
  50. }
  51. else
  52. {
  53. numOutside = numOutside+1;
  54. sam.switchTo(Turtle.BLUE); // Change color to blue to distinguish sections
  55. }
  56.  
  57. if(numOutside>1) // So you don't get: someNumber/0
  58. {
  59. double pi = (numInside/numOutside);
  60.  
  61. System.out.println("In: "+numInside+". " + "Out: " +numOutside+". " +"Pi equals: " +pi);
  62. }
  63.  
  64. sam.paint(0,0); // Paint a dot in current location
  65. sam.move(180, distance); // Turn around, move to starting position
  66. sam.move(-1*angle+180, 0); // Turn to face left
  67. sam.sleep(10); // Pause for 10 milliseconds
  68.  
  69. pointsPlaced = numInside + numOutside; // The total number of points placed so far
  70. }
  71.  
  72. }
  73. }
Last edited by llemes4011; Sep 15th, 2008 at 11:37 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC