Progress Checks: Chapter 6

1. Identify the invalid if statements

 

a. if (a < b) then

            x=y;

    else

            x=z;

 

b. if (a < b)

            else x= y;

 

c. if (a < b)

            x= y;

            else {

            x= z;

            };

 

d. if (a < b) {

            x= y; } else

            x= z;

 

2. Express the following if-then statements using if-then-else.

 

a. if (a < b) x= y;

 

b. if (a < b) {}

 

c. if (a < b) x= y;

    if (a >= b) x=z;

 

3. Using a message box, create if-else statements that sets no car crashes to print a "Safe Driver," at least one crash "Insurance increases" and if the number of car crashes is less than 0 "Prevention."

 

4. Indent the following if statements properly.

a. if (a==b) if (c==d) a=1; else b=1; else c=1;

b. if (a == b) a = 1; if (c == d) b = 1; else c = 1;

c. if (a == b) {if (c == d) a = 1; b = 2; } else b = 1;

d. if (a == b) {

            if (c == d) a = 1; b = 2; }

            else {b = 1; if (a == d) d = 3;

            else c = 1;}

 

5. Which two of the following three if statements are equivalent?

a. if (a == b)

            if (c == d) a = 1;

            else b = 1;

b. if (a == b) {

            if (c == d) a = 1; }

            else b = 1;

c. if (a == b)

            if (c == d) a = 1;

            else b = 1;

 

6. Evaluate the following boolean expressions. For each of the following ex-pressions,

assume x is 10, y is 20, and z is 30. Indicate which of the fol-lowing

boolean expressions are always true and which are always false,

regardless of the values for x, y, or z.

a. x < 10 || x > 10

b. x > y && y > x

c. (x < y + z) && (x + 10 <= 20)

d. z - y == x && Math.abs(y - z) == x

e. x < 10 && x > 10

f. x > y || y > x

g !(x < y + z) || !(x + 10 <= 20)

h. !(x == y) && (x != y) && (x < y || y < x)

 

7. Express the following switch statement using nested-if statements.

switch (grade) {

            case 10:

            case 9: a = 1;

                        b = 2;

                        break;

            case 8: a = 3;

                        b = 4;  

                        break;

            default: a = 5;

                        break;

}

 

8. Write an if statement to find the smallest of three given integers without

using the min method of the Math class.

 

9. Draw control flow diagrams for the following two switch statements.

a. switch (choice) {

            case 1: a = 0;

                        break;

            case 2: b = 1;

                        break;

            case 3: c = 2;

                        break;

            default: d = 3;

                        break;

}

b. switch (choice) {

            case 1: a = 0;

            case 2: b = 1;

            case 3: c = 2;

            default: d = 3;

}

 

10. Write an if statement that prints out a message based on the following

rules:

If the Total Points Are Message to Print

            >=100 You won a free cup of coffee.

            >=200 You won a free cup of coffee and

                        a regular-size doughnut.

            >=300 You won a free cup of coffee and

                        a regular-size doughnut and

                        a 12-oz. orange juice.

            >=400 You won a free cup of coffee and

                        a regular-size doughnut and

                        a 12-oz. orange juice and

                        a combo breakfast.

            >=500 You won a free cup of coffee and

                        a regular-size doughnut and

                        a 12-oz. orange juice and

                        a combo breakfast and

                        a reserved table for one week.

 

4. What's wrong with the following switch statement?

 

switch (N){

            case 0:

            case 1: x= 11;

                        break;

            default: System.out.println("Switch Error");

                        break;

            case 2: x= 22;

                        break;

            case 1: x= 33;

                        break;

}

 

5. What's wrong with the following switch statement?

 

switch (ranking) {

            case > 4.55:

                        pay=pay*.20;

                        break;

            case= 4.55:

                        pay = pay * .15;

                        break;

            default:

                        pay= pay * .05;

                        break;

}