bitjava实验2

上传人:第*** 文档编号:31321785 上传时间:2018-02-06 格式:DOCX 页数:21 大小:227.22KB
返回 下载 相关 举报
bitjava实验2_第1页
第1页 / 共21页
bitjava实验2_第2页
第2页 / 共21页
bitjava实验2_第3页
第3页 / 共21页
bitjava实验2_第4页
第4页 / 共21页
bitjava实验2_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《bitjava实验2》由会员分享,可在线阅读,更多相关《bitjava实验2(21页珍藏版)》请在金锄头文库上搜索。

1、Java 实验二CXY,BIT实验 1.1Pi 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:

2、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 dont need to worry about dealing with invalid parameter values. 2) Show the additional code yo

3、u would add to the above methods to throw an exception if n is less than zero (you dont 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 doesnt need to be a comple

4、te 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

5、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

6、loop.import java.io.*;class myexception extends Exceptionprivate int a; /自定义 exceptionmyexception(int a)this.a=a;public String getmyexception()return Exception:the number of terms in the sum must not be negative!;public class calculate double pisum=0.0,pisum2=0.0;void calculate()pisum=0.0; /初始化void

7、caln(int n)pisum=0.0;for(double i=0;i=n);System.out.println(PiSum=+pisum);static void test1(int a) throws myexceptionif(a=0)System.out.println(Using Iteration:);example.caln(n);System.out.println(Using Recursion:);System.out.println(PiSum=+example.digui(n);catch(myexception e)System.out.println(Caug

8、ht +e.getmyexception();System.out.println();System.out.println(in put a precision value);str=buf.readLine();precision=Double.parseDouble(str);example.calprecision(precision);设计思路:a. 精度算法按照题目要求写,当 1/(2*n+1) 小于精度的时候即可停止。b. 自定义 exception,规定当输入的 n=0 则分别用迭代、递归方法进行计算,其中递归:当 n=0 时为最底层,return 1;c. 计算的时候按照 n

9、%2 的值来确定是加还是减。运行结果:1.1 输入 n 值大于等于 0 时1.2 输入 n 小于 0 时试验 2.2. a) Produce an abstract class to represent a general Shape. It must have the following properties: i) Instance variables to represent the (x, y) coordinate of the shape. Limit their visibility to the Shape class only. ii) It requires a const

10、ructor which initializes the (x, y) coordinate of the shape. Limit the visibility of the constructor to only the Shape class and classes that extend it. iii) Abstract methods to determine the area and circumference (perimeter) of the Shape are required. Make sure that these methods are visible to ev

11、eryone. iv) A concrete move method is needed so that the (x, y) coordinate of the Shape can be altered. Users should be able to indicate a relative move (i.e., 3 places left, 4 up) rather than specify an absolute location. This method should be visible to everyone. v) A print method that displays th

12、e (x, y) coordinates of the Shape and provides its area and circumference. This method should be visible to everyone. b) Define 2 classes, Square and Circle, which extend the class Shape above: i) Square will require an instance variable to hold the length of the sides, and Circle will require an in

13、stance variable to hold the radius. ii) Make sure that the constructors are defined to properly initialize all instance variables. iii) The print method must be overridden so that it displays the length of the sides, or radius (as appropriate) in addition to displaying all the information that the p

14、rint method from the superclass displays. Hint: The area of a circle is given by er2, and the circumference is 2er, where r is the radius of the circle. The value of e should be obtained by using java.lang.Math.PI c) Write an application to test the Shape class that performs the following: i) Declar

15、es an array of Shape objects. ii) Initialize the array so that it contains two Square objects of length 1 and 2 whose origin is (0, 0), and two Circle objects whose radius are 3 and 4 whose origin is also (0, 0). iii) Code to iterate over the Shape array and print out information about the shape. ab

16、stract class shape private int x,y;protected shape(int a,int b)x=a;y=b;public abstract void area(int b);public abstract void circumference(int a);public void move(int a,int b)System.out.println(Coordinate before move:+(+x+,+y+);x+=a;y+=b;System.out.println(Coordinate after move:+(+x+,+y+);public void print() System.out.println(Coordinate:(+x+,+y

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号