本文主要是介绍Java计算以三角形,矩形,圆形为底的柱形的体积,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java计算以三角形,矩形,圆形为底的柱形的体积
代码如下:
Geometry.java:
public abstract class Geometry {public abstract double getArea();
}
Circle.java:
public class Circle extends Geometry{double i;Circle(double i){this.i=i;}public double getArea() {return 3.14*i*i;}}
Rectangle.java:
public class Rectangle extends Geometry{double i,j;Rectangle(double i,double j){this.i=i;this.j=j;}public double getArea() {return i*j;}}
Triangle.java:
public class Triangle extends Geometry{double i,j;Triangle(double i,double j){this.i=i;this.j=j;}public double getArea() {return i*j/2;}
}
Pillar.java:
public class Pillar {Geometry bottom;double height;Pillar(Geometry bottom,double height){this.bottom=bottom;this.height=height;}public double getVolume() {if(bottom==null) {System.out.println("没有底,无法计算体积");return -1;}return bottom.getArea()*height;}
}
text.java:
public class text {public static void main(String[] args) {Pillar pillar;Geometry bottom=null;pillar=new Pillar(bottom,100);System.out.println(pillar.getVolume());bottom=new Circle(3);pillar=new Pillar(bottom,100);System.out.println(pillar.getVolume());bottom=new Rectangle(3,4);pillar=new Pillar(bottom,100);System.out.println(pillar.getVolume());bottom=new Triangle(3,4);pillar=new Pillar(bottom,100);System.out.println(pillar.getVolume());}
}
运行结果:
这篇关于Java计算以三角形,矩形,圆形为底的柱形的体积的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!