-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.java
More file actions
68 lines (57 loc) · 1.61 KB
/
Player.java
File metadata and controls
68 lines (57 loc) · 1.61 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
package src;
public class Player {
private final String userName;
private final float userHeight;
private int userLife;
private boolean hasWin;
// Default Constructor.
Player() {
userLife = 0;
userHeight = 0.0F;
userName = "";
hasWin = true;
}
// Parametrized Constructor.
Player(String userName, float userHeight) {
this.userName = userName;
this.userHeight = userHeight;
}
// Overloaded Parametrized Constructor.
Player(String userName, float userHeight, int userLife) {
// Calling the above src.constructor using this keyword.
this(userName, userHeight);
this.userLife = userLife;
}
// Accessor or getter method.
public int getLife() {
return this.userLife;
}
// Accessor or getter method.
public boolean userStatus() {
return this.hasWin;
}
// Gain Health.
public void addHealth() {
if (userLife < 3)
userLife++;
}
// Loss Health.
public void loseHealth() {
if (userLife < 3)
userLife--;
else
hasWin = false;
}
// Showing Game Results.
public void showSummary() {
System.out.println("Username : " + this.userName);
System.out.println("Height : " + this.userHeight + "cm");
System.out.println("Life : " + this.userLife);
System.out.println("Win : " + this.userStatus());
}
// src.Main driven function.
public static void main(String[] args) {
var p = new Player("Naveed", 1.71F, 3);
p.showSummary();
}
}