编写一个圆类Circle,该类拥有:
①一个成员变量
Radius(私有,浮点型); // 存放圆的半径;
②两个构造方法
Circle( ) // 将半径设为0
Circle(double r ) //创建Circle对象时将半径初始化为r
③ 三个成员方法
double getArea( ) //计算圆的面积
double getPerimeter( ) //计算圆的周长
void show( ) //将圆的半径、周长、面积输出到屏幕
编写应用程序,创建类的对象,分别设置圆的半径计算并分别显示圆半径、圆面积、圆周长。
package com.Summer_0511.cn; /**
* @author Summer
*
*/
class Circle{
private double Radius; public Circle() {
super();
Radius=0;
} public Circle(double r) {
super();
this.Radius = r;
} public double getArea(double r){
double Area = r*r*3.14;
return Area;
} public double getPerimeter(double r){
double Perimeter = 2*3.14*r;
return Perimeter;
} public void show(double r){
System.out.println("圆的半径为"+r);
System.out.println("圆的面积为"+getArea(r));
System.out.println("圆的周长为"+getPerimeter(r));
} }
public class Test04 { public static void main(String[] args) {
Circle c = new Circle();
c.show(3); } }
方法二:
package stage3;
class Circle{
private double radius;//半径
private final double PI = 3.24d;
public Circle(double radius) {
this.radius = radius;
} public Circle() {
this.radius = 0;
}
public double getArea() {
return PI*Math.pow(this.radius, 2);
}
public double getPerimeter() {
return 2* PI * this.radius;
}
public void show() {
System.out.println(this.radius+","+getPerimeter()+","+getArea());
} }
public class Test2 { public static void main(String[] args) { Circle c = new Circle(2);
c.show();
} }