Lab | Assigned | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Design Due (In class) 10 AM |
Implement (In Lab) |
|||||||||
Demo (In Lab) |
||||||||||
Lab Due (In class) Friday 10 AM |
||||||||||
15 | Feb | 18 | Feb | 22-33 | Feb | 29-30 | Feb | 03 | Mar |
PI.java
Coordinates within this class are with respect to the origin of the circle,
at (0,0). Thus, the square goes from (-S/2, -S/2) to (S/2, S/2).
Translation from my coordinates to Java's is handled by the private
method drawLine
, as explained below.
PI(int S, int n)
S
. The approximation uses (approximately)
n
line segments. No canvas is provided as a parameter so
this class has to make its own. After the circle is drawn, a square of
size SxS is drawn, so that the circle sits just inside the square.
int numThrown()
int numLandedIn()
int numLandedOut()
Representation invariant:
numThrown() = numLandedIn() + numLandedOut()
From outside this class, we can compute an approximate to PI by
PI pi = new PI(100, 5); pi.hurlDarts(1000); double approx = 4.0 * pi.numLandedIn() / pi.numThrown();
void hurlDarts(int m)
m
(additional) darts at the circle.
You must do this part iteratively.
Each dart is shown with a dot on the screen.
private void cirrecur(int oldX, int inc)
private int cirfunc(int x)
Y
coordinate for the supplied value of x
.
private boolean insideCircle(int x, int y)
true
if the coordinates supplied
are inside the circle; otherwise, false
is returned.
private hurlDart()
private void drawLine(myX1, myY1, myX2, myY2)
(parameters are all int
).
PI
.
Startup
to invoke your
PI
constructor.