Answer:
boolean rsvp;
int selection;
String option1;
String option2;
(a) if(rsvp){
System.out.println("Attending");
} else {
System.out.println("Not attending");
}
(b) if (selection == 1){
System.out.println("Beef");
} else if (selection == 2){
System.out.println("Chicken");
} else if (selection == 3) {
System.out.println("Pasta");
} else {
System.out.println("Fish");
}
(c) switch(selection){
case 1:
if(rsvp){
option1 = "Thanks for attending. You will be served beef.";
} else{
option1 = "Sorry you can't make it";
}
break;
case 2:
if(rsvp){
option1 = "Thanks for attending. You will be served chicken.";
} else{
option1 = "Sorry you can't make it";
}
break;
case 3:
if(rsvp){
option1 = "Thanks for attending. You will be served pasta.";
} else{
option1 = "Sorry you can't make it";
}
break;
default:
if(rsvp){
option1 = "Thanks for attending. You will be served fish.";
} else{
option1 = "Sorry you can't make it";
}
break;
}
(d) if (option1.equals(option2)){
System.out.println("True");
} else {
System.out.println("False");
}
Explanation:
This code is written in Java.
a) It uses an if statement to print "attending" if rsvp evaluates to true and "not attending" if rsvp is false.
b) An if-else structure determines which food choice is printed depending on user input. If selection equals 1, beef is printed; if it is 2, chicken appears; if it is 3, pasta is displayed; any other value results in fish being printed.
c) The switch statement checks the value of selection and uses an inner if statement to assign "Thanks for attending. You will be served beef." to option1 if rsvp is true. This assignment varies with the selection's value. When rsvp is false, option1 gets the message "Sorry you can't make it."
d) An if statement evaluates whether option1 and option2 are equivalent and outputs true if they match; otherwise, it indicates false.