Booleans in Java are represented by the boolean
type, which values can be either true
or false
.
Java supports three boolean operators:
!
(NOT): negates the boolean&&
(AND): takes two booleans and returns true
if they're both true
||
(OR): returns true
if any of the two booleans is true
!true // => false
!false // => true
true && false // => false
true && true // => true
false || false // => false
false || true // => true
In this exercise, you'll implement the quest logic for a new RPG game that a friend is developing. The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. Unfortunately, disaster strikes: her best friend was kidnapped while searching for berries in the forest. Annalyn will try to find and rescue her friend, optionally taking her dog along on the quest.
After some time spent following the trail, Annalyn discovers the camp where her friend is imprisoned. It turns out there are two kidnappers: a mighty knight and a cunning archer.
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters in the forest and whether Annalyn's pet dog is present or not.
Implement the (static) AnnalynsInfiltration.canFastAttack()
method, which takes a boolean value indicating whether the knight is awake.
This method returns true
if a fast attack can be made based on the state of the knight.
Otherwise, it returns false
:
boolean knightIsAwake = true;
AnnalynsInfiltration.canFastAttack(knightIsAwake);
// => false
Implement the (static) AnnalynsInfiltration.canSpy()
method, which takes three boolean values indicating whether the knight, archer, and prisoner, respectively, are awake.
The method returns true
if the group can be spied upon based on the state of the three characters.
Otherwise, it returns false
:
boolean knightIsAwake = false;
boolean archerIsAwake = true;
boolean prisonerIsAwake = false;
AnnalynsInfiltration.canSpy(knightIsAwake, archerIsAwake, prisonerIsAwake);
// => true
Implement the (static) AnnalynsInfiltration.canSignalPrisoner()
method, which takes two boolean values indicating whether the archer and the prisoner, respectively, are awake.
The method returns true
if the prisoner can be signaled based on the state of the two characters.
Otherwise, it returns false
:
boolean archerIsAwake = false;
boolean prisonerIsAwake = true;
AnnalynsInfiltration.canSignalPrisoner(archerIsAwake, prisonerIsAwake);
// => true
Implement the (static) AnnalynsInfiltration.canFreePrisoner()
method, which takes four boolean values.
The first three parameters indicate whether the knight, archer, and prisoner, respectively, are awake.
The last parameter indicates whether Annalyn's pet dog is present.
The method returns true
if the prisoner can be freed based on the state of the three characters and the presence of Annalyn's pet dog.
Otherwise, it returns false
:
boolean knightIsAwake = false;
boolean archerIsAwake = true;
boolean prisonerIsAwake = false;
boolean petDogIsPresent = false;
AnnalynsInfiltration.canFreePrisoner(knightIsAwake, archerIsAwake, prisonerIsAwake, petDogIsPresent);
// => false
Sign up to Exercism to learn and master Java with 25 concepts, 148 exercises, and real human mentoring, all for free.