-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCR5.java
More file actions
77 lines (59 loc) · 1.5 KB
/
Copy pathCR5.java
File metadata and controls
77 lines (59 loc) · 1.5 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
68
69
70
71
72
73
74
75
76
77
public class CR5 {
abstract class Shape {
private String theColour;
public Shape(String colour) {
this.theColour = colour;
}
public String getColour() {
return theColour;
}
public abstract int getArea();
public abstract void scale(double factor);
public boolean hasLargerAreaThan(Shape other) {
return this.getArea() > other.getArea();
}
}
class Rectangle extends Shape {
int width;
int height;
public Rectangle() {
super("Black");
this.width = 1;
this.height = 1;
}
public Rectangle(int width, int height) {
super("Black");
this.width = width;
this.height = height;
}
public Rectangle(String colour, int width, int height) {
super(colour);
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
@Override
public String toString() {
return getColour() + " Rectangle (" + width + "x" + height + ")";
}
public int getArea() {
return width * height;
}
public void scale(double factor) {
width = (int) (width * factor);
height = (int) (height * factor);
}
}
public static void main(String[] args) {
Shape rec1 = new Rectangle("Blue", 1, 1);
Shape rec2 = new Rectangle("Green", 2, 2);
rec1.scale(3);
System.out.println(rec1.hasLargerAreaThan(rec2));
System.out.println(rec2.hasLargerAreaThan(rec1));
}
}