早教吧 育儿知识 作业答案 考试题库 百科 知识分享

面向对象与Java程序设计基础题目:设计一个程序可以一计算平面图形的面积和立体图形的体积。1.使用interface关键字定义Shape接口,接口中包含一个求解面积的方法定义;2.使用extends从接

题目详情
面向对象与Java程序设计基础
题目:设计一个程序可以一计算平面图形的面积和立体图形的体积。
1.使用interface关键字定义Shape接口,接口中包含一个求解面积的方法定义;
2.使用extends从接口Shape派生出接口Shape2D和Shape3D接口,并为接口Shape2D添加一个求解周长的方法定义;为接口Shape3D添加一个求解体积的方法定义;
3.编写一个Circle类,该类实现Shape2D接口;
4.编写一个Square类,该类实现Shape3D接口;
5.定义一个Scaleable接口,该接口中定义一个scale(double proportion)方法;
6.编写一个可缩放的圆形CircleScaleable类,该类继承于Circle类同时实现Scaleable接口;
7.编写一个CircleScaleable类的测试程序。
三、评分标准
1.按照要求完成各题(80分)
2.完成思考题:
(1)接口类型变量能否指向子类实例对象?(10分)
(2)能否使用接口实现多态性技术的编程?(10分)
▼优质解答
答案和解析
1.shape接口:
public interface Shape {

double getArea();
}
2.shape2D接口:
public interface Shape2D extends Shape {
double getCircumference();
}
shape3D接口:
public interface Shape3D extends Shape {
double getVolume();
}
3.Circle类:
public class Circle implements Shape2D {

public Circle(double radius){
this.setRadius(radius);
}
@Override
public double getCircumference() {
return 2*Math.PI*radius;
}
@Override
public double getArea() {
return Math.PI*radius*radius;
}

public void setRadius(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
private double radius;
}
4.Square类:
public class Square implements Shape3D {

public Square(int length,int width,int height){
this.setHeight(height);
this.setLength(length);
this.setWidth(width);
}
@Override
public double getVolume() {
return length*width*height;
}
@Override
public double getArea() {
return 2*length*width+2*width*height+2*length*height;
}

public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
private int length;
private int width;
private int height;
}
5.Scaleable接口:
public interface Scableable {
void scale(double propertion);
}
6.CircleScaleable类:
public class CircleScaleable extends Circle implements Scableable {
public CircleScaleable(int radius) {
super(radius);
}
@Override
public void scale(double propertion) {
super.setRadius(super.getRadius()*propertion);
}
}
7.CircleScaleable测试程序:
public class CircleScaleableTest {
public static void main(String[] args){
CircleScaleable circle=new CircleScaleable(100);
printShape2D(circle);
circle.scale(0.5);
printShape2D(circle);
circle.scale(2.5);
printShape2D(circle);
}

public static void printShape2D(Shape2D shape){
System.out.println("##############\n");
System.out.println("Circumference:"+shape.getCircumference());
System.out.println("Area:"+shape.getArea()+"\n");
}
}
看了 面向对象与Java程序设计基...的网友还看了以下:

如图,第①个图形中一共有1个平行四边形,第②个图形中一共有5个平行四边形,第③个图形中一共有11个  2020-06-13 …

下列图形都是由同样大小的菱形按照一定规律所组成的,其中第①个图形中一共有3个菱形,第②个图形中一共  2020-07-08 …

下列图形都是由同样大小的小圆圈按一定规律所组成的,其中第①个图形中一共有4个小圆圈,第②个图形中一  2020-07-29 …

下列图形都是由同样大小的平行四边形按一定的规律组成,其中,第①个图形中一共有1个平行四边形,第②个  2020-07-29 …

autocad的问题怎么把画好的三视图其中一部分移动,又不会改变原来的对齐.虚线如果恰好变成直线怎  2020-08-01 …

下列图形都是由同样大小的正方形和正三角形按一定的规律组成,其中,第①个图形中一共有5个正多边形,第  2020-08-02 …

(2014•威海一模)如图R为一含有23892U的放射源,它能放出α、β、γ三种射线变为22286R  2020-11-13 …

如图所示为某门电路构成的一简单控制电路,其中R’与R中一个为光敏电阻(光敏电阻在光照时电阻很小),另  2020-11-26 …

下列图形都是由同样大小的正方形和正三角形按一定的规律组成,其中,第①个图形中一共有5个正多边形,第②  2020-12-12 …

下列图形都是由同样大小的圆按一定的规律组成,其中,第(1)个图形中一共有2个圆;第(2)个图形中一共  2021-01-15 …