java第九次作业

题目:  利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。

1.Shape.java

/**定义了Shape接口
* 有一个getArea的声明
*/
package com;
public interface Shape {
double getArea();
}

2.Rectangle.java

/**Rectangle类使用了Shape接口
* 有2个成员变量,长和宽
* 定义了求Rectangle类的面积的方法
* */
package com;
public class Rectangle implements Shape {//使用了Shape接口
double width; //矩形的长
double length;//矩形的宽
public Rectangle(double width,double length){ //矩形的构造方法
this.width=width;
this.length=length;
}
public double getArea(){//求矩形面积的方法
return width*length;
}
}

3.Triangle.java

/**使用了Shape接口
* 有3个成员变量,三角形的三边
* 一个求面积的方法
* */package com;
public class Triangle implements Shape{//使用了接口
double a;
double b;
double c;
public Triangle(double a,double b,double c){//构造方法
this.a=a;
this.b=b;
this.c=c;
}
public double getArea(){//求面积的方法
double p=(a+b+c)/2;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
}

4.Zheng.java

/**正方形继承了三角形的类
* 一个构造方法
* 求一个面积的方法
* */
package com;
public class Zheng extends Rectangle {//正方形继承了三角形
public Zheng(double width) {
super(width, width);
}
public Zheng(){
super(0,0);
}
public double getArea(){//求面积的方法
return width*width;
}
}

 5.TiXing.java

/**使用了Shape接口
* 3个成员变量
* 一个构造方法
* 一个求面积的方法
* */
package com;
public class TiXing implements Shape {//使用了Shape接口
double shang;//成员变量 上底
double xia;//成员变量下底
double h;// 成员变量高
public TiXing(double shang,double xia,double h){//构造方法
this.shang=shang;
this.xia=xia;
this.h=h;
}
public double getArea(){//求面积的方法
return (shang+xia)*h/2;
}
}

6.ZhuTi.java

/**定义了一个构造方法
* 一个求面积的方法
* 一个换底的方法
* */
package com;
public class ZhuTi {
Shape shape;
double high;//成员变量
public ZhuTi(Shape shape,double high){
this.shape=shape;
this.high=high;

}
public double getV(){//求体积的方法
return shape.getArea()*high;
}
public void setRect(Shape shape){//换底的方法
this.shape=shape;
}
}

7.Factory.java

/**getShape方法获得底面图形
* switch方法根据键盘的输入来选择底面图形并给底面图形各数据赋值
*/
package com;
import java.util.Scanner;
public class Factory {
Shape getShape(char c){
Scanner sc=new Scanner(System.in);
Shape shape=null;
switch(c){//switch分支语句 根据不同的字符选择不同的case语句
case 'R':
System.out.print("请输入矩形的长和宽");
shape = new Rectangle(sc.nextInt(), sc.nextDouble());
break;
case 'T':
System.out.println("请输入三角形的三条边");
shape=new Triangle(sc.nextDouble(),sc.nextDouble(),sc.nextDouble());
break;
case 'Z':
System.out.println("请输入正方形的边");
shape=new Zheng(sc.nextDouble());

break;
case 'C':
System.out.println("请输入圆的半径");
shape=new Circle(sc.nextDouble());
break;
case 't':
System.out.println("请输入梯形的上底下底和高");
shape = new TiXing(sc.nextDouble(), sc.nextDouble(), sc.nextDouble());
break;
}
return shape;
}
}

8.Test.java

/**测试类通过键盘输入选择底面的图形
*创建工厂对象,调用工厂里的getShape方法获得底面图形
*调用柱体求体积方法求得体积
*/
package com;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stubS
Scanner sc = new Scanner(System.in);
System.out.print("请输入你选择的形状:矩形R,三角形T,正方形Z,圆C,梯形t:");
char c = sc.next().charAt(0);//接受从键盘输入 的字符
System.out.print("请输入柱体的高:");
double g=sc.nextDouble();
Factory factory = new Factory();
ZhuTi zhuti= new ZhuTi(factory.getShape(c), g);
System.out.print(zhuti.getV());
}

}

2.运行结果

矩形

三角形

正方形

梯形

 

posted @ 2019-10-08 17:31  安稳度余生  阅读(151)  评论(0编辑  收藏  举报