实验
7
1.
编一个程序,包含以下文件。
(
1
)文件,在该文件中定义接口
Shape
,该接口在
shape
包中。
属性:
PI
。
方法:求面积的方法
area()
。
(
2
)文件,在该文件中定义圆类
Circle
,该类在
circle
包中,实现
Shape
接口。
属性:圆半径
radius
。
方法:构造方法;实现接口中求面积方法
area()
;求周长方法
perimeter()
。
(
3
)
“”
文件,
在该文件中定义圆柱体类
Cylinder
,
该类口在
cylinder
包中,
继承圆类。
属性:圆柱体高度
height
。
方法:构造方法;求表面积方法
area()
;求体积方法
volume()
。
(
4
)文件,在该文件中定义主类
X5_3_6
,该类在默认包中,其中包含主方法
main()
,
在主方法中创建两个圆类对象
cir1
和
cir2
,具体尺寸自己确定,并显示圆的面积和周长;
再创建两个圆柱体类的对象
cy1
和
cy2
,
具体尺寸自己确定,
然后分别显示圆柱体
cy1
和
cy2
的底圆的面积和周长以及它们各自的体积和表面积。
【编程分析】本题主要考察接口、包、继承、封装等问题。编程步骤如下:
第一步:首先创建
p1
包,在其中创建
Shape
接口
;
public class Circle implements Shape{
;
public class X5_3_6 { ;
public class X5_3_6 { ;
public class Circle implements Shape{
;
public class X7_3_2 {
public static void main(String[] args)throws IOException{
InputStreamReader isr = new InputStreamReader;
BufferedReader br = new BufferedReader(isr);
int[] a = new int[5];
int n = ());
if(n>5)
;
public class X7_3_3 {
public static void main(String args[]) throws IOException{
InputStreamReader isr = new InputStreamReader;
BufferedReader br = new BufferedReader(isr);
"
请输入两个整数:
");
int a = ( ());
int b = ( ());
try{
;
public class X7_3_4 {
public static void main(String args[]) throws IOException{
InputStreamReader isr = new InputStreamReader;
BufferedReader br = new BufferedReader(isr);
"
请输入两个整数:
");
int a = ( ());
int b = ( ());