Java创建圆形类Circle.属性:半径r;方法:计算周长、显示周长、计算面积、显示面积,并创建测试类CircleTest

如题所述

package test;
class Circle {
private double r;
private double c = 0.0;
private double s = 0.0;
public Circle(double r) {
this.r = r;
}
/**
* 计算周长
*/
public void calcPerimeter() {
c = Math.PI * r * 2;
}
/**
* 展示周长
*/
public void showPerimeter() {
System.out.printf("周长为:%.2f", c);
System.out.println();
}
/**
* 计算面积
*/
public void calcArea() {
s = Math.PI * Math.pow(r, 2);
}
/**
* 展示面积
*/
public void showArea() {
System.out.printf("面积为:%.2f", s);
System.out.println();
}
}
public class Test {
public static void main(String[] args) {
Circle circle = new Circle(1.0);
circle.calcArea();
circle.showArea();
circle.calcPerimeter();
circle.showPerimeter();
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2019-03-21
public class Circle {

private Double r;

public Double getR() {
return r;
}

public void setR(Double r) {
this.r = r;
}

public Circle(Double r) {

this.r = r;
}

@Override
public String toString() {
return "Circle{" +
"r=" + r +
'}';
}
}
public class CircleMethod {
public static Double circlePerimeter(Circle circle) {
Double perimeter;
perimeter = 2 * circle.getR() * Math.PI;
return perimeter;
}

public static Double circleArea(Circle circle) {
Double area;
area = circle.getR() * circle.getR() * Math.PI;
return area;
}
}
public class CircleaTest {
public static void main(String[] args) {
Circle circle = new Circle(3.0);
System.out.println("圆的面积是:" + CircleMethod.circleArea(circle));
System.out.println("圆的周长是:" + CircleMethod.circlePerimeter(circle));
}
}
第2个回答  2019-03-21
class Cricle{
private Double r;

public Cricle(Double r) {
this.r = r;
}
public Double perimeter(){
Double perimeter= 2*Math.PI*this.r;
System.out.println(perimeter);
return perimeter;
}
public Double area(){
Double area=Math.PI*this.r*this.r;
System.out.println(area);
return area;
}

}

class CricleTest{
public static void main(String[] args) {
Cricle cricle = new Cricle(5.00);
cricle.perimeter();
cricle.area();
}
}

Java创建圆形类Circle.属性:半径r;方法:计算周长、显示周长、计算面积...
private double c = 0.0;private double s = 0.0;public Circle(double r) { this.r = r;} \/ 计算周长 \/ public void calcPerimeter() { c = Math.PI * r * 2;} \/ 展示周长 \/ public void showPerimeter() { System.out.printf("周长为:%.2f", c);System.out.println();} ...

java编程 定义一个类:圆 属性:半径 方法:(1)计算周长、面积 (2)设置半...
c2.setR(80);System.out.printf("c1面积为:%.3f\\r\\n",c1.getArea());System.out.printf("c1周长为:%.3f\\r\\n",c1.getPerimeter());System.out.printf("c2面积为:%.3f\\r\\n",c2.getArea());System.out.printf("c2周长为:%.3f\\r\\n",c2.getPerimeter());} } ...

java编程:定义一个圆类,属性为半径,方法为对输入的半径计算其周长和面...
\\x09\\x09System.out.printf("c2面积为:%.3f\\r\\n",c2.getArea());\\x09\\x09System.out.printf("c2周长为:%.3f\\r\\n",c2.getPerimeter());\\x09 \\x09}

用java编写一个圆类Circle,该类拥有:一个成员变量 radius(私有,浮点型...
Circle(int r){ this.r=r;} public void getS(){ System.out.println("面积是:"+Math.PI*r*r);} public void getL(){ System.out.println("周长是:"+2*Math.PI*r);} public static void main(String[] args){ String s=JOptionPane.showInputDialog("请输入半径:");System.out.prin...

JAVA:定义一个圆类Circle,成员变量:半径 radius;
public class Main{ public static void main(String[] ags) { double radius = 2.5; double width = radius * 2; double height = 10; Circle circle = new Circle(radius); System.out.println("圆形周长:" + circle.calculgirth()); System.out.println("圆形面积...

...并保存在变量r中,计算并输出该圆的面积和周长。急急急!
\/\/求周长 public static float getCir(){ return 2*PI*r;} \/\/求面积 public static float getAre(){ return PI*r*r;} public static void mian(String[] args){ \/\/输入一个半径值 Scanner scan = new Scanner(System.in);r = scan.nextFloat();\/\/打印周长 System.out.println("周长:...

用JavaApplet编写程序计算圆的周长和面积
可以通过创建一个圆的类完成计算圆周长和面积的功能。假设这个圆的类名叫做Circle,因为根据圆的半径就可以求出圆的周长和面积,所以可以在这个类中定义一个半径属性mRadius,然后定义两个方法getLength和getArea分别实现计算圆周长和面积的功能。java语言源代码如下:public class Circle{ \/\/圆的半径 private...

java中怎么去定义一个圆类,其有半径属性,并在类中写两个方法来分别计算...
\/\/圆的面积为 public static double c(double r){ return p*r*r;} \/\/圆的周长 public static double s(double r){ return 2*p*r;} public static void main(String [] arge){ System.out.println(fmt(c(2.3d)));System.out.println(fmt(s(2.3d)));} private static String fmt(...

java编写一个应用程序,创建一个圆类,在类中定义半径成员变量,如何求其...
"圆的面积为-->"+this.area(radius));} } public class OperateDemo{ public static void main(String args[]){ Round round=new Round();System.out.print("请输入圆的半径-->");Scanner scan=new Scanner(System.in);double radius=scan.nextDouble();round.print(radius);} } 楼主别忘了...

java习题:输入圆的半径r,计算并输出圆的面积和周长。
import java.util.Scanner;public class Circle { public static void main(String[] args) { \/\/ TODO Auto-generated method stub System.out.println("请输入半径:");Scanner sc = new Scanner(System.in);int r = sc.nextInt();\/*通过new Scanner(System.in)创建一个Scanner,控制台会一直...

相似回答