You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 1b3696a
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: source/ch6_definingclasses.ptx
+52Lines changed: 52 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -423,6 +423,58 @@ public Fraction(Integer num, Integer den) {
423
423
</code>
424
424
</program>
425
425
</listing>
426
+
427
+
<exerciselabel="java_game_character_parsons">
428
+
<statement>
429
+
<p>
430
+
Rearrange the blocks to create a <c>Player</c> class with a <c>private int health</c> instance variable. Implement a getter and a setter that validates health points to remain within <c>0</c> and <c>100</c>.
431
+
</p>
432
+
</statement>
433
+
<blocks>
434
+
<blockorder="1">
435
+
<choicecorrect="yes">
436
+
<cline>public class Player {</cline>
437
+
<cline> private int health;</cline>
438
+
</choice>
439
+
<choicecorrect="no">
440
+
<cline>public class Player {</cline>
441
+
<cline> public int health;</cline>
442
+
</choice>
443
+
</block>
444
+
<blockorder="2">
445
+
<choicecorrect="yes">
446
+
<cline> public int getHealth() {</cline>
447
+
<cline> return health;</cline>
448
+
<cline> }</cline>
449
+
</choice>
450
+
<choicecorrect="no">
451
+
<cline> public void getHealth() {</cline>
452
+
<cline> return health;</cline>
453
+
<cline> }</cline>
454
+
</choice>
455
+
</block>
456
+
<blockorder="3">
457
+
<choicecorrect="yes">
458
+
<cline> public void setHealth(int health) {</cline>
459
+
<cline> if (health >= 0 && health <= 100) {</cline>
460
+
<cline> this.health = health;</cline>
461
+
<cline> }</cline>
462
+
<cline> }</cline>
463
+
</choice>
464
+
<choicecorrect="no">
465
+
<cline> public void setHealth(int health) {</cline>
466
+
<cline> if (health >= 0 && health <= 100) {</cline>
0 commit comments