:: Enseignements :: ESIPE :: IMAC2 :: IMAC2 2020-2021 :: Object oriented programming in Java ::
[LOGO]

#5.en Inheritance, constructor calls, visibility


Exercice 1 - Robot

We first want to model robot fights. A robot has a name and a number of life points between 10 (perfectly alive) and 0 (totally dead). All robots start with 10 life points when created and lose life points during fights..

  1. Write a class Robot in the package fr.umlv.fight with a constructor taking the name of the robot and so that the following code displays "Robot" followed by the name of the robot.
         var bob = new Robot("bob");
         System.out.println(bob);  // displays "Robot bob"
        
  2. Add a fire method that takes a robot as a parameter and which allows the current robot to shoot the robot taken as a parameter. Robots being perfect, they always hit their target, so a robot that gets shot will always lose 2 life points.
    Then, add a isDead method which allows you to find out if a robot is dead.
  3. Write in a new class fr.umlv.fight.Arena a method fight which simulates the fight between two robots.
    A fight takes place in the following way: the first robot shoots the second one; if the second is not dead, it replies and so on, until one of the robots dies. The robot remaining alive is declared winner and is returned by the fight method.
    To follow the progress of the fight, modify the fire method so that at the end, a message is displayed:
         "Robot foo was hit by bar!"
        

    Write the code of the fight method as well as a main test of a fight between D2R2 and Data .

Exercice 2 - Fighter

In fact, the fights between Robot are not interesting because the first shooter robot always wins. The sports association robot shooting, in accordance with human rights principles decides to organize fights between human fighters and robots. Humans are more interesting because half the time they miss their target.
We want to write a class Fighter which represents human fighters who also have a name, a number of life points but who, when they shoot, miss once out of two times (average).
To model the fact that a human misses once out of two shots, we will use the class java.util.Random which implements a pseudo random generator and its method nextBoolean .
So that all humans seem to behave different, while creating a Fighter , we will indicate a number which will serve as seed ( seed ) to the pseudo-random generator.

  1. Explain how a pseudo random generator works and what is the seed of a pseudo random generator?
  2. Write the class Fighter so that:
    • The Fighter class inherits from the Robot class
    • The display method displays "Fighter" followed by name of the fighter.
    • When a human fires with the fire method, he has a 50/50 chance of missing his target.
    Note: it is assumed here that it is not prohibited to have fields with visibility other than private or package and a little copy and paste won't ruin your nascent career.
  3. Explain why you should never only have one field be declared with visibility other than private or package.
    Modify the code accordingly.
  4. There is a bug in the implementation of the Robot class, it is possible that a robot shoots a dead robot which is strictly prohibited by the code of ethics of the robot shooting sports association.
    Make sure that if a robot tries to shoot a dead robot, an exception is thrown (we leave you guess which one :)
  5. We can notice that the fire method of the class Fighter has the same problem. What can you conclude on copy and paste?
  6. To avoid copying and pasting code, the idea is to introduce a rollDice method in Robot which models the fact that a robot has a 100% chance of reaching its target while one fighter has a 50% chance.
    In this case, the fire method can be written only once in Robot because the rollDice method will have a different code for Robot and Fighter .
    Modify the code to introduce the rollDice method. What visibility should this method have?
  7. Check that we can do a human fight with the following code
         var john = new Fighter("John", 1);
         var jane = new Fighter("Jane", 2);
         System.out.println(Arena.fight(john, jane) + " wins");
        
  8. If this has not already been done, arrange for a Fighter , the display will be "Fighter" followed by its name.
    Note: similarly the name field must remain private !
  9. In conclusion, remember what subtyping is and what is polymorphism using as an example the codes you just wrote.