Lab: Monte Carlo Estimation


Setup

Follow these steps to set up a project for this lab.

  1. Create a new Eclipse project by copying ProjectTemplate, which you created in lab: right-click on it to get the contextual pop-up menu, choose Copy, right-click somewhere in the Package Explorer outside any existing project to get the contextual pop-up menu, and choose Paste. Name the new project MonteCarlo.
  2. Open the src folder of this project and then open (default package). As a starting point you can use any of the Java files. Right-click on one of the Java files to get the contextual pop-up menu, choose Refactor > Rename..., and rename it MonteCarlo.
  3. Select all the files in (default package) except MonteCarlo.java, then right-click on one of the selected files, choose Delete from the contextual pop-up menu, and confirm that you want to delete these files.
  4. Follow the link to MonteCarlo.java, select all the code on that page (click and hold the left mouse button at the start of the program and drag the mouse to the end of the program) and copy it to the clipboard (right-click the mouse on the selection and choose Copy from the contextual pop-up menu), then come back to this page and continue with these instructions.
  5. Finally in Eclipse, open the MonteCarlo.java file by double-clicking on it in the Package Explorer; select all the code in the editor, right-click on it and select Paste from the contextual pop-up menu to replace the existing code with the code you copied in the previous step. Save your file. As you can see this is the complete program you reviewed for the homework.

Method

  1. Modify MonteCarlo.java so that it generates 2-dimensional points in the [0.0,2.0) x [0.0,2.0) square and estimates the area of the circle of radius 1 centered at (1.0,1.0). (See the slides for this lab for some additional information.)
  2. Run your program with different number of points and discuss the following questions with your partner:

Additional Activities

  1. We assume you've drawn a picture of the geometry of the situation handled by your original program: the [0.0,2.0) x [0.0,2.0) square, and the circle of radius 1 centered at (1.0,1.0) contained in that square. (If you haven't drawn this picture, please accept some friendly advice to get into the habit of drawing pictures of what you're trying to write code to accomplish!) Now, draw a picture of a different situation: the [0.0,2.0) x [0.0,2.0) square, and the quarter of the circle of radius 2 centered at (0.0,0.0) contained in that square.
  2. Copy MonteCarlo.java to create MonteCarlo1.java. Modify the new program so it estimates the area of the quarter of the circle of radius 2 centered at (0.0,0.0) and contained in the [0.0,2.0) x [0.0,2.0) square.
  3. Run the program. Can you explain/justify the outcome?