-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColourable.java
More file actions
60 lines (48 loc) · 1.22 KB
/
Colourable.java
File metadata and controls
60 lines (48 loc) · 1.22 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
// Colorable.java
public interface Colourable {
void howToColor();
}
// GeometricObject.java
abstract class GeometricObject {
public abstract double getArea();
}
// Square.java
class Square extends GeometricObject implements Colourable {
private double side;
public Square() {
this.side = 0;
}
public Square(double side) {
this.side = side;
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
@Override
public double getArea() {
return side * side;
}
@Override
public void howToColor() {
System.out.println("Color all four sides.");
}
}
// Test program
class Test {
public static void main(String[] args) {
GeometricObject[] objects = new GeometricObject[5];
// Populate the array
objects[0] = new Square(5);
objects[1] = new Square(7);
// ... similarly, populate the rest of the array
for (GeometricObject object : objects) {
System.out.println("Area: " + object.getArea());
if (object instanceof Colourable) {
((Colourable) object).howToColor();
}
}
}
}