******************************************************************************* 0) Last time- writing programs for dice and geometry - could have to write an instantiable class like this on the exam - go over Die class, ask any questions, *especially* stupid ones - main method can be in or out, does the same thing (move it) - look again at original Counter and see how it's similar - look at solution to geometry, ask any questions about that - any exam questions in general? 1) Administrative - give evals to volunteer - what were your program ideas? - hw: read 5.3-5.5 (next person), do my eval - A2 handout - if indentation problems, print from text file - if got 0/5 on lab visit, I never got your electronic- put in A2 and tell me 2) Quiz for chapter 4 to finish up 3) Conditionals: go over 5.1-5.2 in your groups 4) Highlights of material A. The if statement: when you need to decide at run-time what to do - format: (note braces and indentation) if (sideUp > NUM_SIDES/2) { S.o.p("You succeeded"); } else { S.o.p("You failed"); } - note that the "else" doesn't have a condition - the "else" is also optional - if braces aren't there, only one line below is inside block - condition is boolean expression: evaluates to true or false - note: a variable declared inside braces goes away after (scope) B. Diagrams: draw one for above - tests in diamonds, blocks in squares, arrows for T/F B. Relational operators: < <= > >= != == - the "answer" to one of these is always true or false - their operands are arithmetic expressions - all the arithmetic operators have higher precedence - try to negate statement so it checks for failure, not success if (sideUp <= NUM_SIDES/2) { //failed } C. Boolean expressions: can have more than one condition - simplest boolean expression is "true" or "false" if (true) {} -will always occur - next simplest is a single boolean variable (flag) boolean rollIsGood = (sideUp >= NUM_SIDES/2); if (rollIsGood) {} - then, one relational operator, as before - then combinations, using operators: && || ! if (age < 16 || !passDrivingTest) { //can't drive} if (a < b && b < c) { //in order} - precedence: ! is unary, so is right up with unary +/- && and || in that order below all relational operators - truth table: a b a&&b a||b !a !b !(a&&b) !(a||b) - try to negate these two statements * put all in parentheses and use ! * DeMorgan's law: ! (a&&b) --> !a || !b ! (a||b) --> !a && !b - short-circuiting: lazy evaluation * a && b: if a is false, don't have to check b * a || b: if a is true, don't have to check b * if it doesn't have to, it *never* does 5) Exercises A. Constructive 1) Use getInt(), and if entry is negative, ask again - note: if they enter a negative twice, we can't help it 2) Fill in the following method: public boolean areAnyTwoTrue(boolean a, boolean b, boolean c) return (a&&b || b&&c || a&&c); } 3) Evaluate the following expressions: 6 == 6 || x < y 2 < 4 && (false || 5 <= 4) x <= y && !(z != z) || x > y x < y || z < y && y <= z 4) Simplify the following expressions: !(x <= y && y != z) !(a == b || c == d && d > 1) done==true done==false B. Debugging: run methods in IfStatementBugs.java 6) Summary - if statements, relational operators, boolean expressions *******************************************************************************