View Question
 Question
 Subject: Java Programming Category: Computers > Programming Asked by: strongbow69-ga List Price: \$15.00 Posted: 13 Nov 2002 04:41 PST Expires: 13 Dec 2002 04:41 PST Question ID: 106752
 ```[1] Design, implement and test a class hierarchy of which has a super-class Pythagoras and two sub-classes (Vieta and Wallis), which can be used to produce estimates of PI, using either the method proposed by Franciscus Vieta or that of John Wallis. You should allow for a "number of terms" value to be supplied by the user. [2] Then design and implement an application class that uses the classes in the hierarchy to produce estimates for PI using the two approaches outlined above. Over the centuries many mathematicians have proposed progressions for estimating PI. Including: (1) Francois Vieta (1540-1603), a French mathematician who proposed the sequence: 2 sqrt(2) sqrt(2+sqrt(2)) sqrt(sqrt(2+sqrt(2))) sqrt(sqrt(sqrt(2+sqrt(2)))) ---- = --------- X ----------------- X ----------------------- X ----------------------------- X .... PI 2 2 2 2 After only a few terms this gives a good approximation, for example 20 terms gives the estimate 3.1415926535886185, which is very close to the estimation we use today (3.141592653589793). (2) John Wallis (1616-1703), an English Mathematician , who proposed: PI 2 2 4 4 6 6 8 8 10 10 ---- = --- X --- X --- X --- X --- X --- X --- X --- X ---- X ---- X .... 2 1 3 3 5 5 7 7 9 9 11 (Note that the dividend and divisor are alternatively incremented by 2). Wallis's progression does not operate as efficiently as that proposed by Franciscus Vieta, however if we multiply the first 20,000 terms we get 3.1415141186819087!```
 Subject: Re: Java Programming Answered By: rbnn-ga on 13 Nov 2002 08:51 PST Rated:
 ```Thank you for this question. I had seen these formulas but not implemented them before. It was interesting to see how quickly the Vieta formula converged. By the way, should you have any questions about the answer or need clarification or new features, please use the "Request Clarification" button on the browser to solicit it. I created an abstract base class named Pythagoras with two concrete subclasses, Wallis and Vieta. An instance of a class holds a number of Terms and a value. The base class has an abstract method compute() which is overridden in each subclass in order to perform the computation. There is also a testing class Pi . When run, it prompts the user for the number of terms, runs each computation using that number of terms, and prints the result. Unfortunately, in Java to read input from the terminal is a little bit messy. That is why I had to do all that stuff with BufferedReader and so on in the class Pi. This is standard boilerplate that is just copied from program to program. If you enjoy mathematical computations, or just want to learn Java better, I recommned trying out the site http://www.topcoder.com . It's pretty fun. There is a contest there tonight in fact. Below I am including a sample script below. You can download the four files from: http://www.rbnn.com/google/106752/Pi.java http://www.rbnn.com/google/106752/Pythagoras.java http://www.rbnn.com/google/106752/Vieta.java http://www.rbnn.com/google/106752/Wallis.java To compile them, just put them into their own directory and do javac Pi.java To run, use java Pi % javac Pi.java % java Pi Input the desired number of terms (-1 to exit): 100 Running Vieta for: 100 terms Got Vieta value of: 3.141592653589794 for an error of: 8.881784197001252E-16 Running Wallis for: 100 terms Got Wallis value of: 3.126078900215409 for an error of: -0.015513753374384098 Input the desired number of terms (-1 to exit): 10000 Running Vieta for: 10000 terms Got Vieta value of: 3.141592653589794 for an error of: 8.881784197001252E-16 Running Wallis for: 10000 terms Got Wallis value of: 3.1414355935898644 for an error of: -1.5705999992876585E-4 Input the desired number of terms (-1 to exit): 20000 Running Vieta for: 20000 terms Got Vieta value of: 3.141592653589794 for an error of: 8.881784197001252E-16 Running Wallis for: 20000 terms Got Wallis value of: 3.141514118681855 for an error of: -7.853490793818807E-5 Input the desired number of terms (-1 to exit): -1 Although I recommend downloading the files from the above site, for completeness here are the files: public abstract class Pythagoras{ public int numberTerms; protected double value; public Pythagoras(int terms){ numberTerms=terms; } public double getValue(){return value;} public String toString(){return "Pythagoras: "+value;} public double error(){return value-Math.PI;} public abstract double compute(); } public class Pi{ static java.io.BufferedReader reader; static Vieta vieta; static Wallis wallis; public static void main(String[] args){ reader= new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); while(true){ System.out.print("Input the desired number of terms (-1 to exit): "); String line=null; try{ line=reader.readLine();} catch(Exception e){ System.exit(1); } int nterms=Integer.parseInt(line); if (nterms<1) System.exit(0); vieta=new Vieta(nterms); System.out.println("\nRunning Vieta for: "+nterms+" terms"); double vvalue=vieta.compute(); double verror=vieta.error(); System.out.println("Got Vieta value of: "+vvalue+" for an error of: "+verror); wallis=new Wallis(nterms); System.out.println("\nRunning Wallis for: "+nterms+" terms"); double wvalue=wallis.compute(); double werror=wallis.error(); System.out.println("Got Wallis value of: "+wvalue+" for an error of: "+werror); } } } /* Using formula from Vietas: 2 sqrt(2) sqrt(2+sqrt(2)) sqrt(sqrt(2+sqrt(2))) sqrt(sqrt(sqrt(2+sqrt(2)))) ---- = --------- X ----------------- X ----------------------- X----------------------------- X .... PI 2 2 2 */ public class Vieta extends Pythagoras{ public Vieta(int terms){ super(terms); } public double compute(){ double numerator=0; value=0; if (numberTerms<=0)return value; double product=1; for (int i=0;i
 strongbow69-ga rated this answer: `Great answer, thanks`

 `We need a new sub-category: Computers > Programming > Assignments =)`
 `Yeah, or start a new site. Welcome to a new trend!`