-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircle.java
More file actions
54 lines (49 loc) · 1.5 KB
/
Circle.java
File metadata and controls
54 lines (49 loc) · 1.5 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* The Circle class models a circle with a radius and color.
*/
public class Circle { // Save as "Circle.java"
// private instance variable, not accessible from outside this class
private double radius;
private String color;
// The default constructor with no argument.
// It sets the radius and color to their default value.
public Circle() {
radius = 1.0;
color = "red";
}
// 3rd constructor to construct a new instance of Circle with the given radius and color
public Circle(double radius, String color){
this.radius = radius;
this.color = color;
}
// 2nd constructor with given radius, but color default
public Circle(double radius) {
this.radius = radius;
color = "red";
}
// A public method for retrieving the radius
public double getRadius() {
return radius;
}
// A public method for computing the area of circle
public double getArea() {
return radius*radius*Math.PI;
}
public String getColor(){
return color;
}
public void setColor(String newColor){
color = newColor;
}
public void setRadius(double newRadius){
radius = newRadius;
}
// Return a description of this instance in the form of
// Circle[radius=r,color=c]
public String toString() {
return "Circle[radius=" + radius + " color=" + color + "]";
}
public double getCircumference(){
return (radius*2)*Math.PI;
}
}