Question: Question 1 Rewrite the code snippets below as specified in each part. Where necessary, you may assume that all involved variables and/or methods are already

Question 1

Rewrite the code snippets below as specified in each part. Where necessary, you may assume that all involved variables and/or methods are already properly defined and/or initialized.

Submit your answers directly in the answer box, making sure to mark clearly the answers to the three parts.

Part A (5 pts):

The following code snippet assigns a value to the int variable price, depending on the String object item:

if (!item.equals("car"))
 price = 190;
else
 price = 12000;

Transform the above logic into a single equivalent assignment statement, using the conditional operator ?:.

Part B (10 pts):

The following code snippet calls a different method according to the value of the int variable choice:

if (choice == 1)
 retry();
else if (choice == 2 || choice == 3)
 proceed();
else
 goBack();

Transform the above logic into an equivalent switch statement.

Part C (10 pts):

In the following code snippet, total is an int variable:

int num = total;
while (num >= 0) {
 System.out.println(num);
 num = num - 2;
}

Transform the above into an equivalent for loop.

For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!