第9次作业--接口及接口回调
题目:
利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
代码:
1 package y; 2 // 图形接口 3 public interface Shape { 4 double getArea(); 5 }
1 package y; 2 //圆类重写图形接口,重写面积方法 3 public class Circle implements Shape { 4 double r; 5 public Circle(double r){ 6 this.r=r; 7 } 8 @Override 9 public double getArea() { 10 // TODO Auto-generated method stub 11 return Math.PI*r*r; 12 } 13 14 }
1 package y; 2 //矩形类重写图形接口,重写面积方法 3 public class Rectangle implements Shape { 4 double width; 5 double length; 6 public Rectangle(double width, double length) { 7 this.width = width; 8 this.length = length; 9 } 10 @Override 11 public double getArea() { 12 // TODO Auto-generated method stub 13 return width * length; 14 } 15 16 17 }
1 package y; 2 //圆类重写图形接口,重写面积方法 3 public class Trapezoid implements Shape{ 4 double s; 5 double x; 6 double h; 7 public Trapezoid(double s, double x,double h) { 8 this.s =s; 9 this.x =x; 10 this.h = h; 11 } 12 @Override 13 public double getArea() { 14 // TODO Auto-generated method stub 15 return (s+x)*h/2.0; 16 } 17 18 }
1 package y; 2 //三角形类重写图形接口,重写面积方法 3 public class Triangle implements Shape { 4 double a; 5 double b; 6 double c; 7 public Triangle(double a,double b,double c){ 8 this.a=a; 9 this.b=b; 10 this.c=c; 11 } 12 @Override 13 public double getArea() { 14 // TODO Auto-generated method stub 15 double p=(a+b+c)/2; 16 return Math.sqrt(p*(p-a)*(p-b)*(p-c)); 17 } 18 19 }
1 package y; 2 //正方形类继承矩形类 3 public class Zheng extends Rectangle { 4 public Zheng(double side) { 5 super(side, side); 6 } 7 @Override 8 public double getArea() { 9 // TODO Auto-generated method stub 10 return width * width; 11 } 12 13 }
1 package y; 2 //柱类,包括构造方法,求体积方法,和换底方法 3 public class Cone { 4 Shape shape; 5 double high; 6 7 public Cone(Shape shape, double high) { 8 this.shape = shape; 9 this.high = high; 10 } 11 public double getVolume() { 12 return shape.getArea()*high; 13 } 14 public void setShape(Shape shape) 15 { 16 this.shape=shape; 17 } 18 }
1 package y; 2 3 import java.util.Scanner; 4 //工厂类 5 public class Foctory { 6 public Shape getShape(char c) { 7 Scanner reader = new Scanner(System.in); 8 Shape shape = null; 9 switch (c) { 10 case 'R': 11 System.out.println("请输入矩形的长和宽、柱体的高"); 12 shape = new Rectangle(reader.nextInt(), reader.nextDouble()); 13 break; 14 case 'S': 15 System.out.println("请输入三角形的边柱体的高"); 16 shape = new Triangle(reader.nextDouble(), reader.nextDouble(), reader.nextDouble()); 17 break; 18 case 'C': 19 System.out.println("请输入圆半径,柱体的高"); 20 shape = new Circle(reader.nextDouble()); 21 break; 22 case 'T': 23 System.out.println("请输入梯形的上底下底和高"); 24 shape = new Trapezoid(reader.nextDouble(), reader.nextDouble(), reader.nextDouble()); 25 break; 26 case 'Z': 27 System.out.println("请输入正方形的边长,柱体的高"); 28 shape = new Zheng(reader.nextDouble()); 29 break; 30 } 31 return shape; 32 } 33 }
package h; import java.util.Scanner; import y.Cone; import y.Foctory; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub while(true){ Scanner reader = new Scanner(System.in); System.out.println("矩形R,三角形S,圆C,梯形T,正方形Z"); char c = reader.next().charAt(0); Foctory f = new Foctory(); Cone cone = new Cone(f.getShape(c), reader.nextDouble()); System.out.println(cone.getVolume()); } } }
运行