-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
58 lines (44 loc) · 1.11 KB
/
Main.java
File metadata and controls
58 lines (44 loc) · 1.11 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
interface Shape {
double getArea();
}
class Rectangle implements Shape{
private double width;
private double height;
public Rectangle(double width, double height){
this.width = width;
this.height = height;
}
@Override
public double getArea(){
return width * height;
}
}
class Circle implements Shape{
private double radius;
public Circle(double radius){
this.radius = radius;
}
@Override
public double getArea(){
return Math.PI * radius * radius;
}
}
class AreaCalculator {
public double sumAreas(Shape[] shapes){
double totalArea = 0;
for(Shape shape : shapes){
totalArea += shape.getArea();
}
return totalArea;
}
}
public class Main{
public static void main(String[] args){
Shape shapes[] = new Shape[2];
shapes[0] = new Rectangle(2, 4);
shapes[1] = new Circle(3);
AreaCalculator calculator = new AreaCalculator();
double totalArea = calculator.sumAreas(shapes);
System.out.println("Total Area: " + totalArea);
}
}