欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

圆锥体的表面积和体积计算

最编程 2024-07-05 22:38:40
...

1. 定义Area  和 Volume 连个接口

2. 计算圆锥的表面积和体积

3. 按体积的大小判断圆锥的大小

package zuo_ye4;


import java.util.Scanner;
import java.util.ArrayList;

public class Cone implements Area, Volume {
    double r;       //底面圆的半径
    double r1;      //侧面扇形的母线
    double l;       // 底面圆的周长
    double h;       //圆锥底面圆到顶点的距离(高)

    double s;       //圆锥表面积
    double v;       // 圆锥体积

    public double getR1() {
        return r1;
    }

    public void setR1(double r1) {
        this.r1 = r1;
    }

    public double getR() {
        return r;
    }

    public double getV() {
        return v;
    }

    public void setV(double v) {
        this.v = v;
    }

    public void setR(double r) {
        this.r = r;
    }



    public double getL() {
        return l;
    }

    public void setL(double l) {
        this.l = l;
    }

    public double getH() {
        return h;
    }

    public void setH(double h) {
        this.h = h;
    }

    public double getS() {
        return s;
    }

    public void setS(double s) {
        this.s = s;
    }

    @Override
    public void area(int k) {
        // 底部面积+侧面积
        // (1)底面圆面积 r*r*PI   (2) 底面圆的周长   2*r*PI  (3) 侧面积 1/2*l*R
        int i=1;
        System.out.println("第 "+k+" 个圆锥");
        double s1 = this.r * this.r * this.PI;
        this.l=this.r*PI*2;
        double s2 = this.l*this.r1;
        this.s=s1+s2;
        System.out.printf("底面圆面积为:  %.2f\n",s1);
        System.out.printf("圆锥侧面扇形积为:  %.2f\n",s2);
        System.out.printf("圆锥表面积为:  %.2f\n",this.s);
    }

    @Override
    public void volume() {
        // 底面积 x 高 x 1/3
        // 体积计算
        double s1 = this.r * this.r * this.PI ;
        this.v = (s1 *this.h)*1/3;

    }


        //体积比较VS打印体积
    public void bvb(ArrayList<Cone> arr){
        System.out.println("-------计算圆锥的体积-------");
        Cone c =arr.get(1);
        if (this.v==c.getV()){
            System.out.printf("第一个圆锥的体积为: %.2f\n第二个圆锥的体积为: %.2f\n",this.v,c.getV());
            System.out.println("提示: 两个圆锥相等");
        }else if(this.v > c.getV()){
            System.out.printf("第一个圆锥的体积为: %.2f\n第二个圆锥的体积为: %.2f\n",this.v,c.getV());
            System.out.println("提示: 第一个圆锥大于第二个圆锥");
        }else{
            System.out.printf("第一个圆锥的体积为: %.2f\n第二个圆锥的体积为: %.2f\n",this.v,c.getV());
            System.out.println("提示:第一个圆锥小于第二个圆锥");
        }
    }


    public static void main(String[] args) {
        ArrayList<Cone> arr = new ArrayList<>();
        input(arr);
        System.out.println("-------计算圆锥表面积-------");
        int k =1;
        for (Cone c1 :arr) {
            c1.area(k);
            c1.volume();
            k++;
        }
       Cone c2=arr.get(0);
        c2.bvb(arr);
    }

    public static void input(ArrayList<Cone> arr){
        System.out.println("-------请输入圆锥的已知条件!!-------");
        Scanner scan =new Scanner(System.in);
        int i =1;
        while(i<3) {
            System.out.println("请输入第 "+i+" 圆锥的已知条件");
            System.out.print("输入底部圆的半径 r :");
            Cone c = new Cone();
            double r = scan.nextDouble();
            c.setR(r);
            System.out.print("输入底部到圆锥顶点的高 h :");
            double h = scan.nextDouble();
            c.setH(h);
            System.out.print("输入侧面扇形的母线 r1 :");
            double r1 = scan.nextDouble();
            c.setR1(r1);
            arr.add(c);
            i++;
            System.out.println();
        }
    }
}

// 接口 面积

interface Area {
    public static final double PI = 3.14;
    public abstract void area(int k);
}

// 接口体积

interface Volume {
    public abstract void volume();
}

推荐阅读