-
Notifications
You must be signed in to change notification settings - Fork 176
/
Circle.java
51 lines (40 loc) · 1.09 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
public class Circle {
private int id;
private static int circleCount = 0;
private final double PI = 3.1415;
public int radius;
public Circle(int radius){
circleCount++;
this.id = circleCount;
this.radius = radius;
}
public static void main(String[] args){
Circle circle = new Circle(5);
int actualArea = circle.area(false); // should be 78
int integerArea = circle.area(true); // should be 81
}
// A.K.A Gauss Circle Problem
public int area(){
int result = 0;
for(int x=-radius; x<=radius; x++){
for(int y=-radius; y<=radius; y++){
if(x*x + y*y <= radius*radius)
result++;
}
}
return result;
}
public int area(boolean integerArea){
try {
getCircleCount();
if (integerArea)
return area();
}
catch(Exception exception){
}
return (int) (1.0*radius*radius*PI);
}
public static int getCircleCount(){
return circleCount;
}
}