-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathCircle.java
78 lines (66 loc) · 1.84 KB
/
Circle.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author roberta
*/
public class Circle { // save as "Circle.java"
// private instance variable, not accessible from outside this class
private double radius;
private String color;
// 1st constructor, which sets both radius and color to default
public Circle() {
radius = 1.0;
color = "red";
}
// 2nd constructor with given radius, but color default
public Circle(double r) {
radius = r;
color = "red";
}
//1) 3rd constructor with the given radius and color
/**
* public Circle(double r, String c) {
*
* radius = r; color = c;
*
* }
*/
// 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;
}
//2) Getter for instance variable color
public String getColor() {
return color;
}
/**
* 4)Setter for instance variable radius public void setRadius(double r) {
* radius = r; }
*
* //4)// Setter for instance variable color public void setColor(String c){
* color = c; }
*/
//5) Using the special keyword "this"
public void setRadius(double radius) {
this.radius = radius;
}
public void setColor(String color) {
this.color = color;
}
//5) Using the special keyword "this"
public Circle(double radius, String color) {
this.color = color;
this.radius = radius;
}
public String toString() {
return "Circle: radius=" + radius + " color=" + color;
}
}