问题描述:
设有Shape类,Rectangle类, Cube类,Circle类, Clinder类,这些类之间的关系如下图所示. 要求:1 定义这五个类,至少包含下列表(下页)的功能. 2. 设计一个主方法,其功能是求n个图形(是表中图形个数的任意组合)的面积之和,运行界面如图。3.该题目要给出3种设计方案:(1)每个类是独立的类,(2)利用图给出的继承关系设计5个类,(3)Shape类设计为抽象类,再利用继承设计其他类。
shape类:
public abstract class Shape {
public abstract double area();
public abstract double perimeter();
public abstract void show();
public abstract void getName();
}
Rectangle类:
public class Rectangle extends Shape {
//矩形左上角顶点坐标
private double x;
private double y;
private double len;
private double wid;
public Rectangle(double x, double y, double len, double wid) {
this.x = x;
this.y = y;
this.len = len;
this.wid = wid;
}
public double area(){
return len * wid;
}
public double perimeter(){
return 2 *(len + wid);
}
public void show(){
System.out.println("矩形的左上角顶点坐标为:(" + x + "," + y + "),矩形的长为:" + len + ",宽为:" + wid + ",矩形的面积为:" + area() + ",矩形的周长为:" + perimeter() );
}
public void getName(){
System.out.println("显示矩形");
}
public double getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public double getLen() {
return len;
}
public void setLen(float len) {
this.len = len;
}
public double getWid() {
return wid;
}
public void setWid(float wid) {
this.wid = wid;
}
}
Cube类:
public class Cube extends Rectangle{
private double h;
public Cube(double x, double y, double len, double wid, double h) {
super(x, y, len, wid);
this.h = h;
}
public double area(){
return (super.getLen() * super.getWid() + super.getLen() * h + super.getWid() * h) * 2;
}
public double perimeter(){
return 4 * ( super.getLen() + super.getWid()) + 2 * h;
}
public void show(){
System.out.println("长方体的左上角顶点坐标为:(" + super.getX() + "," + super.getY() + "),长方体的长为:" + super.getLen() + ",宽为:" + super.getWid() + ",高为:" + h + ",长方体的面积为:" + area() + ",长方体的周长为:" + perimeter());
}
public void getName(){
System.out.println("显示立方体");
}
public double getH() {
return h;
}
public void setH(float h) {
this.h = h;
}
}
Circle类:
public class Circle extends Shape{
private double x;
private double y;
private double r;
public Circle(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
public double area(){
return Math.PI * r * r;
}
public double perimeter(){
return 2 * Math.PI * r;
}
public void show(){
System.out.println("圆的圆心坐标为:(" + x + "," + y + "),圆的半径为:" + r + ",圆的面积为:" + area() + ",圆的周长为:" + perimeter());
}
public void getName(){
System.out.println("显示圆");
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
Clinder类:
public class Clinder extends Circle{
private double h;
public Clinder(double x, double y, double r, double h) {
super(x, y, r);
this.h = h;
}
public double area(){
return 2 * super.area() + super.perimeter() * h;
}
public double perimeter(){
return 2 * (2 * Math.PI * super.getR()) + 2 * h;
}
public double volume(){
return super.area() * h;
}
public void show(){
System.out.println("圆柱体底面圆的圆心坐标为:(" + super.getX() + "," + super.getY() + "),底面圆的半径为:" + super.getR() + ",圆柱体的高为:" + h + ",圆柱体的面积为:" + area() + ",圆柱体的周长为:" + perimeter() + ",圆柱体的体积为:" + volume());
}
public void getName(){
System.out.println("显示圆柱体");
}
}
main主函数测试包含菜单:
public class Text {
public static void main(String[] args) {
int select;
double x, y, r, h, len, wid;
Scanner cin = new Scanner(System.in);
Shape[] ce = new Shape[100];
int n = 0;
while (true) {
System.out.println("1,创建一个圆,2.创建一个矩形,3.创建一个长方体,4.创建一个圆柱体,5.显示已经创建的图形,6.求个图形的面积之和,7.退出系统");
System.out.println("请输入您的选择:");
select = cin.nextInt();
if (select < 7 && select > 0) {
switch (select) {
case 1:
System.out.println("请输入圆的圆心坐标x:");
x = cin.nextDouble();
System.out.println("请输入圆的圆心坐标y:");
y = cin.nextDouble();
System.out.println("请输入圆的半径:");
r = cin.nextDouble();
ce[n] = new Circle(x, y, r);
n++;
break;
case 2:
System.out.println("请输入矩形的的左上角顶点坐标x:");
x = cin.nextDouble();
System.out.println("请输入矩形的的左上角顶点坐标y:");
y = cin.nextDouble();
System.out.println("请输入矩形的的长:");
len = cin.nextDouble();
System.out.println("请输入矩形的的宽:");
wid = cin.nextDouble();
ce[n] = new Rectangle(x, y, len, wid);
n++;
break;
case 3:
System.out.println("请输入长方体的的左上角顶点坐标x:");
x = cin.nextDouble();
System.out.println("请输入长方体的的左上角顶点坐标y:");
y = cin.nextDouble();
System.out.println("请输入长方体的的长:");
len = cin.nextDouble();
System.out.println("请输入长方体的的宽:");
wid = cin.nextDouble();
System.out.println("请输入长方体的的高:");
h = cin.nextDouble();
ce[n] = new Cube(x, y, len, wid, h);
n++;
break;
case 4:
System.out.println("请输入圆柱体的底面圆心坐标x:");
x = cin.nextDouble();
System.out.println("请输入圆柱体的底面圆心坐标y:");
y = cin.nextDouble();
System.out.println("请输入圆柱体的底面圆的半径r:");
r = cin.nextDouble();
System.out.println("请输入圆柱体的高:");
h = cin.nextDouble();
ce[n] = new Clinder(x, y, r, h);
n++;
break;
case 5:
for (int i = 0; i < n; i++) {
System.out.print("(" + (i + 1) + ").");
ce[i].show();
}
break;
case 6:
double areaSum = 0;
for (int i = 0; i < n; i++) {
areaSum += ce[i].area();
}
System.out.println("所有图形面积之和为:" + areaSum);
break;
default:
break;
}
} else {
System.out.println("退出系统成功");
break;
}
}
}
}