-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperatiors.java
92 lines (71 loc) · 2.04 KB
/
operatiors.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
import java.lang.Math;
abstract class Shape {
public abstract double area();
public abstract double volume();
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double area() {
return length * width;
}
public double volume() {
return 0;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double area() {
return Math.PI * radius * radius;
}
public double volume() {
return 0;
}
}
class Sphere extends Shape {
private double radius;
public Sphere(double radius) {
this.radius = radius;
}
public double area() {
return 4 * Math.PI * radius * radius;
}
public double volume() {
return (4/3) * Math.PI * Math.pow(radius, 3);
}
}
class Cylinder extends Shape {
private double radius;
private double height;
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double area() {
return 2 * Math.PI * radius * (radius + height);
}
public double volume() {
return Math.PI * radius * radius * height;
}
}
public class operators {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(5, 3);
System.out.println("Rectangle Area: " + rectangle.area());
Circle circle = new Circle(4);
System.out.println("Circle Area: " + circle.area());
Sphere sphere = new Sphere(5);
System.out.println("Sphere Area: " + sphere.area());
System.out.println("Sphere Volume: " + sphere.volume());
Cylinder cylinder = new Cylinder(3, 8);
System.out.println("Cylinder Area: " + cylinder.area());
System.out.println("Cylinder Volume: " + cylinder.volume());
}
}