题目
英语翻译
这是一个java编程题,题目是英文的,想要翻译,如果能完成程序编写,追加100分
Pi can be calculated using the following sum,which is an approximation to Pi/4 :
PiSum = 1 - 1/3 + 1/5 - 1/7 + ...+ (-1)^n / (2*n + 1)
1) Write two methods that return PiSum for a specified value of n,one method
using recursion and the other using iteration to do the sum.
Hint:instead of calculating (-1)^n,you can check whether the nth term is
negative by checking whether n%2 (i.e.n modulo 2) is 1.
Assume that the methods are always called with a non-negative value of n,so you
don't need to worry about dealing with invalid parameter values.
2) Show the additional code you would add to the above methods to throw an
exception if n is less than zero (you don't need to rewrite the whole method).The
exception handler should generate an error message saying that the number of terms
in the sum must not be negative.
Write a code fragment (i.e.it doesn't need to be a complete method) that invokes
one of the methods for calculating Pi and prints out the result if the method returns
correctly,or else catches an exception and prints out the error message returned.
3) When calculating Pi using the given formula,suppose that instead of passing a
parameter specifying the number of terms n in the sum,we instead want to pass a
parameter that specifies a precision value.The sum should terminate at the first value
n for which the absolute value of the nth term in the sum,i.e.1/(2*n+1) is less than
the specified precision.Show how this can be implemented using a do loop.
这是一个java编程题,题目是英文的,想要翻译,如果能完成程序编写,追加100分
Pi can be calculated using the following sum,which is an approximation to Pi/4 :
PiSum = 1 - 1/3 + 1/5 - 1/7 + ...+ (-1)^n / (2*n + 1)
1) Write two methods that return PiSum for a specified value of n,one method
using recursion and the other using iteration to do the sum.
Hint:instead of calculating (-1)^n,you can check whether the nth term is
negative by checking whether n%2 (i.e.n modulo 2) is 1.
Assume that the methods are always called with a non-negative value of n,so you
don't need to worry about dealing with invalid parameter values.
2) Show the additional code you would add to the above methods to throw an
exception if n is less than zero (you don't need to rewrite the whole method).The
exception handler should generate an error message saying that the number of terms
in the sum must not be negative.
Write a code fragment (i.e.it doesn't need to be a complete method) that invokes
one of the methods for calculating Pi and prints out the result if the method returns
correctly,or else catches an exception and prints out the error message returned.
3) When calculating Pi using the given formula,suppose that instead of passing a
parameter specifying the number of terms n in the sum,we instead want to pass a
parameter that specifies a precision value.The sum should terminate at the first value
n for which the absolute value of the nth term in the sum,i.e.1/(2*n+1) is less than
the specified precision.Show how this can be implemented using a do loop.
提问时间:2020-07-27
答案
我有一个类似的代码.将就着用把大概意思是一样的只是没有异常处理public class a2and5{public static void main(String [] agrs){int count=0;double pi=0.0,l=1.0,diff,pi1;diff=1;while(Math.abs(diff)>0.01){ pi1...
举一反三
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
1,人们染上烟瘾,最终因吸烟使自己丧命.
最新试题
热门考点
- 1描写雪景的成语
- 2酸去铁锈时酸不能过量的原因.
- 3向铜片中加入115毫升某浓度的硫酸,在加热条件下反应,待铜片完全溶解后,将其溶液稀释到500毫升
- 4请问这句英文中的learned behavior前面问什么没有冠词?
- 5探究弹力和弹簧伸长的关系时,在弹性限度内,悬挂15N重物时,弹簧长度为0.16m,悬挂20N重物时,弹簧长度为0.18m,则弹簧的原长L0和劲度系数k分别为多少?
- 61.There are a lot of signs in the park.2.They mean different things.3.We can play football改否定句
- 7文章读后感400字
- 8以下关于return语句的叙述中正确的是,请逐一分析!
- 9若方程3m(x+1)+1=m(3-x)-5x的解是正数,则m的取值范围是( ) A.m>54 B.m<54 C.m>−54 D.m<−54
- 10那是一个夏天的长的不能再长的下午,在印第安那州的一个湖边.我起先是不经意地坐着看书,忽然发现湖边有几棵树.正在飘散一些白色的纤维.大团大团的,像棉花似的,有些飘在草地上,有些飘入湖水里.我当时没有十分