英语翻译这是一个java编程题,题目是英文的,想要翻译,如果能完成程序编写,追加100分Pi can be calcul
来源:学生作业帮 编辑:作业帮 分类:英语作业 时间:2024/07/15 01:20:45
英语翻译
这是一个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.
我有一个类似的代码.将就着用把
大概意思是一样的只是没有异常处理
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=pi;
pi+=((4.0/(count*2.0+1.0))*l);
l*=-1.0;
System.out.print(pi+" "+count+"\n");
count++;
diff=pi1-pi;
}
}
}
/*
3.1467739659661436 192
3.136438048653482 193
3.1467208250030967 194
3.1364906459749635 195
3.1466687630233094 196
3.1365421807448284 197
3.146617747495458 198
3.136592684838816 199
3.1465677471829556 200 */
大概意思是一样的只是没有异常处理
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=pi;
pi+=((4.0/(count*2.0+1.0))*l);
l*=-1.0;
System.out.print(pi+" "+count+"\n");
count++;
diff=pi1-pi;
}
}
}
/*
3.1467739659661436 192
3.136438048653482 193
3.1467208250030967 194
3.1364906459749635 195
3.1466687630233094 196
3.1365421807448284 197
3.146617747495458 198
3.136592684838816 199
3.1465677471829556 200 */
英语翻译这是一个java编程题,题目是英文的,想要翻译,如果能完成程序编写,追加100分Pi can be calcul
(编程题)编写一个程序,判断一个整数的奇偶性,如果是奇数,输出1,是偶数输出2
用java循环编写一个程序题目.
一道英文的Java编程题目
java编程题目输入一个整数 查看能否被3或者被5整除;如果能被3或5整除输出该数是3或5的倍数;如果不能被3或者5整除
英语翻译本服务器是通过Socket网络编程技术编写的一个支持FTP协议的服务器程序.用户可通过一个同样支持FTP协议的客
java程序问题?下面是一个 5*5 的螺旋方阵(顺时针方向旋转)编程输出 10*10(n
用JAVA编写一个程序能统计每个长度的单词的数量!
用Java application 编写一个矩形的程序,能输出周长和面积
用JAVA编写一个程序,要求生成5个随机数,随机数的取值范围是20到60
java 判断奇偶数编写一个程序,判断一个整数是奇数还是偶数
谁帮我用c++语言编写这个程序完成的话追加80分 10月31号以前完成追加100分