定义一个圆类Circle,成员变量:半径 radius;成员方法:构造方法、get和set半径的方法、计算面积和周长的方法。定义圆柱和圆锥类,定义相应的变量成员和成员方法。使用以上类编程,输出圆的面

Circle类

public class Circle {

	private double radius;
	
	public Circle() {}
	
	public Circle(double radius) {
		this.radius = radius;
	}
	
	public double getRadius() {
		return radius;
	}
	
	public void setRadius(double radius) {
		this.radius = radius;
	}
	
	public double getArea(double r) {
		return Math.PI * Math.pow(r,2);
	}
}

Cone类


public class Cone {

	private double h;
	private double r;
	
	public Cone() {
	}
	public Cone(double h, double r) {
		this.h = h;
		this.r = r;
	}
	
	public double getH() {
		return h;
	}
	
	public void setH(double h) {
		this.h = h;
	}
	
	public double getR() {
		return r;
	}
	
	public void setR(double r) {
		this.r = r;
	}
	
	public double getVolume(double h, double r) {
		return Math.PI * Math.pow(r,2)*h;
	}
}

 Cylinder类

public class Cylinder {

	private double h;
	private double r;
	
	public Cylinder() {
	}
	
	public Cylinder(double h, double r) {
		this.h = h;
		this.r = r;
	}
	
	public double getH() {
		return h;
	}
	
	public void setH(double h) {
		this.h = h;
	}
	
	public double getR() {
		return r;
	}
	
	public void setR(double r) {
		this.r = r;
	}
	
	public double getVolume(double h, double r) {
		return Math.PI * Math.pow(r,2)*h/3;
	}
}

Test类

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

public class Test {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		Circle c1 = new Circle();
		Cone c2 = new Cone();
		Cylinder c3 = new Cylinder();
		
		System.out.println("圆的面积");
		System.out.println("输入圆的半径");
		double r1 = sc.nextDouble();
		System.out.printf("%.4f\n", c1.getArea(r1));
		
		System.out.println("圆柱的体积");
	    System.out.println("请输入圆柱的半径:");
	    double r2 = sc.nextDouble();      
	    System.out.println("请输入圆柱的高:");
	    double h2 = sc.nextDouble();
        System.out.printf("%.4f\n", c2.getVolume(h2, r2));
      
        System.out.println("圆锥的体积");
        System.out.println("请输入圆锥的半径:");
	    double r3 = sc.nextDouble();
        System.out.println("请输入圆锥的高:");
        double h3 = sc.nextDouble();
	    System.out.printf("%.4f\n", c3.getVolume(h3, r3));

	
	}
}

你可能感兴趣的:(java,前端,开发语言)