亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

怎樣定義一個圓類,成員變量是圓心和半徑,方法是求周長和面積

怎樣定義一個圓類,成員變量是圓心和半徑,方法是求周長和面積

正在回答

8 回答

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);

}


}


0 回復 有任何疑惑可以回復我~

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());
? ?}
}

0 回復 有任何疑惑可以回復我~
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;
		
	}
	
}


0 回復 有任何疑惑可以回復我~

public class Yuan

{


? ? public static void main(String[] args)

? ? {

? ? ? ? // TODO 自動生成的方法存根

? ? ? ? double r=4;// 半徑


? ? ? ? double L=2 * Math.PI * r;

? ? ? ? double S=Math.PI * r * r;

? ? ? ? System.out.println("圓周長是:" + L);

? ? ? ? System.out.println("圓面積是:" + S);


? ? }


}


0 回復 有任何疑惑可以回復我~



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 );
??? }
}



1 回復 有任何疑惑可以回復我~

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);
?? ??? ?}
?? ??? ??? ?
?? ?}
?? ?
}

0 回復 有任何疑惑可以回復我~
#1

森特

前面定義的π可以刪了,多寫了
2016-04-12 回復 有任何疑惑可以回復我~

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());

????????}

}


0 回復 有任何疑惑可以回復我~

//創建圓型類

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 ;

? ? ?}

}

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

怎樣定義一個圓類,成員變量是圓心和半徑,方法是求周長和面積

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號