maple_leaf_notext
Java代写:CS110 Circle

Requirement

In this Assignment, you should write a class that, given a circles radius, has methods that return the circles area, diameter, and circumference.
In case you have forgotten, the equations for a circles area, diameter, and circumference is given below.

1
2
3
area = rr
diameter = 2r
circumference = 2r

Based on Chapter 3, Programming Challenge # 8 Circle class in your textbook. Your output is given below.

1
2
3
4
Enter the radius of a circle: 5.3
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 circles radius, creates a Circle object, and then reports the circles area, diameter, and circumference using the circles getter methods.

Analysis

本题需要的数学背景见Pi,需要实现一个Circle类,以及对应的方法,如setRadius(),getRadius(),getArea(),getDiameter(),getCircumference()。
本题较为简单,注意数据类型,精度以及输入输出格式即可。

Tips

下面是ClicleDemo类的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
}
}

]]>

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注