FAQ: Conditional Operators - Review

This community-built FAQ covers the “Review” 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 Review

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 #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

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 #community:Codecademy-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!

As the code is written now, if you would enter a count number of <1 || >8 for a reservation in the main method, the outcome would be (if restaurantCapacity >= guestCount && isRestaurantOpen is true):

Invalid Reservation
Reservation Confirmed
Please enjoy your meal!

So what could you add to the code so that if the reservation is invalid the reservation isn’t then confirmed?

Many thanks for a response:)

5 Likes

I changed it to:

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

3 Likes

I’m having trouble creating instances in main, here’s my code

Reservation burger = New Reservation(8, 12, true, true);

System.out.println(burger.restaurantCapacity);

any help would be appreciated.

You don’t need to mark isConfirmed in main() as it is classified in confirmReservation(). Your reservation should only read

Reservation burger = New Reservation(8, 12, true);

.

Hi! :wave:

I’m adding the following sample code in main():

  public static void main(String[] args) {
    // Create instances here
    Reservation individualReservation = new Reservation(1, 4, true);
    Reservation groupReservation = new Reservation(20, 8, true);
    System.out.println("For the individual reservation, status is " + individualReservation.confirmReservation());
    System.out.println(individualReservation.informUser());
  }

but I’m getting back the error:

Reservation.java:38: error: 'void' type not allowed here
    System.out.println("For the individual reservation, status is " + individualReservation.confirmReservation());
                                                                                                              ^
Reservation.java:39: error: 'void' type not allowed here
    System.out.println(individualReservation.informUser());
                                                       ^
2 errors

I’ve checked against other sample code and can’t understand what’s wrong. Why is main() complaining about a void type not allowed when neither confirmReservartion() nor informUser() accept parameters?

Thank you! :pray:

i am completely sure, but i guess what you doing is trying to print a print statement. callling individualReservation.confirmReservation() already prints out a statement but then again you are printing the print statement. maybe thats whats causing the errors.

This question if about the Review. Can someone show me what the code would look like where you create instances in the main() that run every possible conditional branch? I clicked on view solution so I could understand this, and it didn’t change the code at all it just showed the same code as before clicking view solution. Thank you for any help.

Why does the code print “Unable to confirm reservation, please contact restaurant.”
When isConfirmed is true?
I need a answer

You need to post your code. Line 27 will always default to false unless the variable is set, or it is unreachable for some reason.

Take a look at the order in which you call your functions. As @psmilliorn mentioned, the default value for a boolean is false, meaning isConfirmed is has a value of false before it is assigned a value in confirmReservation().

Yes, isConfirmed is true, but only after it is assigned that value when confirmReservation() is called. Now take a look at where you call informUser() in relation to confirmReservation(). Why might the message printed indicate that the reservation cannot be confirmed?

oh!
confirmReservation() and informUser() must switch places.
It didn’t change the variable when informUser() was invoked?

Yes!

If you walk through your code step by step, you’ll see that the value of isConfirmed was false when informUser() was called. It only becomes true when you call confirmReservation(), which happened after the call to informUser().

So, even though the final value of isConfirmed is true, its value when informUser() was called was false, meaning informUser() executed the if statement, not the else clause.