第9次作业--接口及接口回调
一、题目
利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
二、代码
1.Shape.java
package Thirtenth; /* *一个Shape接口类 * */ public interface Shape { public double getArea(); }
2.Rectiangle.java
package Thirtenth; /* * Rectiangle类使用接口 * 一个getArea的方法计算面积 * */ public class Rectiangle implements Shape { double a; double b; public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } Rectiangle(double a, double b) { this.a = a; this.b = b; } Rectiangle() { a = 0; b = 0; } @Override public double getArea() { // TODO Auto-generated method stub return a * b; } }
3.Square.java
package Thirtenth; /* * Square类使用接口 * 一个getArea的方法计算面积 * */ public class Square extends Rectiangle { Square(double side) { super(side, side); } Square() { a=0; } public double getArea() { // TODO Auto-generated method stub return a * a; } }
4.Triangle.java
package Thirtenth; /* * Triangle类使用接口 * 一个getArea的方法计算面积 * */ public class Triangle implements Shape { double a; double b; double c; public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public double getC() { return c; } public void setC(double c) { this.c = c; } Triangle(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } Triangle() { a = 0; b = 0; c = 0; } @Override public double getArea() { // TODO Auto-generated method stub double p = (a + b + c) / 2; return Math.sqrt(p * (p - a) * (p - b) * (p - c)); } }
5.Circle.java
package Thirtenth; /* * Circle类使用接口 * 一个getArea的方法计算面积 * */ public class Circle implements Shape { double r; public double getR() { return r; } public void setR(double r) { this.r = r; } Circle(double r){ this.r=r; } Circle(){ r=0; } @Override public double getArea() { // TODO Auto-generated method stub return Math.PI*r*r; } }
6.Trapezoid.java
package Thirtenth; /* * Trapezoid类使用接口 * 一个getArea的方法计算面积 * */ public class Trapezoid implements Shape { double a; double b; double h; public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public double getH() { return h; } public void setH(double h) { this.h = h; } Trapezoid(double a, double b, double h) { this.a = a; this.b = b; this.h = h; } Trapezoid() { a = 0; b = 0; h = 0; } @Override public double getArea() { // TODO Auto-generated method stub return (a + b) * h / 2; } }
7.Cone.java
package Thirtenth; /* * Cone类用来执行换底操作 * getV方法来计算体积 * */ public class Cone { Shape shape; double high; public Shape getShape() { return shape; } public void setShape(Shape shape) { this.shape = shape; } public double getHigh() { return high; } public void setHigh(double high) { this.high = high; } Cone(Shape shape, double high) { this.shape = shape; this.high = high; } Cone() { shape = null; high = 0; } public double getV() { return shape.getArea() * high; } }
8.Factory.java
package Thirtenth; /* * 简单工厂模式 * 根据输入的不同字符来对各种情况下的选择做输出 * */ import java.util.Scanner; public class Factory { Shape shape = null; Scanner sc = new Scanner(System.in); Cone cone = new Cone(); Shape getShape(char c) { switch (c) { case 'J': System.out.println("请输入以矩形为底的长:"); double Ja = sc.nextDouble(); System.out.println("请输入以矩形为底的宽:"); double Jb = sc.nextDouble(); shape = new Rectiangle(Ja, Jb); break; case 'Z': System.out.println("请输入以正方形为底的长:"); double Za = sc.nextDouble(); shape = new Square(Za); break; case 'S': System.out.println("请输入以三角形为底的边a:"); double Sa = sc.nextDouble(); System.out.println("请输入以三角形为底的边b:"); double Sb = sc.nextDouble(); System.out.println("请输入以三角形为底的边c:"); double Sc = sc.nextDouble(); shape = new Triangle(Sa, Sb, Sc); break; case 'Y': System.out.println("请输入以圆形为底的半径:"); double Yr = sc.nextDouble(); shape = new Circle(Yr); break; case 'T': System.out.println("请输入以梯形为底的上边a:"); double Ta = sc.nextDouble(); System.out.println("请输入以梯形为底的下边b:"); double Tb = sc.nextDouble(); System.out.println("请输入以梯形为底的高:"); double Th = sc.nextDouble(); shape = new Trapezoid(Ta, Tb, Th); break; } return shape; } }
9.Test.java
package Thirtenth; /* * 主方法创建对象并获取键盘录入值最后输出结果体积 * */ import java.util.Scanner; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); Factory fac = new Factory(); System.out.println("请输入要选择为底的图形模块:"); System.out.println("1.正方形 2.矩形 3.三角形 4.圆形 5.梯形 (输入首字母即可)"); while (true) { char c = sc.next().charAt(0); if (c != 'E') { System.out.println("请输入柱体的高度:"); double h = sc.nextDouble(); Cone cone = new Cone(fac.getShape(c), h); System.out.println(cone.getV()); } else { System.out.println("退出循环选择,感谢使用。"); return; } } } }
三、运行结果