java第九次作业
题目: 利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
1.Shape.java
/**定义了Shape接口 * 有一个getArea的声明 */ package com; public interface Shape { double getArea(); }
2.Rectangle.java
/**Rectangle类使用了Shape接口 * 有2个成员变量,长w和宽l * 定义了求Rectangle类的面积的方法 * */ package com; public class Rectangle implements Shape {//使用了Shape接口 double w; //矩形的长 double l;//矩形的宽 public Rectangle(double w,double l){ //矩形的构造方法 this.w=w; this.l=l; } public double getArea(){//求矩形面积的方法 return w*l; } }
3.Triangle.java
/**使用了Shape接口 * 有3个成员变量,三角形的三边a,b,c * 一个求面积的方法 * */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 w) { super(w, w); } public Zheng(){ super(0,0); } public double getArea(){//求面积的方法 return w*w; } }
5.TiXing.java
/**使用了Shape接口 * 定义3个成员变量s,x.h * 一个构造方法 * 一个求面积的方法 * */ package com; public class TiXing implements Shape {//使用了Shape接口 double s;//成员变量 上底 double x;//成员变量下底 double h;// 成员变量高 public TiXing(double s,double x,double h){//构造方法 this.s=s; this.x=x; this.h=h; } public double getArea(){//求面积的方法 return (s+x)*h/2; } }
6.ZhuTi.java
/**定义了一个构造方法 * 一个求面积的方法 * 一个换底的方法 * */ package com; public class ZhuTi { Shape shape; double h;//成员变量 public ZhuTi(Shape shape,double h){ this.shape=shape; this.h=h; } public double getV(){//求体积的方法 return shape.getArea()*h; } public void setRect(Shape shape){//换底的方法 this.shape=shape; } }
7.Circle.java
/** * 创建圆类,定义变量r和求面积的方法 */ package com; public class Circle implements Shape{ double r; Circle(double r){ this.r=r; } public double getArea(){ return r*r*Math.PI; } }
8.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; } }
9.Test.java
/**测试类通过键盘输入选择底面的图形 *创建对象,调用工厂里的getShape方法获得底面图形 *调用柱体求体积方法求得体积 */ package com; import java.util.Scanner; public class Test { public static void main(String[] args) { while(true){ 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.运行结果