forked from namigoel/hacktober-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculateArea.java
More file actions
67 lines (54 loc) · 1.71 KB
/
Copy pathCalculateArea.java
File metadata and controls
67 lines (54 loc) · 1.71 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
package Java;
/**
* @author Alessandro Arosio - 06/10/2020 16:38
*/
public class CalculateArea {
public static void main(String[] args) {
GeometricShape shape1 = new GeometricShape(Shape.CIRCLE, 6d, null);
GeometricShape shape2 = new GeometricShape(Shape.RECTANGLE, 3d, 4d);
GeometricShape shape3 = new GeometricShape(Shape.SQUARE, 7d, null);
printArea(shape1);
printArea(shape2);
printArea(shape3);
}
private static void printArea(GeometricShape shape) {
switch (shape.getShape()) {
case CIRCLE -> System.out.println(calculateCircleArea(shape.getX()));
case RECTANGLE -> System.out.println(calculateRectangleArea(shape.getX(), shape.getY()));
case SQUARE -> System.out.println(calculateSquareArea(shape.getX()));
}
}
private static Double calculateCircleArea(Double radius) {
return Math.PI * (radius * radius);
}
private static Double calculateRectangleArea(Double x, Double y) {
return x * y;
}
private static Double calculateSquareArea(Double base) {
return base * base;
}
private enum Shape {
CIRCLE,
RECTANGLE,
SQUARE
}
private static class GeometricShape {
private final CalculateArea.Shape shape;
private final Double x;
private final Double y;
public GeometricShape(Shape shape, Double x, Double y) {
this.shape = shape;
this.x = x;
this.y = y;
}
public Shape getShape() {
return shape;
}
public Double getX() {
return x;
}
public Double getY() {
return y;
}
}
}