-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAliens.java
111 lines (83 loc) · 1.94 KB
/
Aliens.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
abstract class AlienParent { //Abstract Base Class. Cannot be made into an object, constructors cannot be directly called.
private int health;
public AlienParent(int h) {
super();
health = h;
}
public AlienParent(AlienParent a) {
health = a.health;
}
public boolean isAlive() {
return health > 0;
}
public void takeDamage(int d) {
health -= d;
}
public abstract AlienParent clone();
public abstract int getDamage();
public int getHealth() {return health;}
public String toString() {
return "AlienParent. Health: " + health + ".";
}
}
class BorgAlien extends AlienParent {
private final int DAMAGE = 10;
public BorgAlien(int h) {
super(h);
}
public BorgAlien(BorgAlien b) {
super(b);
}
public BorgAlien clone() {
return new BorgAlien(this);
}
public int getDamage() {return DAMAGE;}
//public void Assimilate(AlienParent a) {
//
//}
public String toString() {
return "BorgAlien. Health: " + getHealth() + ", Damage: " + getDamage() + ".";
}
}
class MartianAlien extends AlienParent{
public MartianAlien(int h) {
super(h);
}
public MartianAlien(MartianAlien b) {
super(b);
}
public MartianAlien clone() {
return new MartianAlien(this);
}
public int getDamage() {return 3;} // overriding abstract method
public String toString() {
return "MartianAlien. Health: " + getHealth() + ", Damage: " + getDamage() + ".";
}
}
class AlienPack2 {
private AlienParent [] aliens;
private int next;
public AlienPack2(int num) {
aliens = new AlienParent[num];
}
public AlienPack2(AlienPack2 a) {
aliens = new AlienParent[a.aliens.length];
next = a.next;
for(int i = 0; i < next; i++) {
aliens[i] = a.aliens[i].clone();
}
}
public void addAlien(AlienParent a) {
if(next < aliens.length) {
aliens[next] = a;
next++;
}
}
public int totalDamage() {
int total = 0;
for(int i = 0; i < next; i++) {
total += aliens[i].getDamage();
}
return total;
}
}