/* * Copyright Derek Park 2001 * All rights reserved */ import java.awt.Graphics; import javax.swing.*; public class pi{ // a class to compute pi public static void main(String Args []){ // main method // get user input, send to pi(), display results JOptionPane.showMessageDialog(null, "PI = " + new Double( pi( Long.parseLong( JOptionPane.showInputDialog( "How many iterations?" ) ) ) ) ); System.exit(0); }//end main static double pi(long numOfIterations){ // get input double returnVal = 0.0; // stores return value of pi double denominator = 1.0; // stores current denominator int sign = 1; // stores current sign for(int i = 0; i <= numOfIterations; i++){ // until it meets the user input returnVal += 4.0/denominator*sign; // add the next term in the series denominator += 2; // set the denominator up for the next iteration sign *= -1; // set up the sign for the next iteration } return returnVal; // exit method } //end pi } //end class