设计一个计算工具类,要求:
(1)为用户提供几个常见的几何体面积和体积的算法(如球的体积和表面积,圆柱体的体积和表面积,圆锥体的体积和表面积)
(2)设计一个测试类,给出几个不同的圆柱体,圆锥体,球体的尺寸,用计算工具类分别求它们体积的累加和与表面积的累加和。
(3)设计一个测试类,随机产生球,圆柱、圆锥体的尺寸,模拟考试,要求用户回答它们的体积和面积,并判断用户的回答是否正确。
思路:为了方便用户使用,这里利用多态的特性,写一个父类或者接口来实现。
package pro3; import java.util.*; public class Graphic01{ public static void main(String[] args) { Graphic g[]=new Graphic[3]; g[0]=new Ball(3); g[1]=new Cone(3,4); g[2]=new Cylinder(3,4); Test01 t1=new Test01(); t1.sum(g,3); Test02 t2=new Test02(); t2.judge(); } } interface Graphic { //表面积 double SurfaceArea(); //体积 double Volume(); } //圆柱 class Cylinder implements Graphic{ double r=0,h=0; public Cylinder(){ Scanner sc=new Scanner(System.in); System.out.println("请输入半径:"); double r=sc.nextDouble(); System.out.println("请输入高:"); double h=sc.nextDouble(); } public Cylinder(double r,double h){ this.r=r; this.h=h; } public double SurfaceArea() { double surfaceArea=2*Math.PI*r*r+2*Math.PI*r*h; // System.out.println("表面积:"+surfaceArea); return surfaceArea; } public double Volume() { double volume=Math.PI*r*r*h; // System.out.println("体积:"+volume); return volume; } } //圆锥 class Cone implements Graphic{ private double r=0,h=0; public Cone(){ Scanner sc=new Scanner(System.in); System.out.println("请输入半径:"); double r=sc.nextDouble(); System.out.println("请输入高:"); double h=sc.nextDouble(); } public Cone(double r,double h){ this.r=r; this.h=h; } public double SurfaceArea() { double surfaceArea=Math.PI*r*r+Math.PI*r*Math.sqrt(r*r+h*h); // System.out.println("表面积:"+surfaceArea); return surfaceArea; } public double Volume() { double volume=(Math.PI*r*r*h)/3; // System.out.println("体积:"+volume); return volume; } } //球 class Ball implements Graphic{ private double r=0; public Ball(){ Scanner sc=new Scanner(System.in); System.out.println("请输入半径:"); double r=sc.nextDouble(); } public Ball(double r){ this.r=r; } public double SurfaceArea() { double surfaceArea=Math.PI*r*r*4; // System.out.println("表面积:"+surfaceArea); return surfaceArea; } public double Volume() { double volume=(Math.PI*r*r*r)*4/3; // System.out.println("体积:"+volume); return volume; } } class Test01{ public void sum(Graphic g[],int n){ double sumA=0,sumV=0; for(int i=0;i<n;i++){ sumA+=g[i].SurfaceArea(); sumV+=g[i].Volume(); } System.out.println("表面积和:"+sumA+" 体积和是:"+sumV); } } class Test02{ Graphic g[]=new Graphic[3]; double side01=random(100); double side02=random(100); double side03=random(100); double side04=random(100); double side05=random(100); double ball_a,ball_v,zhu_a,zhu_v,zhui_a,zhui_v; double ball_aa,ball_vv,zhu_aa,zhu_vv,zhui_aa,zhui_vv; public Test02(){ g[0]=new Ball(side01); g[1]=new Cylinder(side02,side03); g[2]=new Cone(side04,side05); System.out.println("\n\n\t求几何体的表面积 和 体积:"); } private double random(int n){ Random random=new Random(); double i=(double)random.nextInt(n)/10+1; return i; } private void print(){ System.out.printf("球的半径为%f\n",side01); System.out.printf("圆柱底面的半径为%f ,高为%f \n",side02,side03); System.out.printf("圆锥底面的半径为%f ,高为%f \n",side04,side05); } private void calculate(){ ball_a=g[0].SurfaceArea(); ball_v=g[0].Volume(); zhu_a=g[1].SurfaceArea(); zhu_v=g[1].Volume(); zhui_a=g[2].SurfaceArea(); zhui_v=g[2].Volume(); System.out.printf("正确答案:\n\t%f,%f,%f,%f,%f,%f\n",ball_a,ball_v,zhu_a,zhu_v,zhui_a,zhui_v); } private void user_calculate(){ Scanner sc=new Scanner(System.in); System.out.print("请计算球的表面积:"); ball_aa=sc.nextDouble(); System.out.print("请计算球的体积:"); ball_vv=sc.nextDouble(); System.out.print("请计算圆柱的表面积:"); zhu_aa=sc.nextDouble(); System.out.print("请计算圆柱的表面积:"); zhu_vv=sc.nextDouble(); System.out.print("请计算圆锥的表面积:"); zhui_aa=sc.nextDouble(); System.out.print("请计算圆锥的表面积:"); zhui_vv=sc.nextDouble(); } public boolean compare(double a,double b){ if(Math.abs(a-b)<=0.01) return true; else return false; } public void judge(){ print(); calculate(); user_calculate(); if(compare(ball_a,ball_aa)){ System.out.println("球的表面积计算正确"); }else{ System.out.println("球的表面积计算错误"); }if(compare(ball_v,ball_vv)){ System.out.println("球的体积计算正确"); }else{ System.out.println("球的体积计算错误"); } if(compare(zhu_a,zhu_aa)){ System.out.println("圆柱的表面积计算正确"); }else{ System.out.println("圆柱的表面积计算错误"); }if(compare(zhu_v,zhu_vv)){ System.out.println("圆柱的体积计算正确"); }else{ System.out.println("圆柱的体积计算错误"); } if(compare(zhui_a,zhui_aa)){ System.out.println("圆锥的表面积计算正确"); }else{ System.out.println("圆锥的表面积计算错误"); }if(compare(zhui_v,zhui_vv)){ System.out.println("圆锥的体积计算正确"); }else{ System.out.println("圆锥的体积计算错误"); } } }