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

Pages 82-99 -> Switch, Case, and Break (Java programming for beginners) 1)p85 is

ID: 3750271 • Letter: P

Question

Pages 82-99 -> Switch, Case, and Break (Java programming for beginners)

1)p85 is a perfect example of why I am so vocally against switch statements. They are just not extendable. What if choice 5 becomes "red with white trim". Then choice 2 becomes "green, unless we are out of blue cars". Creating an artificially simple program snippet that is designed to not change as a way to justify switch statements is ridiculous. In real life, a month from now you are going to want to make a change, and the moment you want to do anything more complicated than a single check, you will wish you had ifs. The answer to this question is banana.

2)What does the shortcut i-- do?  

3)What horrible thing will happen if you run the code at the bottom of page 90 under "while loops"?  

4)What is the difference between next and nextLine when using the Scanner? (It's equivalent to the difference between print and println when using System.out) p

5)How many times would the loop "for(int i = 0; i < 14; i++)" run?  

Explanation / Answer

We are allowed to answer only 4 parts. You have 5 parts and the question 3 referring to book which we don't know. So I am providing answer to other parts. In case of any doubts, please comment.

5)

It will run for 14 times. i = 0 to 1 = 13.

Execution order:

i = 0; i <14; then body will be executed.

i increments by i++.

The above two lines repeats until i is 14.

4)

next key word reads until next space.

nextLine reads entire line.

It is not exactly same. print prints all the data in the same line. No delimiter will be inserted between different print statements.

print(1);

print(2);

It prints 12.

println(1);

println(2);

prints

1

2.

2) It decrement i value by 1. It is same as i = i - 1;

1)

You can have if/else if your choices are less. If your choices are more i.e 100 or so, then go for switch. It will look up in constant time. Even in ifs also, code is constant only.

if(choice5.equals("red with white trim")) {

}

If you want to check green with white trim later, you have to add a if or change existing if.

In case of any doubts, please comment.