CS 100: Programming Assignment P1
Solution
// P1A
// Draws a logarithmic spiral with variable color..
import java.io.*;
import java.awt.*;
public class P1A {
public static void main(String args[]) throws IOException {
Drawing d = new Drawing();
d.resize(1000,800);
d.move(0,75);
d.setTitle("Logarithmic Spiral");
d.show();
}
}
public class Drawing extends Frame {
final int hc = 500;
final int vc = 350; // (hc,vc) = center of the spiral
final int n = 1000; // The total number of "legs" in the spiral.
final int turn = 150; // Turn factor. Each leg turns "left" this amount in degrees
final double d = .5; // The length of the kth leg is k*d
public void paint(Graphics g) {
int h, v; // The horizontal and vertical coordinates of the current vertex.
int h_next, v_next; // The horizontal and vertical coordinates of the next vertex.
double theta; // The "angle" of the current leg.
int k; // The index of the current leg.
double L; // The length of the current leg.
h = hc; v = vc;
for (k=1;k<=n;k++) { // Determine the color of the kth leg. if (k%3="=0)" g.setColor(Color.magenta); else if (k%3="=1)" g.setColor(Color.cyan); else g.setColor(Color.green); // Determine the endpoints and draw. theta="(k*turn)*Math.PI/180;" L="k*d;" h_next="(int)" Math.round(h + L*Math.cos(theta)); v_next="(int)" Math.round(v + L*Math.sin(theta)); g.drawLine(h,v,h_next,v_next); h="h_next;" v="v_next;" } } } // P1B // Estimates pi as a limit of polygon areas. import java.io.*; public class P1B { public static void main( String args[]) { int n; // The number of sides in the approximating n-gons. double OuterA, InnerA; // The areas of the circumscibed and inscribed n-gons. double Error; // OuterA InnerA double c, s; // The cosine and sine of pi/n. n="4;" OuterA="4;" InnerA="2;" c="1.0/Math.sqrt(2.0);" System.out.println("\n n A(n) B(n) B(n)-A(n)"); System.out.println("-----------------------------------------------------------------"); while (OuterA InnerA> 0.00000001) {
n = 2*n;
s = Math.sqrt((1.0 - c) / 2.0);
c = Math.sqrt((1.0 + c) / 2.0);
InnerA = n*s*c;
OuterA = n*s/c;
Error = OuterA - InnerA;
//System.out.println(n + " " + InnerA + " " + OuterA + " " + Error);
Format.print(System.out,"%5d ",n);
Format.print(System.out," %15.9f ",InnerA);
Format.print(System.out," %15.9f ",OuterA);
Format.println(System.out," %6.2e ",Error);
}
System.out.println();
}
}
// P1B
// Estimates pi as a limit of polygon areas.
import java.io.*;
public class P1B {
public static void main( String args[]) {
int n; // The number of sides in the approximating n-gons.
double OuterA, InnerA; // The areas of the circumscibed and inscribed n-gons.
double Error; // OuterA - InnerA
double c, s; // The cosine and sine of pi/n.
n = 4;
OuterA = 4;
InnerA = 2;
c = 1.0/Math.sqrt(2.0);
System.out.println("\n n A(n) B(n) B(n)-A(n)");
System.out.println("-----------------------------------------------------------------");
while (OuterA - InnerA > 0.00000001) {
n = 2*n;
s = Math.sqrt((1.0 - c) / 2.0);
c = Math.sqrt((1.0 + c) / 2.0);
InnerA = n*s*c;
OuterA = n*s/c;
Error = OuterA - InnerA;
//System.out.println(n + " " + InnerA + " " + OuterA + " " + Error);
Format.print(System.out,"%5d ",n);
Format.print(System.out," %15.9f ",InnerA);
Format.print(System.out," %15.9f ",OuterA);
Format.println(System.out," %6.2e ",Error);
}
System.out.println();
}
}
// P1C
// Estimates pi via Monte Carlo.
import java.io.*;
import java.awt.*;
public class P1C {
public static void main(String args[]) {
Drawing d = new Drawing();
d.resize(600,500);
d.move(0,75);
d.setTitle("P1C: The Dart Board");
d.show();
}
}
public class Drawing extends Frame {
final int hc = 300;
final int vc = 250;
// (hc,vc) = center of circle
final int r = 200; // the radius
public void paint(Graphics g) {
int Left = hc-r; // Left edge of square
int Top = vc-r; // Top edge of square
long k; // Dart throw index
long hits; // Number of darts that land in circle.
long n; // Total number of darts thrown.
double x,y; // (x,y) = coordinates of current dart throw
double dist; // Distance of current dart throw to center.
double MyPi;
// Draw the target.
g.drawOval(hc-r,vc-r,2*r,2*r);
g.drawRect(hc-r,vc-r,2*r,2*r);
// --------------- Comment Line A ----------------------
n = 100;
for (k=1;k<=n;k++){
// Generate the next dart throw and plot.
x = Left + 2*r*Math.random();
y = Top + 2*r*Math.random();
g.drawOval((int)x,(int)y,1,1);
}
// --------------- Comment Line B ----------------------
hits = 0;
n = 100000;
for (k=1;k<=n;k++){
// Generate the next dart throw and plot.
x = Left + 2*r*Math.random();
y = Top + 2*r*Math.random();
// Check to see if the throw lands inside the circle and tabulate.
dist = Math.sqrt((x-hc)*(x-hc) + (y-vc)*(y-vc));
if (dist<=r)
hits++;
}
MyPi = ((double) hits/n)*4;
Format.println(System.out,"Number of dart throws = %6d\n",n);
Format.println(System.out,"Appx Pi = %20.15f",MyPi);
Format.println(System.out,"True Pi = %20.15f",Math.PI);
System.out.println();
// --------------- Comment Line C ----------------------
}
}