Welcome to Wizards and Warriors on Exercism's Java Track.
If you need help running the tests or submitting your code, check out HELP.md.
If you get stuck on the exercise, check out HINTS.md, but try and solve it without using those first :)
Inheritance is a core concept in OOP (Object Oriented Programming). It donates IS-A relationship. It literally means in programming as it means in english, inheriting features from parent(in programming features is normally functions and variables).
Consider a class, Animal as shown,
//Creating an Animal class with bark() as a member function.
public class Animal {
public void bark() {
System.out.println("This is a animal");
}
}Animal is a parent class, because the properties this class has can be extended to all the animals in general.
Consider an animal named Lion, having a class like,
//Lion class is a child class of Animal.
public class Lion extends Animal {
public void bark() {
System.out.println("Lion here!!");
}
}Now whenever we do,
Animal animal = new Lion(); //creating instance of Animal, of type Lion
animal.bark();Note: Initialising the Animal class with Lion.
The output will look like
Lion here!!According to OOP, there are many types of inheritance, but Java supports only some of them(Multi-level and Hierarchical). To read more about it, please read this.
In this exercise you're playing a role-playing game named "Wizards and Warriors," which allows you to play as either a Wizard or a Warrior.
There are different rules for Warriors and Wizards to determine how much damage points they deal.
For a Warrior, these are the rules:
- Deal 6 points of damage if the fighter they are attacking is not vulnerable
- Deal 10 points of damage if the fighter they are attacking is vulnerable
For a Wizard, these are the rules:
- Deal 12 points of damage if the Wizard prepared a spell in advance
- Deal 3 points of damage if the Wizard did not prepare a spell in advance
In general, fighters are never vulnerable. However, Wizards are vulnerable if they haven't prepared a spell.
You have six tasks that work with Warriors and Wizard fighters.
Override the toString() method on the Fighter class to return a description of the fighter, formatted as "Fighter is a <FIGHTER_TYPE>".
Fighter warrior = new Warrior();
warrior.toString();
// => "Fighter is a Warrior"Ensure that the Fighter.isVulnerable() method always returns false.
Fighter warrior = new Warrior();
warrior.isVulnerable();
// => falseImplement the Wizard.prepareSpell() method to allow a Wizard to prepare a spell in advance.
Wizard wizard = new Wizard();
wizard.prepareSpell();Ensure that the isVulnerable() method returns true if the wizard did not prepare a spell; otherwise, return false.
Fighter wizard = new Wizard();
wizard.isVulnerable();
// => trueImplement the Wizard.damagePoints() method to return the damage points dealt: 12 damage points when a spell has been prepared, 3 damage points when not.
Wizard wizard = new Wizard();
Warrior warrior = new Warrior();
wizard.prepareSpell();
wizard.damagePoints(warrior);
// => 12Implement the Warrior.damagePoints() method to return the damage points dealt: 10 damage points when the target is vulnerable, 6 damage points when not.
Warrior warrior = new Warrior();
Wizard wizard = new Wizard();
warrior.damagePoints(wizard);
// => 10- @himanshugoyal1065