mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zanninso
378852698e
|
1 year ago | |
---|---|---|
.. | ||
README.md | 1 year ago |
README.md
AdventureCharacter
Instructions
In the following quest, we will work with the same files and classes. You should keep them from one exercise to the following.
Create a file Character.java
.
Create a public class named Character
.
The class must contains three properties :
- maxHealth (int) : with a getter and no setter. This property is not updatable (
final
keyword). - currentHealth (int) : with a getter and no setter.
- name (String) : with a getter and no setter. This property is not updatable (
final
keyword).
Create a constructor with two parameters (name and maxHealth) : the currentHealth is initialized with the maxHealth value.
Rewrite the toString
, that will have the format <name> : <currentHealth>/<maxHealth>
. If the currentHealth is 0, the format is <name> : KO
.
Implement two methods :
takeDamage
, with an integer parameter, that will remove the amount in parameter to thecurrentHealth
. The current health can't be lower than 0.attack
, with aCharacter
parameter, that will calltakeDamage
of the parameter with a default value :9
.
Usage
Here is a possible ExerciseRunner.java to test your function :
public class ExerciseRunner {
public static void main(String[] args) {
Character aragorn = new Character("Aragorn", 20);
Character uruk = new Character("Uruk", 5);
System.out.println(aragorn.toString());
System.out.println(uruk.toString());
aragorn.attack(uruk);
System.out.println(uruk.toString());
aragorn.takeDamage(12);
System.out.println(aragorn.toString());
}
}
and its output :
$ javac *.java -d build
$ java -cp build ExerciseRunner
Aragorn : 20/20
Uruk : 5/5
Uruk : KO
Aragorn : 8/20
$