0% found this document useful (0 votes)
79 views5 pages

Chapter 3 Selections

This document contains examples and explanations of Java programming concepts including: 1) Boolean expressions using comparison and logical operators; 2) If/else statements and switch statements; 3) Formatting output using format specifiers.

Uploaded by

charugesh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views5 pages

Chapter 3 Selections

This document contains examples and explanations of Java programming concepts including: 1) Boolean expressions using comparison and logical operators; 2) If/else statements and switch statements; 3) Formatting output using format specifiers.

Uploaded by

charugesh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 3 Selections

1. 2. <, <=, ==, !=, >, >= (true) && (3 > 4) false !(x > 0) && (x > 0) false (x > 0) || (x < 0) true (x != 0) || (x == 0) true (x >= 0) || (x < 0) true (x != 1) == !(x == 1) true 3. (x > 1) && (x < 100) 4. 5. ((x > 1) && (x < 100)) || (x < 0) x > y > 0 incorrect x = y && y incorrect x /= y correct x or y incorrect x and y incorrect 6. 7. x is 2. No. Boolean values cannot be cast to other types.

8. x is 1. 9. d d false -4 10. age > 13 && age < 18 11. weight > 50 || height > 160. 12. weight > 50 && height > 160. 13. weight > 50 ^ height > 160. 14. Note: else matches the first if clause. No output if x = 3 and y = 2. Output is z is 7 if if x = 3 and y = 4. Output is x is 2 if if x = 2 and y = 2.

true

x>2

false

y>2

false

System.out.println("x is " + x);

true int z = x + y; System.out.println("z is " + z);

15. a, c, and d are the same. (B) and (C) are correctly indented.

16. No output if x = 2 and y = 3. Output is x is 3 if x = 2 and y = 2. Output is z is 6. 17. Yes. 18. 19. 0.5, 0.0, 0.234 (int)(Math.random() * 20) 10 + (int)(Math.random() * 10) 10 + (int)(Math.random() * 41) 20.
if (y > 0) x = 1;

21.
if (score > 90) pay *= 1.03;

22.
if (score > 90) pay *= 1.03; else pay *= 1.01;

23.
newLine = (count % 10 == 0);

24. Switch variables must be of char, byte, short, or int data types. If a break statement is not used, the next case statement is performed. You can always convert a switch statement to an equivalent if statement, but not an if statement to a switch statement. The use of the switch statement can improve readability of the program in some cases. The compiled code for the switch statement is also more efficient than its corresponding if statement. 25. 26. y is 2. switch case case case case (a) { 1: x += 2: x += 3: x += 4: x += 5; break; 10; break; 16; break; 34;

a is 1

x += 5;

break

a is 2 x += 10; a is 3 x += 16; a is 4 x += 34; break break break

27.

switch case case case case case case case }

(day) { 0: dayName 1: dayName 2: dayName 3: dayName 4: dayName 5: dayName 6: dayName

= = = = = = =

"Sunday"; break; "Monday"; break; "Tuesday"; break; "Wednesday"; break; "Thurday"; break; "Friday"; break; "Saturday"; break;

28. System.out.print((count % 10 == 0) ? count + "\n" : count + " ");


pay = (temperature > 90) ? pay * 1.5 : pay * 1.1;

29.

30. The specifiers for outputting a boolean value, a character, a decimal integer, a floating-point number, and a string are %b, %c, %d, %f, and %s. 31. (a) the last item 3 does not have any specifier. (b) There is not enough items (c) The data for %f must a floating-point value 32. (a) amount is 32.320000 3.233000e+01

(b) (c) (d) (e) (f) 33. 34.

amount is 32.3200 3.2330e+01 *false // * denote a space **Java // * denote a space false*****Java *falseJava Use the String.format method to create a formatted string.

The precedence order for boolean operators is ^, &&, and ||


is true is true

true || true && false true && true || false

35. 36. 37.

True both are false To display a confirmation dialog box, invoke JOptionPane.showConfirmDialog(null, Prompting message). The method returns an int value: 0 if the Yes button is clicked, 1 if the No button is clicked, and 2 if the Cancel button is clicked,

You might also like