怎样定义一个圆类,成员变量是圆心和半径,方法是求周长和面积
怎样定义一个圆类,成员变量是圆心和半径,方法是求周长和面积
怎样定义一个圆类,成员变量是圆心和半径,方法是求周长和面积
2016-04-12
package com.imooc;
import java.util.Scanner;
public class Circle {
/*
* 关于圆的计算问题:
*
* */
final double PI=3.14;//用final修饰,代表常量值,不可改变
//创建一个输入的对象、动态给定值(因为有两个方法,将这个定成全局的)
Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
//实例化对象
Circle cl=new Circle();
//调用方法执行
cl.getZC();//计算周长
cl.getAcreage();//计算面积
}
//创建一个计算的周长的方法(2*PI*r)
public void getZC(){
//提示输入
System.out.println("请输入圆的半径:");
//定义圆的半径
int r=sc.nextInt();
//定义一个变量计算周长
double zc=2*PI*r;
System.out.println("圆的周长为:"+zc);
}
//创建一个计算半径的方法
public void getAcreage(){
//提示输入
System.out.println("请输入圆的半径:");
//定义圆的半径
int r=sc.nextInt();
//定义一个变量计算周长
double Acreage=PI*r*r;
System.out.println("圆的面积为:"+Acreage);
}
}
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public static void main(String[] args) {
Circle c = new Circle(1.28);
System.out.printf("圆的周长: %.2f\n", c.getPerimeter());
System.out.printf("圆的面积: %.2f\n", c.getArea());
}
}
import java.io.PipedInputStream; import org.omg.CORBA.PUBLIC_MEMBER; public class Circle { public static void main(String[] args) { // 怎样定义一个圆类,成员变量是圆心和半径,方法是求周长和面积 int R = 8; double Pi = 3.14; Circle circle = new Circle(); System.out.println("周长: " + circle.zhouchang(R, Pi)); System.err.println("面积: " + circle.mianji(Pi, R)); } public double zhouchang(int R, double Pi) { double zhouchang = 2 * R * Pi; return zhouchang; } public double mianji(double Pi, int R) { double mianji = Pi * R * R; return mianji; } }
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 );
}
}
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
double π = 3.14;
double r;
double l;
double s;
Scanner sc = new Scanner(System.in);
sc.useDelimiter("\n");
while(sc.hasNext()){
r=Double.parseDouble(sc.nextLine());
l=2*Math.PI*r;;
s=Math.PI*r*r;;
System.out.println("周长是:" + l);
System.out.println("面积是:" + s);
}
}
}
class A{
private float r;
public float getR(){
return r;
}
public void setR(float r){
this.r = r;
}
public float Area(){
return (float)(3.14*r*r);
}
public float Circumf(){
return (float)3.14*2*r;
}
}
public class Circle {
public static void main(String[] args) {
A a = new A();
a.setR(4);
System.out.println(a.Area());
System.out.println(a.Circumf());
}
}
//创建圆型类
class circle
{
//创建 算周长方式 传出半径算周长
public double void setPerimeter( double radii )
{
//
double π = 3.14159265358979323846;
double perimeter= radii *2*π ;
retuan perimeter ;
}
//创建 算周长方式 传出半径算面积
public double void setAcreage( double radii )
{ double π = 3.14159265358979323846;
double acreage= radii *radii *π ;
retuan acreager ;
}
}
举报