Home  >  Article  >  Java  >  How to write code to find the area of ​​a circle in java

How to write code to find the area of ​​a circle in java

WBOY
WBOYforward
2023-04-29 20:52:053933browse

1. The process of finding a circle

Create a circle Circle class.

Provide the variable r for this class to represent the radius, and the constant PI to represent the pi ratio.

Provide class methods: find the area of ​​a circle.

Provide no-argument structure method for this class, and initialize r value to 4.

Test the main method.

2. Example

package hello.circle;
 
/**
 * 创建一个圆Circle类。
 * 为该类提供一个变量r表示半径,一个常量PI表示圆周率;
 * 同时为该类提供方法:用于求圆的面积;
 * 为该类提供一个无参的构造方法,用于初始化r的值为4。
 * 在main方法中测试。
 */
//创建一个圆Circle类
public class Circle {
 
    //为该类提供一个变量r表示半径,一个常量PI表示圆周率
    public double r;
    public final double PI = 3.14;
 
    //为该类提供一个无参的构造方法,用于初始化r的值为4。
    public Circle() {
        System.out.println("无参数的构造函数:为R赋值为 4 ---");
        r = 4;
    }
 
    //用于求圆的面积
    public void area() {
        System.out.println(r);
        System.out.println("圆的面积为:" + PI * r * r);
    }
    // main方法
    public static void main(String[] args) {
 
        System.out.println("-----");
        Circle c = new Circle();
        System.out.println("-----");
 
        c.area();
 
        c.girth();
    }
}

The above is the detailed content of How to write code to find the area of ​​a circle in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete