java编程:定义一个圆类,属性为半径,方法为对输入的半径计算其周长和面积,
public class Test {
private double r;
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
public Test(double r) {
super();
this.r = r;
}
public void MJ(){
System.out.println("圆的面积为:" + (3.14 * this.getR() * this.getR()));
}
public void ZC(){
System.out.println("圆的周长为:" + (2 * 3.14 * this.getR()));
}
public static void main(String[] args) {
Test t1 = new Test(4.5);
System.out.println("圆的半径为:" + t1.getR());
t1.MJ();
t1.ZC();
}
}
java编程定义一个类圆属性半径方法,计算周长面积设置半径读取半径急java编程定义一个类。
java编程定义一个类圆属性半径,方法计算周长面积设置半径读取半径
class Circle
{\x09private double r;
\x09public void setR(double r)
\x09{
\x09\x09this.r=r;
\x09}
\x09public double getArea()
\x09{
\x09\x09return Math.PI*r*r;
\x09}
\x09public double getPerimeter()
\x09{
\x09\x09return 2*Math.PI*r;
\x09}
public double getRadius()
{return r;
}\x09public static void main(String args[])
\x09{\x09
\x09\x09Circle c1=new Circle();
\x09\x09Circle c2=new Circle();
\x09\x09c1.setR(50);
\x09\x09c2.setR(80);
\x09\x09System.out.printf("c1面积为:%.3f\r\n",c1.getArea());
\x09\x09System.out.printf("c1周长为:%.3f\r\n",c1.getPerimeter());
\x09\x09System.out.printf("c2面积为:%.3f\r\n",c2.getArea());
\x09\x09System.out.printf("c2周长为:%.3f\r\n",c2.getPerimeter());\x09
\x09}