I need help with Task4 method canFreePrisoner();

class AnnalynsInfiltration {
    public static boolean canFastAttack(boolean knightIsAwake) {
        return !knightIsAwake;

    public static boolean canSpy(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake) {
       return knightIsAwake || archerIsAwake || prisonerIsAwake;

    public static boolean canSignalPrisoner(boolean archerIsAwake, boolean prisonerIsAwake) {
        return !archerIsAwake && prisonerIsAwake;

    public static boolean canFreePrisoner(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake, boolean petDogIsPresent) {
         if (petDogIsPresent) {
 return !knightIsAwake && archerIsAwake && prisonerIsAwake;
 } else {
 return !knightIsAwake && !archerIsAwake && prisonerIsAwake;

What doesn’t work? What are your test results? What do you think the problem might be?

1 Like

Thanks for the help, I think the problem is that I’m not looking at the task in a simple way!

read the task carefully. As far as I can remember, the prisoner needn’t be awake, if the dog is present.

for a nicer solution, you could also omit if … else. If you try to, start your return statement with the knight.

Hi, divide and conquer :slight_smile:

So, the task says:

  • Free prisoner: Annalyn can try sneaking into the camp to free the prisoner.
    This is a risky thing to do and can only succeed in one of two ways:
    • If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep.
      The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape.

— So, if the dog is present and the archer is asleep, it does not matter if the knight is asleep or don’t because he is scared of the dog, the prisoner is irrelevant since if the knight is scared and the archer will need time to get ready.

  • If Annalyn does not have her dog then she and the prisoner must be very sneaky!
    Annalyn can free the prisoner if the prisoner is awake and the knight and archer are both sleeping, but if the prisoner is sleeping they can’t be rescued: the prisoner would be startled by Annalyn’s sudden appearance and wake up the knight and archer.

— Here, it is important that both archer and knight are asleep, but also it is important to know that the escape will only be true if the prisoner is awake when they are asleep (!awake)

I hope it helps you :slight_smile:

If you still need it, here I have the solution:

public static boolean canFreePrisoner(boolean knightIsAwake, boolean archerIsAwake, boolean prisonerIsAwake, boolean petDogIsPresent) {
if (petDogIsPresent && !archerIsAwake) {
return knightIsAwake || !archerIsAwake || prisonerIsAwake;

} else {
return !knightIsAwake && !archerIsAwake && prisonerIsAwake;

Providing complete solutions is rarely helpful in helping people learn. Please don’t respond to help requests, mentor requests, etc with completed solutions.

@HerbQuijano What have you tried? What are you having trouble with?

I believe that @Herbouijano was not requesting help, but trying to break it down in a way that was helpful.

I do not believe they were indicating that they were having a problem with the problem.

I believe that they reacted thinking that your first paragraph was in response to their answer rather than in response to CristianHdezZ about the complete solution at: I need help with Task4 method canFreePrisoner(); - #7 by CristianHdezZ

It may be due (in part) to the unfortunate lack of mention or not having the benefit of the “reply” threading for this message.