java实现circle类求圆的面积_怎样定义一个圆类,成员变量是圆心和半径,方法是求周长和面积...

package com.basic;

import com.basic.InnerClass.Inner;

/**

* 圆类,以圆心和半径来区分两个圆是否相同。

*

* @author Administrator

*/

public class Circle

{

private static final int CONSTANT_TWO = 2;

/**

* 圆心

*/

private String centerPoint;

/**

* 半径

*/

private double radius;

/**

* 无参构造器

*/

public Circle()

{

}

/**

* 有参构造器

*

* @param centerPoint

*            圆心(可以继续封装成坐标)

* @param radius

*            半径

*/

public Circle( String centerPoint,double radius )

{

super();

this.centerPoint = centerPoint;

this.radius = radius;

}

/**

* 获取圆心值

*

* @return 圆心值

*/

private String getCenterPoint()

{

return centerPoint;

}

/**

* 设置圆心值

*

* @param centerPoint

*/

private void setCenterPoint( String centerPoint )

{

this.centerPoint = centerPoint;

}

/**

* 获取圆半径

*

* @return

*/

private double getRadius()

{

return radius;

}

/**

* 设置圆半径

*

* @param radius

*/

private void setRadius( long radius )

{

this.radius = radius;

}

/**

* 计算圆周长

*

* @return

*/

public double caculatePerimeter()

{

return Math.PI * this.radius * CONSTANT_TWO;

}

/**

* 计算圆面积

*

* @return

*/

public double caculateArea()

{

return Math.PI * this.radius * this.radius;

}

/**

* 重写圆类的toString方法

*/

@Override

public String toString()

{

return "Circle [centerPoint=" + centerPoint + ", radius=" + radius

+ "]";

}

@Override

public int hashCode()

{

final int prime = 31;

int result = 1;

result = prime * result

+ ((centerPoint == null) ? 0 : centerPoint.hashCode());

long temp;

temp = Double.doubleToLongBits( radius );

result = prime * result + (int)(temp ^ (temp >>> 32));

return result;

}

@Override

public boolean equals( Object obj )

{

if ( this == obj )

return true;

if ( obj == null )

return false;

if ( getClass() != obj.getClass() )

return false;

Circle other = (Circle)obj;

if ( centerPoint == null )

{

if ( other.centerPoint != null )

return false;

} else if ( !centerPoint.equals( other.centerPoint ) )

return false;

if ( Double.doubleToLongBits( radius ) != Double

.doubleToLongBits( other.radius ) )

return false;

return true;

}

public static void main( String[] args )

{

Circle circle = new Circle( "a1", 10 );

System.out.println( circle );

double perimeter = circle.caculatePerimeter();

System.out.println( circle + "的周长为:" + perimeter );

double area = circle.caculateArea();

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

}

}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值