共计 1161 个字符,预计需要花费 3 分钟才能阅读完成。
全文链接:tecdat.cn/?p=29631
Requirement
In this Assignment, you should write a class that, given a circle’s radius, has methods that return the circle’s area, diameter, and circumference.
In case you have forgotten, the equations for a circle’s area, diameter, and circumference is given below.
diameter = 2r
circumference = 2πr
复制代码
Based on Chapter 3, Programming Challenge # 8 Circle class in your textbook. Your output is given below.
The circle’s area is 88.2472631
The circle’s diameter is 10.6
The circle’s circumference is 33.300854
复制代码
Write a separate class called CircleDemo with a main method that asks the user for the circle’s radius, creates a Circle object, and then reports the circle’s area, diameter, and circumference using the circle’s getter methods.
Analysis
本题须要的数学背景见 Pi,须要实现一个 Circle 类,以及对应的办法,如 setRadius(),getRadius(),getArea(),getDiameter(),getCircumference()。
本题较为简单,留神数据类型,精度以及输入输出格局即可。
Tips
上面是 ClicleDemo 类的实现
class CircleDemo {
private double radius;
private static final PI = 3.14159;
public CircleDemo() {}
public void setRadius() {
this.radius = radius;
}
public double getRadius() {
return this.radius;
}
public double getArea() {
return CircleDemo.PI * this.radius * this.radius;
}
public double getDiameter() {
return 2 * this.radius;
}
public double getCircumference() {
return 2 * CircleDemo.PI * this.radius;
}
}