Home > Java > javaTutorial > 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
Release: 2023-04-29 20:52:05
forward
4019 people have browsed it

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();
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template