文档库

最新最全的文档下载
当前位置:文档库 > Java实验2

Java实验2

1.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 constructor 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 everyone.

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 the (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 instance 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 print 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 http://www.wendangku.net/doc/9d4e20bb3968011ca200910d.html ng.Math.PI]

c) Write an application to test the Shape class that performs the following:

i) Declares 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.

2.现有一个字符串数组str_arr,其中共有20个元素,欲调用java.util.Arrays.sort()方法对其进行排序。请编写http://www.wendangku.net/doc/9d4e20bb3968011ca200910d.html parator的实现类,将两个字符串的比较规则改为:先将字符串中的数字’0’~’9’分别替换成字母’a’~’j’,并忽略字母的大小写。测试该实现类的正确性。

3.编写程序,从命令行参数读取一个字符串,然后1)检验它是否是回文;2)显示其中最大连续递增的有序子串。

免费下载Word文档免费下载: Java实验2

(共3页)