Unit 3 - Boolean Expressions and if Statements
- 3.1 -
- 3.2 -
- 3.3 -
- 3.4 -
- 3.5 -
- 3.6 -
- 3.7 -
- Coding Challenge 1 -
- Coding Challenge 3 -
- Possible CollegeBoard Test Question Topics:
- Scavenger Hunt (done on paper)
- HW: 2019 FRQ 1:
3.1 -
- Boolean Expressions: represent logic and tell whether something is true or false
- Operators used to create Booleans
- "==" (same value/equals to)
- "!=" (checks for inequality)
- "<" (less than)
- "<=" (less than or equal to)
- ">" (greater than)
- ">=" (greater than or equal to)
3.2 -
- Conditional Statements - perform computations depending on whether a Boolean condition evaluates to true or false
- If Statements - if statement occurs if a block of code is run only if the condition is true
3.3 -
- If-Else Statements - statement to run a block of code among more than one alternative
3.4 -
- Else-if Statements - statement to run a condition if the original condition was false
3.5 -
- Nested if statements - If-statements within if-statements
- Note - If the outer if-statement evaluates to false, the inner if-statements are not evaluated.
- Logical operators - Used to combine Boolean expressions
- && - and
- || - or
- ! - not
- Short-circuited evaluation: The result of a compound Boolean expression can be determined just by looking at a few expressions.
3.6 -
- De Morgan's laws: Help simplify Boolean expressions
- !(a&&b) = (!a || !b)
- !(a || b) = (!a && !b)
3.7 -
- Use '==' to see if two object references are aliases for the same object or to see if an object is null
- Use '.equals()' to see if the attributes of two objects are the same
Coding Challenge 1 -
- my answer: 1 4 5 6 7 8 9
Coding Challenge 3 -
- my answer: true false true true
Possible CollegeBoard Test Question Topics:
- equals vs == operator (The == operator will only check if pointing at the same objects)
- if only using 1 line of code, {} is not needed between if-statements
- conditionals must have if or else-if to latch onto
- conditionals and control structures can be nested
Scavenger Hunt (done on paper)
- my answer: Mr M's Desk
public class FrogSimulation
{
/** Distance, in inches, from the starting position to the goal. */
private int goalDistance;
/** Maximum number of hops allowed to reach the goal. */
private int maxHops;
/** Constructs a FrogSimulation where dist is the distance, in inches, from the starting
* position to the goal, and numHops is the maximum number of hops allowed to reach the goal.
* Precondition: dist > 0; numHops > 0
*/
public FrogSimulation(int dist, int numHops)
{
goalDistance = dist;
maxHops = numHops;
}
/** Returns an integer representing the distance, in inches, to be moved when the frog hops.
*/
private int hopDistance()
{ /* implementation not shown */ }
/** Simulates a frog attempting to reach the goal as described in part (a).
* Returns true if the frog successfully reached or passed the goal during the simulation;
* false otherwise.
*/
public boolean simulate()
{ /* to be implemented in part (a) */ }
/** Runs num simulations and returns the proportion of simulations in which the frog
* successfully reached or passed the goal.
* Precondition: num > 0
*/
public double runSimulations(int num)
{ /* to be implemented in part (b) */ }
}
/** Simulates a frog attempting to reach the goal as described in part (a).
* Returns true if the frog successfully reached or passed the goal during the simulation;
* false otherwise.
*/
public boolean simulate() {
int position = 0; // frog starts at 0
for (int count = 0; count < maxHops; count++) { // maxHops is the max the frog can hop, and after each hop the count goes up by one, the loop repeating until count = maxHops
position += hopDistance();
if (position >= goalDistance) { // if the frog goes farther than needed, return true
return true;
}
else if (position < 0) { // if the frog isn't there, return false and keep going
return false;
}
}
return false; // if the hops are taken up without the goal being met, it returns false
}
/** Runs num simulations and returns the proportion of simulations in which the frog
* successfully reached or passed the goal.
* Precondition: num > 0
*/
public double runSimulations(int num) { // double makes it so the value can be a decimal
int countSuccess = 0; // starts at 0
for (int count = 0; count < num; count++) { // checks if the simulation was successful for each time it was run
if(simulate()) { // if it turns out true, raise the count of the number of successful runs
countSuccess++;
}
}
return (double)countSuccess / num; // divides the number of successes to the overall amount of simulations to find the final result
}