-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathQ8_Inheritance.java
105 lines (89 loc) · 3 KB
/
Q8_Inheritance.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.util.Scanner;
import java.lang.Math;
class Circle{
double radius;
Circle(){
radius = 0;
}
Circle(double r){
radius = r;
}
double Circle_area(){
return (Math.PI * radius *radius);
}
}
class Sector extends Circle{
double angle;
Sector(double a, double r){
angle = a;
radius = r;
}
Sector(){
angle = 0;
radius = 0;
}
double Sector_area(){
return (0.5*radius*radius*angle);
}
}
class Segment extends Circle{
double length;
Segment(){
radius = 0;
length = 0;
}
Segment(double l,double r){
radius = r;
length = l;
}
double Segment_area(){
double h = radius - Math.pow(Math.pow(radius, 2) - Math.pow((length / 2), 2), 0.5);
return ((h / (6 * length)) * ((3 * h * h) + (4 * length * length)));
}
}
class Q8_Inheritance{
public static void main(String[] args){
double a,r,l;
int ch;
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("--------------------------------------------");
System.out.println("1. Area of circle");
System.out.println("2. Area of sector of circle");
System.out.println("3. Area of segment of circle");
System.out.println("4. Exit!");
System.out.println("--------------------------------------------");
System.out.println("Enter your choice?");
ch = sc.nextInt();
switch(ch){
case 1:
System.out.println("Enter the radius of the circle");
r = sc.nextDouble();
Circle c = new Circle(r);
System.out.println("The area of the circle is: "+ c.Circle_area());
break;
case 2:
System.out.println("Ennter the radius of the circle");
r = sc.nextDouble();
System.out.println("Enter the angle of sector in radians");
a = sc.nextDouble();
Sector s1 = new Sector(a,r);
System.out.println("The area of the segment of the circle is: "+s1.Sector_area());
break;
case 3:
System.out.println("Enter the radius of the circle");
r = sc.nextDouble();
System.out.println("Enter the length of segment of the circle");
l = sc.nextDouble();
Segment s2 = new Segment(l,r);
System.out.println("The area of the segment of the circle is: "+s2.Segment_area());
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice!!");
}
}
}
}