diff --git a/source/ch6_definingclasses.ptx b/source/ch6_definingclasses.ptx index 4375608..e70519e 100644 --- a/source/ch6_definingclasses.ptx +++ b/source/ch6_definingclasses.ptx @@ -423,6 +423,58 @@ public Fraction(Integer num, Integer den) { + + + +

+ Rearrange the blocks to create a Player class with a private int health instance variable. Implement a getter and a setter that validates health points to remain within 0 and 100. +

+
+ + + + public class Player { + private int health; + + + public class Player { + public int health; + + + + + public int getHealth() { + return health; + } + + + public void getHealth() { + return health; + } + + + + + public void setHealth(int health) { + if (health >= 0 && health <= 100) { + this.health = health; + } + } + + + public void setHealth(int health) { + if (health >= 0 && health <= 100) { + health = health; + } + } + + + + } + + +
+