标签:棱柱 java double 面向对象 shape reader 可换底 public nextDouble
题目:计算棱柱体积可换底
一、源程序
1、Test.java
/**计算棱柱体积可换底 * 9个类 */ package w29; import java.util.Scanner; public class Test { public static void main(String[] args) { while(true){ Scanner reader = new Scanner(System.in); System.out.println("请输入柱体底的形状:r(矩形);s(正方形);t(梯形);T(三角形);c(圆形)。"); char c = reader.next().charAt(0); Factory factory = new Factory(); Shape shape; Column column = new Column(factory.getShape(c),reader.nextDouble()); System.out.println("柱体的体积"+column.getVolume()); } } }
2、Factory.java
package w29; //工厂类 import java.util.Scanner; public class Factory { protected Shape getShape(char c){ Scanner reader = new Scanner(System.in); Shape shape = null; switch(c){ case 'r':System.out.println("请输入矩形底的长和宽、柱体的高");shape = new Rectangle(reader.nextDouble(),reader.nextDouble());break; case 's':System.out.println("请输入正方形底的边、柱体的高");shape = new Square(reader.nextDouble());break; case 't':System.out.println("请输入梯形底的上下底和高、柱体的高");shape = new trapezium(reader.nextDouble(),reader.nextDouble(),reader.nextDouble());break; case 'T':System.out.println("请输入三角形底的三边、柱体的高");shape = new Triangle(reader.nextDouble(),reader.nextDouble(),reader.nextDouble());break; case 'c':System.out.println("请输入圆形底的半径、柱体的高");shape = new Circle(reader.nextDouble());break; } return shape; } }
3、Shape.java
package w29; //形状接口 public interface Shape { double getArea(); }
4、Rectangle.java
package w29; //矩形类 public class Rectangle implements Shape { double width,length; public Rectangle(double width,double length){ this.width = width; this.length = length; } public double getArea() { return width*length; } }
5、Square.java
package w29; //正方形类 public class Square extends Rectangle { public Square(double side) { super(side, side); } public double getArea() { return width*length; } }
6、trapezium.java
package w29; //梯形类 public class trapezium extends Rectangle { double height; public trapezium(double up, double down, double height) { super(up, down); this.height = height; } public double getArea() { return (width+length)*height/2; } }
7、Triangle.java
package w29; //三角形类 public class Triangle implements Shape { double a,b,c; public Triangle(double a, double b, double c) { super(); this.a = a; this.b = b; this.c = c; } public double getArea() { double sum = (a+b+c)/2; return Math.sqrt(sum*(sum-a)*(sum-b)*(sum-c)); } }
8、Circle.java
package w29; //圆形类 public class Circle implements Shape { double radius; public Circle(double radius) { super(); this.radius = radius; } public double getArea() { // TODO Auto-generated method stub return radius*radius*3.14; } }
9、Column.java
package w29; //柱体类 public class Column { Shape shape; double high; public Column(Shape shape, double high) { super(); this.shape = shape; this.high = high; } public double getVolume(){ return shape.getArea()*high; } }
二、成功界面截图
标签:棱柱,java,double,面向对象,shape,reader,可换底,public,nextDouble 来源: https://www.cnblogs.com/wangxiangyue/p/11609235.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。