(Solved) : Pick Switch Statement Reading Put Main Java Program Make Work Attach Java File Copyandpast Q44003079 . . .
Pick a switch statement from the reading. Put it into amain() Java program and make it work.
- Attach the .java file and
- copyAndPaste the output into the WriteSubmissionBox
The switch Statement
S1.51
Multiway if-else statements can become unwieldy when you mustchoose from among many possible courses of action. If the choice isbased on the value of an integer or character expres- sion, theswitch statement can make your code easier to read.
The switch statement begins with the word switch followed by anexpression in parenthe- ses. This expression is called thecontrolling expression. Its value must be of type int, char, byte,short, or String. The switch statement in the following exampledetermines the price of a ticket according to the location of theseat in a theater. An integer code that indicates the seat locationis the controlling expression:
int seatLocationCode;<CodehereassignsavaluetoseatLocationCode > .. .
double price = −0.01;
switch (seatLocationCode)
{
case 1: System.out.println(“Balcony.”); price = 15.00;
break;
case 2: System.out.println(“Mezzanine.”); price = 30.00;
break;
case 3: System.out.println(“Orchestra.”); price = 40.00;
break;
default:
System.out.println(“Unknown ticket code.”); break;
} // end switch
The switch statement contains a list of cases, each consistingof the reserved word case, a constant, a colon, and a list ofstatements that are the actions for the case. The constant afterthe word case is called a case label. When the switch statementexecutes, the controlling expres- sion—in this example,seatLocationCode—is evaluated. The list of alternative cases issearched until a case label that matches the current value of thecontrolling expression is found. Then the action associated withthat label is executed. You are not allowed to have duplicate caselabels, as that would be ambiguous.
If no match is found, the case labeled default is executed. Thedefault case is optional. If there is no default case, and no matchis found to any of the cases, no action takes place. Although thedefault case is optional, we encourage you to always use it. If youthink your cases cover all the possibilities without a defaultcase, you can insert an error message or an assertion as thedefault case. You never know when you might have missed someobscure case.
Notice that the action for each case in the previous exampleends with a break statement. If you omit the break statement, theaction just continues with the statements in the next case until itreaches either a break statement or the end of the switchstatement. Sometimes this feature is desirable, but sometimesomitting the break statement causes unwanted results.
Expert Answer
Answer to Pick a switch statement from the reading. Put it into a main() Java program and make it work. Attach the .java file and…
OR