Lab: Monte Carlo Estimation
Setup
Follow these steps to set up a project for this lab.
- 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.
- 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.
- 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.
- 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.
- 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
- 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.)
- Run your program with different number of points and
discuss the following questions with your partner:
- What is the area of a circle of radius 1?
- How well does your program estimate it?
- How is the estimate affected by the number of points
generated?
Additional Activities
- 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.
- 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.
- Run the program. Can you explain/justify the outcome?