日博官网

当前位置: > 日博官方 >

用java寻求圆的面积与周长

时间:2018-10-10 02:20来源:未知 作者:admin 点击:
class Circle { private int r; public Circle(int r){ this.r=r; } public int getr(){ return r; } public double getArea(){ return Math.PI*r*r; } public double getCircleLength(){ return Math.PI*2*r; } } public class CircleComputer{ public stati

  class Circle {

  private int r;

  public Circle(int r){

  this.r=r;

  }

  public int getr(){

  return r;

  }

  public double getArea(){

  return Math.PI*r*r;

  }

  public double getCircleLength(){

  return Math.PI*2*r;

  }

  }

  public class CircleComputer{

  public static void main(String []args){

  Circle a=new Circle(1);

  System.out.println("圆半径r="+a.getr());

  System.out.println("圆面积为:"+a.getArea());

  System.out.println("圆周长为:"+a.getCircleLength());

  }

  }

  public class Test {

  public static void main(String[] g) {

  Circle circle=new Circle(1);

  System.out.println("半径 "+circle.getR());

  System.out.println("面积 "+circle.area());

  System.out.println("周长 "+circle.circleLength());

  }

  }

  class Circle {

  private int r;

  //构造函数

  public Circle(int r) {

  this.r=r;

  }

  //获取半径

  public int getR() {

  return r;

  }

  //计算圆面积

  public double area() {

  return r * r * Math.PI;

  }

  //计算圆周长

  public double circleLength() {

  return 2 * r * Math.PI;

  }

  }

  public class Square

  {

  private double radius;//私拥有成员变量

  public Square()//构造函数

  {

  this.radius=6.0;

  }

  public double Getr()//读取半径

  {

  return radius;

  }

  public void getArrer()//计算面积

  {

  System.out.print(radius*radius*3.14);

  }

  public void getcirclelength()//计算周长

  {

  System.out.print(2*radius*3.14);

  }

  }

  public class NewMain {//主函数

  public static void main(String[] args)

  {

  Square sq=new Square();

  System.out.print("圆的半径为:"+sq.Getr());

  System.out.print("面积为:");

  sq.getArrer();

  System.out.print("周长:");

  sq.getcirclelength();

  }

  }

  public class Circle {

  private int r;

  public Circle(int r) {

  this.r=r;

  }

  public int getR() {

  return r;

  }

(责任编辑:admin)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
推荐内容