Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that an int variable age has been declared and already given a value and

ID: 3649012 • Letter: A

Question

Assume that an int variable age has been declared and already given a value and assume that a char variable choice has been declared as well. Assume further that the user has just been presented with the following menu:

S: hangar steak, red potatoes, asparagus
T: whole trout, long rice, brussel sprouts
B: cheddar cheeseburger, steak fries, cole slaw
(Yes, this menu really IS a menu!)

Write some code that reads a single character (S or T or B) into choice . Then the code prints out a recommended accompanying drink as follows:

If the value of age is 21 or lower, the recommendation is "vegetable juice" for steak, "cranberry juice" for trout, and "soda" for the burger. Otherwise, the recommendations are "cabernet", "chardonnay", and "IPA" for steak, trout, and burger respectively. Regardless of the value of age , your code should print "invalid menu selection" if the character read into choice was not S or T or B.

I have this much:
choice = stdin.next();
if(age<=21)
switch(choice){
case 'S':
System.out.println("vegetable juice");
break;
case 'T':
System.out.println("cranberry juice");
break;
case 'B':
System.out.println("soda");
break;
default:
System.out.println("invalid menu selection");
break;
}
else if (age>21)
switch(choice){
case 'S':
System.out.println("cabernet");
break;
case 'T':
System.out.println("chardonnay");
break;
case 'B':
System.out.println("IPA");
break;
default:
System.out.println("invalid menu selection");
break;
}
I need to know how to set choice that wont result in an error:
CTest.java:12: error: incompatible types
case 'S':
^
required: String
found: char
CTest.java:15: error: incompatible types
case 'T':
^
required: String
found: char

Explanation / Answer

Well, three differences between your code and the assignment are immediately apparent: 1. The assignment says "if the value of age is 21 or lower", but your if statement is saying "if the value of age is lower than 21". If age is exactly 21, your code doesn't do the right thing. 2. The assignment says that for underage users the recommendation for "T" is "cranberry juice". You have it as "cabernet". 3. Your if statement at the end is always going to return true. It returns true if choice is not S, or not T, or not B. It's always not one of those things, so it's always going to return true. You probably meant to use && (and) rather than || (or).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote