Unit 7 ShortCircuit Evaluation De Morgans Laws The












- Slides: 12
Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math. random() 1 AP Computer Science A – Healdsburg High School
Short-Circuit Evaluation if (condition 1 && condition 2). . . If condition 1 is false, then condition 2 is not evaluated (the result is false anyway) if (condition 1 || condition 2). . . If condition 1 is true, then condition 2 is not evaluated (the result is true anyway) 2 AP Computer Science A – Healdsburg High School
De Morgan’s Laws One could think of it as the “Distributive Property” for Logic ! (p && q) evaluates to ( !p || !q ) ! (p || q) evaluates to ( !p && !q ) 3 AP Computer Science A – Healdsburg High School
The switch Statement switch (expression) { case value 1: . . . break; switch case default break case value 2: . . . break; . . . default: . . . break; Reserved words Only works with: - int - char 4 - enum } AP Computer Science A – Healdsburg High School Don’t forget breaks!
The switch Statement (cont’d) • The same case can have two or more labels. For example: 5 switch (num) { case 1: case 2: System. out. println(“Buckle my shoe"); break; case 3: case 4: System. out. println(“Shut the door"); break; AP Computer Science A – Healdsburg High School …
The Cast Operator • Java allows a programmer to “cast” a piece of data to a different type when needed. • Can be useful if working with int values and a decimal result is needed. For example, one might want the decimal equivalent of a fraction even though the numerator and denominator are integers. Example: dec. Answer = (double)my. Denominator / (double)my. Numerator; 6 AP Computer Science A – Healdsburg High School
Math. Random() • returns a double value 0 ≤ x < 1 • Can then be scaled to fit the needs of your application. • For example, if you needed a random number from 1 to 12, you would do something like this: rand. Num = (int) (12 * Math. Random() ) + 1; 7 AP Computer Science A – Healdsburg High School
The Craps Project 8 AP Computer Science A – Healdsburg High School
The Craps Project (cont’d) Your job 9 AP Computer Science A – Healdsburg High School
The Craps Project (cont’d) Step 1: Test your Craps. Game class separately using the class Craps. Test 1 provided to you. Craps. Test 1 Craps. Game 10 AP Computer Science A – Healdsburg High School
The Craps Project (cont’d) Step 2: Use your Die and Craps. Game classes in the Craps. Stats application. Craps. Stats Craps. Game 11 Die AP Computer Science A – Healdsburg High School
The Craps Project (cont’d) Step 3: Finish the Rolling. Die class (the draw. Dots method). Integrate Craps. Game and Rolling. Die into the Craps program. 12 AP Computer Science A – Healdsburg High School