第9次作业--接口及接口回调
题目:
利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。
图形接口
package com.ccut.java191007; /** * 图形接口 * @author DELL * */ public interface Shape { /** * 求图形面积 * @return */ double getArea(); }
以下为:圆形类、正方形类、矩形类、三角形类、梯形类
package com.ccut.java191007; /** * 圆类,实现Shape接口 * @author DELL * */ public class Circle implements Shape{ private double radius; private static double PI=3.1415926; /** * 圆形构造方法 * @param radius */ public Circle(double radius) { // TODO Auto-generated constructor stub this.radius=radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public static double getPI() { return PI; } /** * 求圆形面积 */ @Override public double getArea() { // TODO Auto-generated method stub return PI*Math.pow(radius, 2); } }
package com.ccut.java191007; /** * 矩形类,实现Shape接口 * @author DELL * */ public class Rectangle implements Shape{ private double length; private double weight; /** * 矩形构造方法 * @param length * @param weight */ public Rectangle(double length,double weight) { // TODO Auto-generated constructor stub this.length=length; this.weight=weight; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } /** * 求矩形面积 */ @Override public double getArea() { // TODO Auto-generated method stub return length*weight; } }
package com.ccut.java191007; /** * 正方形类,继承了矩形类 * @author DELL * */ public class Square extends Rectangle{ /** * 正方形类构造方法 * @param length */ public Square(double length) { super(length, length); // TODO Auto-generated constructor stub } }
package com.ccut.java191007; /** * 梯形类,实现了Shape接口 * @author DELL * */ public class Trapezium implements Shape{ private double t1; private double t2; private double h; /** * 梯形构造方法 * @param t1 * @param t2 * @param t */ public Trapezium(double t1,double t2,double t) { // TODO Auto-generated constructor stub this.t1=t1; this.t2=t2; this.h=h; } public double getT1() { return t1; } public void setT1(double t1) { this.t1 = t1; } public double getT2() { return t2; } public void setT2(double t2) { this.t2 = t2; } public double getH() { return h; } public void setH(double h) { this.h = h; } /** * 求梯形面积 */ @Override public double getArea() { // TODO Auto-generated method stub return (t1+t2)*h/2; } }
package com.ccut.java191007; /** * 三角形类,实现了Shape接口 * @author DELL * */ public class Triangle implements Shape{ private double edgeA; private double edgeB; private double edgeC; /** * 三角形构造方法 * @param edgeA * @param edgeB * @param edgeC */ public Triangle(double edgeA,double edgeB,double edgeC) { // TODO Auto-generated constructor stub this.edgeA=edgeA; this.edgeB=edgeB; this.edgeC=edgeC; } public double getEdgeA() { return edgeA; } public void setEdgeA(double edgeA) { this.edgeA = edgeA; } public double getEdgeB() { return edgeB; } public void setEdgeB(double edgeB) { this.edgeB = edgeB; } public double getEdgeC() { return edgeC; } public void setEdgeC(double edgeC) { this.edgeC = edgeC; } /** * 求三角形面积 */ @Override public double getArea() { // TODO Auto-generated method stub double p=(edgeA+edgeB+edgeC)/2; return Math.sqrt(p*(p-edgeA)*(p-edgeB)*(p-edgeC)); } }
柱体类
package com.ccut.java191007; /** * 柱体类 * @author DELL * */ public class Cone { Shape shape; double high; /** * 柱体构造方法 * @param shape 底面 * @param high 高 */ public Cone(Shape shape,double high) { // TODO Auto-generated constructor stub this.shape=shape; this.high=high; } /** * 求柱体体积 * @return 柱体体积 */ public double getVolume() { return high*shape.getArea(); } /** * 换底面方法 * @param shape 底面 */ public void setShape(Shape shape) { this.shape=shape; } }
Factory工厂类
package com.ccut.java191007; public class ShapeFactory { public Shape getShape(String shapeType,double a) { if (shapeType == null) return null; if (shapeType.equals("Squ")) { return (Shape) new Square(a); } else if (shapeType.equals("Cir")) { return (Shape) new Circle(a); } return null; } public Shape getShapes(String shapeType,double a,double b) { if (shapeType == null) return null; if (shapeType.equals("Rec")) { return new Rectangle(a, b); } return null; } public Shape getShapes(String shapeType,double a,double b,double c) { if (shapeType == null) return null; if (shapeType.equals("Tra")) { return new Trapezium(a,b,c); } if (shapeType.equals("Tri")) { return new Triangle(a,b,c); } return null; } }
主方法
package com.ccut.java191007; import java.util.Scanner; /** * 主方法类 * * @author DELL * */ public class Main { /** * 利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。 * * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); ShapeFactory factory = new ShapeFactory(); // 工厂类,用于生产图形的实例化对象 Shape shape; // shape接口 Cone cone = null; // 柱体对象 String shapeType; while (true) { shapeType=""; System.out.println("输入你要计算的类型(Rec,Squ,Cir,Tra,Tri):"); shapeType = in.nextLine(); if (shapeType.equals("Rec")) { System.out.println("请输入矩形的长、宽:"); double length = in.nextDouble(); double weigtht = in.nextDouble(); shape = factory.getShapes(shapeType, length, weigtht); System.out.println("矩形面积:" + shape.getArea()); System.out.println("请输入柱体的高:"); double h = in.nextDouble(); cone = new Cone(shape, h); System.out.println("柱体的体积:" + cone.getVolume()); } else if (shapeType.equals("Squ")) { System.out.println("请输入正方形的长:"); double length = in.nextDouble(); shape = factory.getShape(shapeType, length); System.out.println("正方形面积:" + shape.getArea()); System.out.println("请输入柱体的高:"); double h = in.nextDouble(); cone = new Cone(shape, h); System.out.println("柱体的体积:" + cone.getVolume()); } else if (shapeType.equals("Cir")) { System.out.println("请输入圆形的半径:"); double radius = in.nextDouble(); shape = factory.getShape(shapeType, radius); System.out.println("圆形面积:" + shape.getArea()); System.out.println("请输入柱体的高:"); double h = in.nextDouble(); cone = new Cone(shape, h); System.out.println("柱体的体积:" + cone.getVolume()); } else if (shapeType.equals("Tra")) { System.out.println("请输入梯形的上底、下底、高:"); double t1 = in.nextDouble(); double t2 = in.nextDouble(); double h = in.nextDouble(); shape = factory.getShapes(shapeType, t1, t2, h); System.out.println("梯形的面积:" + shape.getArea()); System.out.println("请输入柱体的高:"); double h_ = in.nextDouble(); cone = new Cone(shape, h_); System.out.println("柱体的体积:" + cone.getVolume()); } else if (shapeType.equals("Tri")) { System.out.println("请输入三角形的三边:"); double a = in.nextDouble(); double b = in.nextDouble(); double c = in.nextDouble(); shape = factory.getShapes(shapeType, a, b, c); System.out.println("三角形的面积:" + shape.getArea()); System.out.println("请输入柱体的高:"); double h = in.nextDouble(); cone = new Cone(shape, h); System.out.println("柱体的体积:" + cone.getVolume()); } System.out.println("换底:请输入你要换的底面(Rec,Squ,Cir,Tra,Tri):"); Scanner in_=new Scanner(System.in); String changeType=in_.nextLine(); if (changeType.equals("Rec")) { System.out.println("请输入矩形的长、宽:"); double length = in_.nextDouble(); double weigth = in_.nextDouble(); shape = factory.getShapes(changeType, length, weigth); System.out.println("矩形面积:" + shape.getArea()); cone.setShape(shape); System.out.println("柱体的体积:" + cone.getVolume()); } else if (changeType.equals("Squ")) { System.out.println("请输入正方形的长:"); double length = in_.nextDouble(); shape = factory.getShape(changeType, length); System.out.println("正方形面积:" + shape.getArea()); cone.setShape(shape); System.out.println("柱体的体积:" + cone.getVolume()); } else if (changeType.equals("Cir")) { System.out.println("请输入圆形的半径:"); double radius = in_.nextDouble(); shape = factory.getShape(changeType, radius); System.out.println("圆形面积:" + shape.getArea()); cone.setShape(shape); System.out.println("柱体的体积:" + cone.getVolume()); } else if (changeType.equals("Tra")) { System.out.println("请输入梯形的上底、下底、高:"); double t1 = in_.nextDouble(); double t2 = in_.nextDouble(); double h = in_.nextDouble(); shape = factory.getShapes(changeType, t1, t2, h); System.out.println("梯形的面积:" + shape.getArea()); cone.setShape(shape); System.out.println("柱体的体积:" + cone.getVolume()); } else if (changeType.equals("Tri")) { System.out.println("请输入三角形的三边:"); double a = in_.nextDouble(); double b = in_.nextDouble(); double c = in_.nextDouble(); shape = factory.getShapes(changeType, a, b, c); System.out.println("三角形的面积:" + shape.getArea()); cone.setShape(shape); System.out.println("柱体的体积:" + cone.getVolume()); } System.out.println("----------------------------------------------"); in.nextLine(); in_.nextLine(); } } }
运行结果

浙公网安备 33010602011771号