Upcoming SlideShare
×

# Ch05 Visual Aids

530 views

Published on

Eclipse

Published in: Education, Sports, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
530
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Refer to page 105 in the text.
• Refer to page 105 in the text. Relational operators are used to form Boolean expressions. Boolean expressions evaluate to true or false.
• Refer to pages 106 and 107 in the text.
• Refer to page 107 in the text.
• Refer to page 108 in the text.
• Refer to page 109 in the text.
• Refer to pages 110 and 111 in the text. The Random class actually generates pseudorandom (like random) numbers. Java implements the Linear Congruential Method to ensure that generated numbers can be considered random for most applications.
• Refer to page 112 in the text. Compound Boolean expressions use more than one Boolean expression to determine if a condition is true or false.
• Refer to page 112 in the text.
• Refer to page 112 in the text.
• Refer to page 112 in the text.
• Refer to page 113 in the text.
• Refer to page 115 in the text. The diamond flowchart symbol indicates a decision.
• Refer to page 115 in the text. The RPS flowchart illustrates the application solution. Solution steps include: 1. prompting the user for a number between 1 and 3 2. generating a random number between 1 and 3 3. comparing the generated number to the number typed by the user 4. determining a winner and displaying an appropriate message
• ### Ch05 Visual Aids

1. 1. Chapter 5 The if Statement <ul><li>Conditional control structure, also called a decision structure </li></ul><ul><li>Executes a set of statements when a condition is true </li></ul><ul><li>The condition is a Boolean expression </li></ul><ul><li>For example, the statement if (x == 5) { y = 20; } assigns the value 20 to y only if x is equal to 5. </li></ul>
2. 2. Chapter 5 Relational Operators Operator Meaning == equal < less than <= less than or equal > greater than >= greater than or equal != not equal
3. 3. Chapter 5 The if-else Statement Contains an else clause that is executed when the if condition evaluates to false. For example, the statement if (x == 5) { y = 20; } else { y = 10; } assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5.
4. 4. Chapter 5 Nested if-else Statements <ul><li>Should be indented to make the logic clear. </li></ul><ul><li>Nested statement executed only when the branch it is in is executed. For example, the statement if (x == 5) { y = 20; } else { if (x > 5) { y = 10; } else { y = 0; } } evaluates the nested if-else only when x is not equal to 5. </li></ul>
5. 5. Chapter 5 The if-else if Statement <ul><li>Used to decide among three or more actions. </li></ul><ul><li>Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement if (x < 5) { y = 20; } else if (x < 10) { y = 40; } else if (x < 15) { y = 80; } would give very different results if the conditions were ordered differently. </li></ul>
6. 6. Chapter 5 The switch Statement <ul><li>Used to decide among three or more actions. </li></ul><ul><li>Uses an expression that evaluates to an integer. </li></ul><ul><li>The break statement moves program execution to the next statement after the switch . </li></ul><ul><li>The default code is optional and is executed when none of the previous cases are met: switch (numLegs) { case 2: System.out.println(&quot;human&quot;); break; case 4: System.out.println(&quot;beast&quot;); break; case 8: System.out.println(&quot;insect&quot;); break; default: System.out.println(&quot;???&quot;); break; } </li></ul>
7. 7. Chapter 5 The Math Class <ul><li>Part of the java.lang package </li></ul><ul><li>The random() methods generates a double between 0 and 1.0. For example, double rNum; rNum = Math.random(); </li></ul><ul><li>A random integer in a range is generated by using the expression: (highNum – lowNum + 1) * Math.random() + lowNum </li></ul>
8. 8. Chapter 5 Compound Boolean Expressions <ul><li>More than one Boolean expression in a single condition. </li></ul><ul><li>Formed using the logical And ( && ), logical Or ( || ), or logical Not ( ! ) operators. </li></ul>
9. 9. Chapter 5 And Truth Table False False False False True False False False True True True True Result Exp2 Exp1 And
10. 10. Chapter 5 Or Truth Table False False False True True False True False True True True True Result Exp2 Exp1 Or
11. 11. Chapter 5 Not Truth Table True False False True Result Exp Not
12. 12. Short Circuit Evaluation <ul><li>Java uses Short Circuit Evaluation to determine the result of a compound Boolean expression </li></ul><ul><ul><li>Left side is evaluated first </li></ul></ul><ul><ul><li>If the result of the entire expression can be determined by the value of the left operand, then no other operands will be evaluated </li></ul></ul>
13. 13. Order of Operations <ul><li>Not (!) evaluated first </li></ul><ul><li>And (&&) evaluated second </li></ul><ul><li>Or (||) evaluated last </li></ul>
14. 14. Quarter 2 Drill # 4 <ul><li>size=100 weight=50 value=75 </li></ul><ul><li>size > 50 && weight ==50 t or f? </li></ul><ul><li>size > 50 || weight ==50 t or f? </li></ul><ul><li>Which one of the above can be Short Circuited? </li></ul>
15. 15. Quarter 2 Drill # 4 <ul><li>size=100 weight=50 value=75 </li></ul><ul><li>!(value<75) </li></ul><ul><li>size >=100 || value >=100 </li></ul><ul><li>Can #2 be short circuited? </li></ul>
16. 16. Quarter 2 Drill # 4 <ul><li>size=100 weight=50 value=75 </li></ul><ul><li>value < 100 && !(weight==50) </li></ul><ul><li>!(size>100 && weight>50 && value>75) </li></ul><ul><li>(value<125 || weight<76) && size==100 </li></ul>
17. 17. Quarter 2 Drill # 4 <ul><li>Write the following: </li></ul><ul><li>Value not greater than 75 or size not greater than 100 </li></ul>
18. 18. Chapter 5 The Math Class <ul><li>Part of the java.lang package </li></ul><ul><li>Methods include: </li></ul><ul><ul><li>abs(num) returns the absolute value of num </li></ul></ul><ul><ul><li>pow(num1, num2) returns num1 raised to the num2 power </li></ul></ul><ul><ul><li>sqrt(num) returns the square root of num, where num is a positive number </li></ul></ul>
19. 19. Chapter 5 Flowchart Symbols decision
20. 20. Chapter 5 The RPS Flowchart