一道简单的JAVA题
编写一个圆(Circle)类,属性有半径,并提供计算面积和周长的功能。另编写一个测试类TestCircle,要求该测试类中定义一个静态常量circleNumber用来统计当前已经统计圆(Circle对象)的个数。并能计算并输出已有圆(Circle)对象的面积和周长。注意:圆周率对于所有圆都是相等的。这个怎么写啊。。。搜索更多相关的解决方案:
JAVA
----------------解决方案--------------------------------------------------------
程序代码:
//Circle.java
public class Circle {
final double PI = 3.14;
static int circleNumber = 0;
private double radius;
public Circle() {
this.radius = 1.0;
circleNumber++;
}
public Circle(double radius) {
this.radius = radius;
circleNumber++;
}
public double area() {
return radius*radius*PI;
}
public double perimeter() {
return 2*PI*radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}
//TestCircle.java
public class TestCircle {
public static void main(String[] args) {
Circle c1 = new Circle(1.23);
Circle c2 = new Circle(4.56);
System.out.println(Circle.circleNumber);
}
}
----------------解决方案--------------------------------------------------------
回复 楼主
兄弟我是个初学者,编的如果有什么错误或不满足要求的,可回复一下,我再修改,共同讨论一起进步嘛!!import javax.swing.JOptionPane;
public class Circle {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 100; i++) {
count += 1;
String n = JOptionPane.showInputDialog("请输入n的值:");
double radiu = Double.parseDouble(n);
double pa = 3.14159;
double area = radiu * radiu * pa;
double girth = 2 * radiu * pa;
double m = area * 100000 / 100;
double x = girth * 100000 / 100;
System.out.println("圆的面积和周长分别为:" + m + " 和 " + x);
System.out.println("已统计圆的个数为:" + count);
}
}
}
----------------解决方案--------------------------------------------------------
回复 楼主
兄弟我是个初学者,编的如果有什么错误或不满足要求的,可回复一下,我再修改,共同讨论一起进步嘛!!import javax.swing.JOptionPane;
public class Circle {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 100; i++) {
count += 1;
String n = JOptionPane.showInputDialog("请输入n的值:");
double radiu = Double.parseDouble(n);
double pa = 3.14159;
double area = radiu * radiu * pa;
double girth = 2 * radiu * pa;
double m = area * 100000 / 100;
double x = girth * 100000 / 100;
System.out.println("圆的面积和周长分别为:" + m + " 和 " + x);
System.out.println("已统计圆的个数为:" + count);
}
}
}
----------------解决方案--------------------------------------------------------
回复 4楼 myhnuhai
初学JAVA,一定要先养成面向对象的思想编程,对你将来深入学习JAVA有很大的好处 ----------------解决方案--------------------------------------------------------
回复 2楼 lampeter123
版主这个貌似有点不对吧。我运行了一下,好像达不到想要的结果呢 ----------------解决方案--------------------------------------------------------
回复 6楼 hcypx
其实你只要修改TestCircle就可以了例如: Circle c1 = new Circle(1.23);//是实例化一个Circle对像c1
double a = c1.area(); //用对象c1调用方法area()求得面积
double b = c1.perimeter();//用对象c1调用方法perimeter()求得周长
int n = Circle.circleNumber //统计创建Circle对象的个数
System.out.println("Circle c1的面积: " + a);
System.out.println("Circle c1的周长: " + b);
System.out.println("已有对象的个数: " +n );
----------------解决方案--------------------------------------------------------
来学习了
----------------解决方案--------------------------------------------------------