精选问答
面向对象与Java程序设计基础题目:设计一个程序可以一计算平面图形的面积和立体图形的体积。1.使用interface关键字定义Shape接口,接口中包含一个求解面积的方法定义;2.使用extends从接口Shape派生出接口Shape2D和Shape3D接口,并为接口Shape2D添加一个求解周长的方法定义;为接口Shape3D添加一个求解体积的方法定义;3.编写一个Circle类,该类实现Shape2D接口;4.编写一个Square类,该类实现Shape3D接口;5.定义一个Scaleable接口,该接

2020-01-04

面向对象与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("##############");
System.out.println("Circumference:"+shape.getCircumference());
System.out.println("Area:"+shape.getArea()+"");
}
}
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("##############");
System.out.println("Circumference:"+shape.getCircumference());
System.out.println("Area:"+shape.getArea()+"");
}
}
相关问答