Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lab Exercise 5a
Approximate
Due Date
You must demonstrate the solution to this lab exercise to the instructor by Tuesday, February 17, 2015, in order
to receive credit for this work.
In this exercise you are to write a simple Java program to calculate an approximate value for the mathematical
constant . (This is programming exercise 5.25 from the Liang textbook.)
This lab exercise is also an opportunity to practice precise control over the output from your program.
= 4 1 3 + 5 7 + 9 11 + +
where
()
The value
()
21
+1 and -1.
Hint:
loopMax).
i = 1, 2, 3, ..., loopMax.
Debug your code for a small value like loopMax == 10.
for
11
piOver4 = 1 + +
CIT239-T1_Lab5a_20150203.docx
2/3/2015 1:15 PM
+ +
()
21
1
Lab Exercise 5a
Approximate
loopMax == 10,
so it displays
After the first 50 iterations of the loop, display the output less frequently: only when
of 100, plus the very next iteration after the multiple of 100. In other words, we want output for all of
the first 50 iterations, but after that we want a reporting interval of 100. Each reporting interval
should report two cases: one where the approximation is too low (counter value is even), and the
other where the counter value is too high (counter value is odd).
Always display the FINAL iteration.
i is a multiple
What this means is that, while the calculations must be done for every term of the series, our output will
be less frequent overall, according to the rules stated above.
Optional Enhancement
which keeps asking the user for integer input, until the user finally enters an integer value.
Sample Output:
In the two output samples (which begin on the next page):
Count = the value of
Math.PI
i.
constant:
piOver4
CIT239-T1_Lab5a_20150203.docx
()
calculation:
21
2/3/2015 1:15 PM
where
()
is either
+1
or
-1.
Lab Exercise 5a
Approximate
Variance (%)
+27.3239544735
-15.1173636843
+10.3474272104
-07.8417091430
+06.3053969096
-05.2695080425
+04.5246423016
-03.9636213300
+03.5260230508
-03.1752377109
%
%
%
%
%
%
%
%
%
%
termI
+1.0000000000
-0.3333333333
+0.2000000000
-0.1428571429
+0.1111111111
-0.0909090909
+0.0769230769
-0.0666666667
+0.0588235294
-0.0526315789
Variance (%)
+27.3239544735
-15.1173636843
+10.3474272104
-07.8417091430
+06.3053969096
-05.2695080425
+04.5246423016
-03.9636213300
+03.5260230508
-03.1752377109
+02.8878077402
-02.6480163673
+02.4449418116
-02.2707602059
+02.1197209828
-01.9875033551
+01.8707982957
-01.7670289750
+01.6741589837
-01.5905577976
+01.5149045066
-01.4461176905
+01.3833035200
-01.3257167879
+01.2727312626
-01.2238168643
+01.1785218993
-01.1364590911
+01.0972944961
-01.0607386305
+01.0265393116
-00.9944758387
+00.9643542301
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
termI
+1.0000000000
-0.3333333333
+0.2000000000
-0.1428571429
+0.1111111111
-0.0909090909
+0.0769230769
-0.0666666667
+0.0588235294
-0.0526315789
+0.0476190476
-0.0434782609
+0.0400000000
-0.0370370370
+0.0344827586
-0.0322580645
+0.0303030303
-0.0285714286
+0.0270270270
-0.0256410256
+0.0243902439
-0.0232558140
+0.0222222222
-0.0212765957
+0.0204081633
-0.0196078431
+0.0188679245
-0.0181818182
+0.0175438596
-0.0169491525
+0.0163934426
-0.0158730159
+0.0153846154
1776
Error
+00.8584073464
-00.4749259869
+00.3250740131
-00.2463545584
+00.1980898861
-00.1655464775
+00.1421458301
-00.1245208365
+00.1107732811
-00.0997530347
+00.0907231558
-00.0831898877
+00.0768101123
-00.0713380358
+00.0665929987
-00.0624392594
+00.0587728618
-00.0555128525
+00.0525952556
-00.0499688469
+00.0475921287
-00.0454311271
+00.0434577618
-00.0416486212
+00.0399840318
-00.0384473407
+00.0370243574
-00.0357029153
+00.0344725233
-00.0333240869
+00.0322496836
-00.0312423799
+00.0302960816
CIT239-T1_Lab5a_20150203.docx
2/3/2015 1:15 PM
Lab Exercise 5a
34:
3.1121872427
-00.0294054109
35:
3.1701582572
+00.0285656036
36:
3.1138202290
-00.0277724246
37:
3.1686147496
+00.0270220960
38:
3.1152814162
-00.0263112374
39:
3.1672294682
+00.0256368146
40:
3.1165965568
-00.0249960968
41:
3.1659792728
+00.0243866193
42:
3.1177865018
-00.0238061518
43:
3.1648453253
+00.0232526717
44:
3.1188683138
-00.0227243398
45:
3.1638121340
+00.0222194804
46:
3.1198560901
-00.0217365635
47:
3.1628668428
+00.0212741892
48:
3.1207615796
-00.0208310740
49:
3.1619986930
+00.0204060394
50:
3.1215946526
-00.0199980010
100:
3.1315929036
-00.0099997500
101:
3.1514934011
+00.0099007475
200:
3.1365926848
-00.0049999688
201:
3.1465677472
+00.0049750936
300:
3.1382593295
-00.0033333241
301:
3.1449149036
+00.0033222500
400:
3.1390926575
-00.0024999961
401:
3.1440864153
+00.0024937617
500:
3.1395926556
-00.0019999980
501:
3.1435886596
+00.0019960060
600:
3.1399259881
-00.0016666655
601:
3.1432565459
+00.0016638924
700:
3.1401640829
-00.0014285707
701:
3.1430191864
+00.0014265328
800:
3.1403426541
-00.0012499995
801:
3.1428410926
+00.0012484390
900:
3.1404815428
-00.0011111108
901:
3.1427025312
+00.0011098776
1000:
3.1405926538
-00.0009999997
1001:
3.1425916543
+00.0009990007
1100:
3.1406835629
-00.0009090907
1101:
3.1425009186
+00.0009082650
1200:
3.1407593204
-00.0008333332
1201:
3.1424252929
+00.0008326393
1300:
3.1408234229
-00.0007692307
1301:
3.1423612930
+00.0007686394
1400:
3.1408783680
-00.0007142856
1401:
3.1423064294
+00.0007137758
1500:
3.1409259870
-00.0006666666
1501:
3.1422588760
+00.0006662224
1600:
3.1409676537
-00.0006249999
1601:
3.1422172631
+00.0006246096
1700:
3.1410044183
-00.0005882352
1701:
3.1421805430
+00.0005878894
1776:
3.1410295906
-00.0005630630
Math.PI = 3.1415926536
BUILD SUCCESSFUL (total time: 8 seconds)
CIT239-T1_Lab5a_20150203.docx
-00.9360032994
+00.9092714032
-00.8840237303
+00.8601400296
-00.8375126967
+00.8160451536
-00.7956504726
+00.7762501999
-00.7577733480
+00.7401555282
-00.7233382014
+00.7072680286
-00.6918963062
+00.6771784731
-00.6630736793
+00.6495444081
-00.6365561422
-00.3183019294
+00.3151505804
-00.1591539484
+00.1583621475
-00.1061030007
+00.1057505010
-00.0795773472
+00.0793789006
-00.0636619136
+00.0635348441
-00.0530516109
+00.0529633387
-00.0454728177
+00.0454079493
-00.0397887202
+00.0397390465
-00.0353677542
+00.0353285004
-00.0318309807
+00.0317991815
-00.0289372564
+00.0289109737
-00.0265258192
+00.0265037328
-00.0244853722
+00.0244665518
-00.0227364175
+00.0227201888
-00.0212206567
+00.0212065190
-00.0198943659
+00.0198819397
-00.0187241093
+00.0187131016
-00.0179228525
2/3/2015 1:15 PM
Approximate
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
-0.0149253731
+0.0144927536
-0.0140845070
+0.0136986301
-0.0133333333
+0.0129870130
-0.0126582278
+0.0123456790
-0.0120481928
+0.0117647059
-0.0114942529
+0.0112359551
-0.0109890110
+0.0107526882
-0.0105263158
+0.0103092784
-0.0101010101
-0.0050251256
+0.0049751244
-0.0025062657
+0.0024937656
-0.0016694491
+0.0016638935
-0.0012515645
+0.0012484395
-0.0010010010
+0.0009990010
-0.0008340284
+0.0008326395
-0.0007147963
+0.0007137759
-0.0006253909
+0.0006246096
-0.0005558644
+0.0005552471
-0.0005002501
+0.0004997501
-0.0004547522
+0.0004543389
-0.0004168404
+0.0004164931
-0.0003847634
+0.0003844675
-0.0003572705
+0.0003570154
-0.0003334445
+0.0003332223
-0.0003125977
+0.0003124024
-0.0002942042
+0.0002940312
-0.0002816108