Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
  
Lab 1A: Approximating PI 
 
Introduction: 
 
The history of the mathematical constant PI covers thousands of years and cultures from around 
the globe. 
 
If you read the Wikipedia entry on PI you will be amazed to see how clever and hardworking 
early mathematicians were and how close their approximations came to the correct value of PI. 
For example, Archimedes knew that the value of PI was between 223/71 and 22/7 way back in 
250 BC. The Chinese mathematician Zu Chongzhi approximated PI to 6 decimal places with the 
ratio 355/113 in 480 AD, which was the most accurate estimate of PI for over 1000 years. 
 
More recent approximations of PI on computers have brought us from a thousand digits of 
accuracy in 1949 to over 10 trillion digits of accuracy in 2011. Along the way, researchers have 
invented some very fancy algorithms and kept thousands of computers busy number crunching 
for months at a time. 
 
Instructions: 
 
The true purpose of this lab is not to calculate the world's best approximation of PI, but instead to 
learn about how Java stores numbers and performs calculations. 
 
The program below uses the Java System.out.println statement to print out a sequence of 
messages and approximations to PI. The top of the program has an import statements that tells 
the Java compiler which libraries of functions your program will be using. In this case, 
"java.util.Scanner " for user input commands. 
 
import java.util.Scanner; 
public class Main 
{ 
    public static void main(String[] args) 
    { 
        Scanner scnr = new Scanner(System.in); 
         
        // Read input parameter 
        System.out.println("Some PI approximations: ");  
 
        // Archimedes 225 BC 
        System.out.println("22 / 7 = " + 22 / 7); 
 
        // Zu Chongzhi 480 AD 
        System.out.println("355 / 113 = " + 355 / 113); 
 
        // Indiana law 1897 AD 
        System.out.println("16 / 5 = " + 16 / 5); 
 
        // Java math library 
        System.out.println("Math.PI = " + Math.PI); 
    } 
} 
 
Step 1: Go to onlinegdb.com, select the “Java” compiler, and copy and paste the program above 
into the editor window and compile the program. Hopefully you will not get any error messages. 
 
Step 2: When you run the program, you should see several lines of messages with different 
approximations of PI. The good news is that your program has output. The bad news is that all of 
your approximation for PI are all equal to 3, which is not what we expected or intended. 
 
Step 3: Java performs two types of division. If you have x/y and both numbers x and y are 
integers, then C++ will do integer division, and return an integer result. On the other hand if you 
have x/y and either number is floating point Java will do floating point division and give you a 
floating point result. Edit your program and change "22 / 7" into "(float)22/(float)7" and 
recompile and run your program. Now your program should output " 3.142857". 
 
Step 4: Edit your program again and convert the other integer divisions into floating point 
divisions. Recompile and run your program to see what it outputs. Hopefully you will see that Zu 
Chongzhi was a little closer to the true value of PI than the Indiana law in 1897. 
 
Step 5: By default, the " System.out.println " command prints floating point numbers with up to 
6 digits of precision after ther decimal point. This is much less than the accuracy of most 
computers. Fortunately, we get much more accuracy with a double.  Edit your program and 
change all (float) into (double) in your formulas and look at your results.  You should see that 
you now have 15 digits after the decimal point.  
 
Step 6:  Java assumes that all numbers with decimal points like 22.0 are double, so you can go in 
and edit your program to use 22.0/7.0 and get the same result as (double)22/(double)7 with a lot 
less typing.  
 
Step 7: Once you think your program is working correctly, upload your final program and a copy 
of your program output into Blackboard for grading.