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.

62 lines
2.0 KiB

## 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 the `currentHealth`. The current health can't be lower than 0.
* `attack`, with a `Character` parameter, that will call `takeDamage` of the parameter with a default value : `9`.
### Usage
Here is a possible ExerciseRunner.java to test your function :
```java
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 :
```shell
$ javac *.java -d build
$ java -cp build ExerciseRunner
Aragorn : 20/20
Uruk : 5/5
Uruk : KO
Aragorn : 8/20
$
```
### Notions
[Class](https://docs.oracle.com/javase/tutorial/java/javaOO/classdecl.html)
[Property](https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html)
[Override](https://docs.oracle.com/javase/tutorial/java/IandI/override.html)