| | |
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
![]() |
•
•
Join Date: Aug 2008
Posts: 205
Reputation:
Solved Threads: 13
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...
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...
JAVA Syntax (Toggle Plain Text)
/** * Draw two circles. One has an area of Pi (3.1415...) * and the other has an area of 4. Place random points * in the circles to determin Pi. * * @author (Neil) * @version (9/15/08) */ import java.util.Random; import javax.swing.JOptionPane; import java.lang.String; import java.util.Scanner; public class PiFinder { public static void main(String[] args) { Turtle sam = new Turtle(); // Create an object that can draw Random rand1 = new Random(); // Random objects for later, used to Random rand2 = new Random(); // randomize points in circles. int angle = 0; // Stores the angle that the Turtle rotated double distance = 0; // Stores the distance traveled by the Turtle int numInside = 0; // Number of points in the inner circle int numOutside = 0; // Number of points in the outter part of the bigger circle String pointsToDraw = JOptionPane.showInputDialog(null, "How many points would you like to place?"); int pointsUser = Integer.parseInt( pointsToDraw ); // The number that the user chaose int pointsPlaced = numInside + numOutside; // The total number of points placed so far sam.swingAround (100); // Create the circle that has an area of Pi (for this, 100 = 1) sam.swingAround ((4/3.141592654)*100); // Create the circle with an area of 4 (for this, 400 = 4) (the math is approx. 127.323) while(pointsPlaced != pointsUser) { angle = rand1.nextInt(361); // Store the angle turned distance = rand2.nextInt(127); // Store the distance moved sam.move(angle, distance); if( distance < 100) { numInside = numInside+1; sam.switchTo(Turtle.RED); // Change color to red to distinguish sections } else { numOutside = numOutside+1; sam.switchTo(Turtle.BLUE); // Change color to blue to distinguish sections } if(numOutside>1) // So you don't get: someNumber/0 { double pi = (numInside/numOutside); System.out.println("In: "+numInside+". " + "Out: " +numOutside+". " +"Pi equals: " +pi); } sam.paint(0,0); // Paint a dot in current location sam.move(180, distance); // Turn around, move to starting position sam.move(-1*angle+180, 0); // Turn to face left sam.sleep(10); // Pause for 10 milliseconds pointsPlaced = numInside + numOutside; // The total number of points placed so far } } }
Last edited by llemes4011; Sep 15th, 2008 at 11:37 pm.
![]() |
Other Threads in the Java Forum
- Previous Thread: Having problems rotating Shapes in JPanel
- Next Thread: Need Help on calling classes
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working





