-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
78 lines (66 loc) · 1.62 KB
/
Player.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
public class Player
{
private String userName;
private 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 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());
}
// Main driven function.
public static void main(String[] args)
{
var p = new Player("Naveed", 1.71F, 3);
p.showSummary();
}
}