FAQ: Conditional Operators - Logical NOT:!

This community-built FAQ covers the “Logical NOT: !” exercise from the lesson “Conditional Operators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Logical NOT: !

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hey I have a question about this exercise. I do not get an error when I run the code as is:
I’ve highlighted at the bottom the part that confuses me. I should have to enter another instance variable correct? i.e. ‘boolean isConfirmed = false;’

But if I don’t include that, I get no error but the incorrect answer, right?

public class Reservation {
int guestCount;
int restaurantCapacity;
boolean isRestaurantOpen;
boolean isConfirmed;

public Reservation(int count, int capacity, boolean open) {
if (count < 1 || count > 8) {
System.out.println(“Invalid reservation!”);
}
guestCount = count;
restaurantCapacity = capacity;
isRestaurantOpen = open;
}

public void confirmReservation() {
if (restaurantCapacity >= guestCount && isRestaurantOpen) {
System.out.println(“Reservation confirmed”);
isConfirmed = true;
} else {
System.out.println(“Reservation denied”);
isConfirmed = false;
}
}

public void informUser() {
** // Write conditional here**


** if (!isConfirmed){**
** System.out.println(“Unable to confirm reservation, please contact restaurant.”);**
** } else {**
** System.out.println(“Please enjoy your meal!”);**
** }**

** }**

public static void main(String args) {
Reservation partyOfThree = new Reservation(3, 12, true);
Reservation partyOfFour = new Reservation(4, 3, true);
partyOfThree.confirmReservation();
partyOfThree.informUser();
partyOfFour.confirmReservation();
partyOfFour.informUser();
}
}

Hello Tommy, the instructions were not clear for me as well, you don’t need to reassign any values to isConfirmed all they wanted is for you to create if else, without “!” operator. That’s what I’ve done
example:

if(isConfirmed == true) // just a simple if condition like on a previous exercise, try it out! 
{
    // do something 
} 
else 
{
  // do something in case if the first statement didn't get  implemented 
}

Hope this helps.

1 Like
public class Reservation {
  int guestCount;
  int restaurantCapacity;
  boolean isRestaurantOpen;
  boolean isConfirmed;
  
  public Reservation(int count, int capacity, boolean open) {
    if (count < 1 || count > 8) {
      System.out.println("Invalid reservation!");
    }
    guestCount = count;
		restaurantCapacity = capacity;
		isRestaurantOpen = open;
  }  
  
  public void confirmReservation() {
    if (restaurantCapacity >= guestCount && isRestaurantOpen) {
      System.out.println("Reservation confirmed");
      isConfirmed = true;
    } else {
      System.out.println("Reservation denied");
			isConfirmed = false;
    }
  }
  
  public void informUser() {
    // Write conditional here
    if (!isConfirmed) {
      System.out.println("Unable to confirm reservation, please contact restaurant.");
    } else {
      System.out.println("Please enjoy your meal!");
    }

  }
  
  public static void main(String[] args) {
    Reservation partyOfThree = new Reservation(3, 12, true);
    Reservation partyOfFour = new Reservation(4, 3, true);
    partyOfThree.confirmReservation();
    partyOfThree.informUser();
    partyOfFour.confirmReservation();
    partyOfFour.informUser();
  }
}

with output:

Reservation confirmed
Please enjoy your meal!
Reservation denied
Unable to confirm reservation, please contact restaurant.

public void informUser() {

// Write conditional here

if(!isConfirmed)

{

System.out.println(“Unable to confirm reservation, please contact restaurant.”);

}

else

{

System.out.println(“Please enjoy teh meal!”);

}

}

I’m not able to debug error in the method, any inputs?

Welcome to the forums!

Please paste your full code using the </> button to improve readability and preserve formatting. Additionally, please indicate what error you received.

If formatted properly, your code should work (however, note that there is a typo in your second print statement, where the is spelled teh).

code:
ublic void informUser() {
// Write conditional here
if(!isConfirmed){
System.out.println(“Unable to confrim reservation, please contact restaurant”);
} else {
System.out.println(“Please enjoy your meal”);
}
}
output:
Reservation confirmed
Please enjoy your meal
Reservation denied
Unable to confrim reservation, please contact restaurant

error:
Does your code print ‘Unable to confirm reservation, please contact restaurant.’ if the reservation instance has a field of isConfirmed set to true?


I have tried putting isConfirmed = true before the if then else statement but the logic is wrong so I tried to put it after the if then else and the output was still correct. I just dont know why it gives me that error

sorry hahaha it turns out i wrote confrim instead of confirm. My bad hahaha. Have a nice day!

Ok, so this is a bit “out there”, but when they present this example:

boolean doorIsLocked = false;
 
if (!doorIsLocked) {
  System.out.println("Come on in!");
}

Explaining that:

This code snippet will print Come on in! because the false value is flipped to true by ! .

Sure, I understand it.
But wouldn’t it be more accurate to say that (!doorIsLocked) is pretty much the same as writing (doorIsLocked == false)? Then, since the boolean attributed to isDoorLocked actually holds the false value, the print command executes.

I tested it, it works and it seems a lot easier to think of it this way…
after all, if the door is NOT locked, you can come on in.

1 Like

Input:
public class Reservation {
int guestCount;
int restaurantCapacity;
boolean isRestaurantOpen;
boolean isConfirmed;

public Reservation(int count, int capacity, boolean open) {
if (count < 1 || count > 8) {
System.out.println(“Invalid reservation!”);
}
guestCount = count;
restaurantCapacity = capacity;
isRestaurantOpen = open;
}

public void confirmReservation() {
if (restaurantCapacity >= guestCount && isRestaurantOpen) {
System.out.println(“Reservation confirmed”);
isConfirmed = true;
} else {
System.out.println(“Reservation denied”);
isConfirmed = false;
}
}

public void informUser() {
// Write conditional here
if (!isConfirmed) {
System.out.println(“Unable to confirm reservation, please contact restaurant.”);
}
else {
System.out.println(“Please enjoy your meal!”);
}
}

}

public static void main(String args) {
Reservation partyOfThree = new Reservation(3, 12, true);
Reservation partyOfFour = new Reservation(4, 3, true);
partyOfThree.confirmReservation();
partyOfThree.informUser();
partyOfFour.confirmReservation();
partyOfFour.informUser();
}
}

Output:
Reservation.java:39: error: class, interface, or enum expected
public static void main(String args) {
^
Reservation.java:41: error: class, interface, or enum expected
Reservation partyOfFour = new Reservation(4, 3, true);
^
Reservation.java:42: error: class, interface, or enum expected
partyOfThree.confirmReservation();
^
Reservation.java:43: error: class, interface, or enum expected
partyOfThree.informUser();
^
Reservation.java:44: error: class, interface, or enum expected
partyOfFour.confirmReservation();
^
Reservation.java:45: error: class, interface, or enum expected
partyOfFour.informUser();
^
Reservation.java:46: error: class, interface, or enum expected
}
^
7 errors

What’s wrong here?

AT one point of your code, you have an extra } or missing an }. Please format your code properly so using corresponding code-blocks so we can help you better!

I’ve not touched anything but what I was supposed to. I’ve tried adding and deleting } and cannot figure it out. EDIT: I figured it out. It was a spacing issue? So weird.

Not sure if i’m understand the theory, or the check is wrong… when I run this through the java debugger in vscodium it seem correct doing exactly what I expect, but when I submit it as an answer it says it’s wrong. From what I’m reading:

Reservation partyOfThree = new Reservation(3, 12, true); inputs that there are 3 guest, and 12 open spaces and the true indicates the restaurant is open. Which means the reservation should be available to be booked.
While Reservation partyOfFour = new Reservation(4, 3, true); inputs that there are 4 guests and 3 open spaces and the true indicates the restaurant is open. With this information the reservation should NOT be accepted, when I step this through the debugger this is EXACTLY what happens but for some reason when I submit it in CodeAcademy online code checker it doesn’t accept the answer. Can someone PLEASE make clear what I’m doing wrong here? I’ve pasted my entire code in the bottom.

public class Reservation {
  int guestCount;
  int restaurantCapacity;
  boolean isRestaurantOpen;
  boolean isConfirmed;
  
  public Reservation(int count, int capacity, boolean open) {
    if (count < 1 || count > 8) {
      System.out.println("Invalid reservation!");
    }
    guestCount = count;
		restaurantCapacity = capacity;
		isRestaurantOpen = open;
  }  
  
  public void confirmReservation() {
    if (restaurantCapacity >= guestCount && isRestaurantOpen) {
      System.out.println("Reservation confirmed");
      isConfirmed = true;
    } else {
      System.out.println("Reservation denied");
			isConfirmed = false;
    }
  }
  
  public void informUser() {
    // Write conditional here
    if (!isConfirmed) {
      System.out.println("Unable to confirm reservation, please contact resturant.");
    } else {
      System.out.println("Please enjoy your meal!");
    }
  }
  
  public static void main(String[] args) {
    Reservation partyOfThree = new Reservation(3, 12, true);
    Reservation partyOfFour = new Reservation(4, 3, true);
    partyOfThree.confirmReservation();
    partyOfThree.informUser();
    partyOfFour.confirmReservation();
    partyOfFour.informUser();
  }
}

I used the AI Code checker and I misspelt the word Restaurant.