-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathw5q2_Circle_Mehtods.java
More file actions
83 lines (63 loc) · 2.26 KB
/
w5q2_Circle_Mehtods.java
File metadata and controls
83 lines (63 loc) · 2.26 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.Scanner;
import java.lang.Math;
public class w5q2_Circle_Mehtods {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double r, c_x, c_y, x, y;
while (true) {
System.out.println("Enter radius and co-ordinate of center of the circle");
r = sc.nextDouble();
// continue to take value unless user give negative value of radius
if (r < 0) {
System.out.println("Finished! you enterd radius a negative number");
break;
}
c_x = sc.nextDouble();
c_y = sc.nextDouble();
Circle cr = new Circle(r, c_x, c_y);
double ar = cr.areaCircle(cr.rad);
System.out.println("The area of the circle is: " + ar);
double pr = cr.periCircle(cr.rad);
System.out.println("The perimeter of the circle is: " + pr);
// taking co-ordinates
System.out.println("Enter co-ordinate to check if this lies inside the circle or not");
x = sc.nextDouble();
y = sc.nextDouble();
// checking the co-ordinate if it lies inside, on or outside the circle
double ck = cr.checkPoint(x, y);
if (ck == cr.rad) {
System.out.println("The given co-ordinate lies on the circumference of the circle");
} else if (ck > cr.rad) {
System.out.println("The given co-ordinate doesn't lie inside the circle");
} else if (ck < cr.rad) {
System.out.println("The given co-ordinate lies inside the circle");
}
}
sc.close();
}
}
/**
* Circle
*/
class Circle {
double rad;
double x, y;
Circle(double rad, double x, double y) {
this.rad = rad;
this.x = x;
this.y = y;
}
double areaCircle(double rad) {
double area = (22.0 / 7.0) * rad * rad;
return area;
}
double periCircle(double r) {
double peri = 2 * (22.0 / 7.0) * r;
return peri;
}
double checkPoint(double x, double y) {
double dist = Math.sqrt(Math.pow((this.x - x), 2) + Math.pow((this.y - y), 2));
// System.out.println(dist);
return dist;
}
}