-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTank.java
More file actions
99 lines (88 loc) · 2.12 KB
/
Copy pathTank.java
File metadata and controls
99 lines (88 loc) · 2.12 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* This class is the parent for
* the Panzer and Alien classes.
*/
public abstract class Tank {
/**
* The unique attribute of each tank.
*/
public int health;
/**
* Class constructor that assigns health.
*/
protected Tank(int health) {
this.health = health;
}
/**
* Reduces the health of a tank by five points.
*
* @see PlayerActions.shootBullet()
*/
public final void decreaseHealthByFive() {
this.health -= 5;
}
/**
* Reduces the health of a tank to zero points.
*
* @see PlayerActions.activateAtomicBomb()
*/
public final void decreaseHealthToZero() {
this.health = 0;
}
/**
* Returns the health integer as a String with
* at least a width of 2 (padded).
*/
public final String getPaddedHealth() {
// % - begin format specifier
// 0 - variable 'salud' at index 0
// 2 - width of two (with padding, e.g., 00)
// d - to integer conversion
return String.format("%02d", health);
}
}
/**
* A children class of Tank
* whose initial health is 10.
*/
final class PanzerTank extends Tank {
/**
* Class constructor that calls the parent class.
*/
public PanzerTank() {
super(10);
}
/**
* Formats the health info for
* the String representation
*
* @return A String with type and health
*/
public String toString() {
// % - begin format specifier
// s - to string conversion
return String.format("PT-%s", super.getPaddedHealth());
}
}
/**
* A children class of Tank
* whose initial health is 20.
*/
final class AlienTank extends Tank {
/**
* Class constructor that calls the parent class.
*/
public AlienTank() {
super(20);
}
/**
* Formats the health info into the String representation
*
* @return A String with type and health
*/
public String toString() {
// % - begin format specifier
// s - to string conversion
return String.format("AT-%s", super.getPaddedHealth());
}
}