|
1 | 1 | Polymorphism Exercise
|
2 | 2 | <ol>
|
3 | 3 | <li> Create a Person class with attributes String name and int health.
|
4 |
| -Create a constructor with parameter name and set health = 100;. |
5 |
| -Create getter methods for name and health and setter method for |
6 |
| - health.</li> |
7 |
| -<li> Create a Warrior class that extends Person with an attribute Weapon |
8 |
| -weapon;. Create a constructor with parameter name. Call the super |
9 |
| -constructor with the name. Create a setter method |
10 |
| - setWeapon(Weapon weapon);.</li> |
11 |
| -<li> Create a Weapon class with one abstract method strike(Warrior |
12 |
| - opponent); an no attributes.</li> |
13 |
| -<li> To the Warrior class. Add a decHealth method with a single |
14 |
| -parameter int amt. Set the health value to Math.max(0, health – |
15 |
| -amt); which will keep the health attribute from going negative. Then |
16 |
| -call the opponent’s decHealth method. The getName, getHealth, |
17 |
| - and setHealth from Person class will be used.</li> |
18 |
| -<li> To the Warrior class add an attack method that takes a Warrior |
19 |
| -opponent parameter. If this warrior’s health is positive and the |
20 |
| -opponent’s health is positive and this warrior’s weapon is not null, |
21 |
| -print this warrior’s name is attacking the opponent’s name. Finally, |
22 |
| - print the opponent’s name and health.</li> |
| 4 | +Create a constructor with parameter name and set <strong>health = 100;</strong>. |
| 5 | +Create getter methods for <strong>name</strong> and <strong>health</strong> and setter method for |
| 6 | + <strong>health</strong>.</li> |
| 7 | +<li> Create a Warrior class that extends Person with an attribute <strong>Weapon |
| 8 | +weapon;</strong>. Create a constructor with parameter <strong>name</strong>. Call the super |
| 9 | +constructor with the <strong>name</strong>. Create a setter method |
| 10 | + <strong>setWeapon(Weapon weapon);</strong>.</li> |
| 11 | +<li> Create a Weapon class with one abstract method <strong>strike(Warrior |
| 12 | + opponent);</strong> an no attributes.</li> |
| 13 | +<li> To the <strong>Warrior</strong> class. Add a <strong>decHealth</strong> method with a single |
| 14 | +parameter <strong>int amt</strong>. Set the <strong>health</strong> value to <strong>Math.max(0, health – |
| 15 | +amt);</strong> which will keep the <strong>health attribute from going negative. Then |
| 16 | +call the opponent’s <strong>decHealth method. The <strong>getName</strong>, <strong>getHealth</strong>, |
| 17 | + and <strong>setHealth</strong> from Person class will be used.</li> |
| 18 | +<li> To the <strong>Warrior</strong> class add an <strong>attack</strong> method that takes a <strong>Warrior |
| 19 | +opponent</strong> parameter. If this warrior’s <strong>health</strong> is positive and the |
| 20 | +opponent’s <strong>health</strong> is positive and this warrior’s <strong>weapon</strong> is not null, |
| 21 | +print this warrior’s <strong>name</strong> is attacking the opponent’s <strong>name</strong>. Finally, |
| 22 | + print the opponent’s <strong>name</strong> and <strong>health</strong>.</li> |
23 | 23 | <li> Use the provided Weapons and Main classes to validate your Warrior
|
24 | 24 | and Weapon classes.</li>
|
25 | 25 | <li> Output should be:<ul>
|
|
0 commit comments