/** LAB2 ==== Part 1. COMPUTATION OF Pi. Three iterative algorithms to compute the value of PI Math.PI value of PI = 3.141592653589793 */ /* CONVERGENCE for piGregory() - "add" = difference between the upper and lower bounds. pi = 3.017071817071818, add = 0.07692307692307693, i= 6 pi = 3.12236366153074, add = 0.009900990099009901, i= 50 pi = 3.139600623693466, add = 9.99000999000999E-4, i= 500 pi = 3.1413927335598015, add = 9.999000099990002E-5, i= 5000 pi = 3.141572654389749, add = 9.99990000099999E-6, i= 50000 pi = 3.1415906535976923, add = 9.99999000001E-7, i= 500000 pi = 3.1415924535898596, add = 9.9999990000001E-8, i= 5000000 pi = 3.1415926335902515, add = 9.9999999E-9, i= 50000000 CONVERGENCE of piAnon: ("add" = difference from previous iteration) pi = 3.1666666666666665; add = 0.041666666666666664; i= 2 pi = 3.1333333333333333; add = 0.008333333333333333; i= 4 pi = 3.1427128427128426; add = 7.575757575757576E-4; i= 10 pi = 3.1417360992606653; add = 8.234519104084322E-5; i= 22 pi = 3.1416106990404735; add = 9.63613937711995E-6; i= 46 pi = 3.1415907698497954; add = 9.706853038245E-7; i= 100 pi = 3.1415924605642283; add = 9.786559061492474E-8; i= 216 pi = 3.1415926338263667; add = 9.945878507513514E-9; i= 464 pi = 3.1415926516017554; add = 9.970069850309371E-10; i= 1000 NOTE: 1/x operation is unstable for i >= 1290 pi = 3.1415926507918335; add = -4.665738523122133E-10; i= 1290 CONVERGENCE of piWallis: ("add" = difference from previous iteration) pi = 2.8444444444444446; add = 0.08888889; i= 4 pi = 3.038673628883418; add = 0.0077517186; i= 14 pi = 3.1035169615392304; add = 9.6984906E-4; i= 40 pi = 3.1292486213430553; add = 9.85528E-5; i= 126 pi = 3.137658290464043; add = 9.903974E-6; i= 398 pi = 3.1403412724785795; add = 9.985085E-7; i= 1254 pi = 3.1411965130417427; add = 9.995345E-8; i= 3964 pi = 3.1414673432588804; add = 9.998232E-9; i= 12534 pi = 3.141553022293614; add = 9.99951E-10; i= 39634 pi = 3.1415801206317506; add = 9.9998676E-11; i= 125332 pi = 3.1415886902872745; add = 9.999779E-12; i= 396334 pi = 3.1415914001359817; add = 9.998669E-13; i= 1253174 pi = 3.141592256639885; add = 9.992007E-14; i= 3957174 SUMMARY: For piGregory and piAnon, which are additive, converge to EPSILON when the additive term is of the order of EPSILON. However piWallis, which is multiplicative, converges when the number of terms is of the order (1/EPSILON). */ public class ComputePi { public static void main (String args[]) { final double EPSILON = 1E-6; boolean DEBUG = false; double pi = Math.PI; System.out.println("Math.PI: value of PI is " + pi); pi = piGregory(EPSILON, DEBUG); System.out.println("Gregory: value of PI is " + pi); pi = piAnon(EPSILON, DEBUG); System.out.println("Anon: value of PI is " + pi); pi = piWallis(EPSILON, DEBUG); System.out.println("Wallis: value of PI is " + pi); } /** Algorithm for computing PI - by James Gregory and also by Leibniz. Oscillates between an upper and a lower bound. Poor convergence - 500,000 terms for six digit accuracy. @author = Amitabha Mukerjee @version = Aug 13, 2002 */ public static double piGregory (double epsilon, boolean debug) { double add = 1.0; double pi = 0.0; double eps = 0.1; double upper, lower, minus; for (int i=0; add > epsilon; i += 2) { add = 1.0/ (2*i+1); upper = pi += add; minus = 1.0 / (2*i+3); lower = pi -= minus; if (debug && add < eps) { System.out.println("pi = " + 4.0*pi + ", add = " + add + ", i= " + i ); eps *= 0.1; } } pi *= 4.0; return (pi); } /** Algorithm for computing PI (anonymous?). Good convergence. 100 terms gives six digit accuracy, but the computation becomes unstable (due to finite storage in the double) after 644 terms (i=1288). */ public static double piAnon (double epsilon, boolean debug) { double add = 1.0; int sign = 1; double pi = 0.0; double eps=0.1; for (int i=2; add > epsilon ; i += 2) { add = ( 1.0/ ( i*(i+1)*(i+2)) ); pi += add * sign; sign = sign * (-1); if (debug && add < eps) { System.out.println("pi = " + (4.0*pi+3) + "; add = " + add + "; i= " + i ); eps *= 0.1; } } pi = pi*4.0 + 3.0; return (pi); } /** Algorithm for computing PI - by Jon Wallis. An infinite product iteration that oscillates about the value of pi. Very slow, non-linear convergence - approx. a million terms for six significant digits. */ public static double piWallis (double epsilon, boolean debug) { double mult = 1.0; double pi = 1.0; double old = 0.0; double eps=0.1; double add=1.0; double upper, lower, minus; for (double i=2; i < 1.0/epsilon; i += 2) // the number of terms - 1/epsilon - is approximate. // any better estimates? { old = pi; mult = i / (i-1.0) ; upper = pi *= mult; mult = i / (i+1.0) ; lower = pi *= mult; add = pi - old; if (debug && i > 1/eps) { System.out.println("pi = " + 2.0*pi + "; add = " + (float)add + "; i= " + (int)i ); eps *= 0.1; } } pi = pi*2.0; return (pi); } } /***********************************************************************************************/ /* Part 2. CONSTRUCTORS. The "Abstract Windowing Toolkit (AWT)" is a JAVA package that includes a number of tools for graphical interfaces. In blueJ, check out the java.awt.Rectangle class (In the "Tools" menu, go to "Use Library Class" and type in the name of the class). Write a program that imports the Rectangle class from the java.awt package: import java.awt.Rectangle; and uses the Rectangle(int,int,int,int) constructor to create a rectangle object rectangleA, and translates it to create a second object rectangleB, prints them, and then prints their intersection. Test the situations when 1. The rectangles touch at a line 2. The rectangles touch at a corner, and 3. When they don't intersect at all. In lab 4 we shall extend this lab to incorporate graphics. */ import java.awt.*; public class Lab2Rectangle { Rectangle r1,r2; public void MyRectangle_r1(int a, int b, int c, int d) { r1 = new Rectangle(a,b,c,d); r2 = new Rectangle(r1); } public void Translate_r1_into_r2(int a, int b) { if(r1==null) { System.out.println("To translate, first create a rectangle using the MyRectangle_r1" + "(" + "int,int,int,int" + ")" + " method"); return; } r2.setBounds(r1); r2.translate(a,b); } public boolean Intersection_r1_r2() { if(r1==null) { System.out.println("To check intersection, first create a rectangle using the MyRectangle_r1" + "(" + "int,int,int,int" + ")" + " method"); return false; } return r1.intersects(r2); } public void Show_r1() { if(r1==null) { System.out.println("To print the coordinates, first create a rectangle using the MyRectangle_1" + "(" + "int,int,int,int" + ")" + " method"); return ; } System.out.println("Rectangle r1......."); System.out.println("Top left Coordinate : (" + r1.x + "," + r1.y + ")"); System.out.println("Height : " + r1.height); System.out.println("Width : " + r1.width); } public void Show_r2() { if(r2==null) { System.out.println("To print the coordinates, first create a rectangle using the MyRectangle_1" + "(" + "int,int,int,int" + ")" + " method"); return ; } System.out.println("Rectangle r2......."); System.out.println("Top left Coordinate : (" + r2.x + "," + r2.y + ")"); System.out.println("Height : " + r2.height); System.out.println("Width : " + r2.width); } }