CSC152 2004F, Class 12: Conditionals Admin: * Homework (List interface) due. Email it to me now! (We'll discuss it later.) * New Homework for Friday: Extending Vec2D and its implementing classes Overview: * Background: * Reminder about structure of programs * Types in Java * Boolean expressions in Java * Basic conditionals: The if statement * Example: getQuadrant * Additional conditionals: The switch statement * Example: printQuadrant Background, Phase I * Reminder: Structure of Java methods public RETURN-TYPE METHOD-NAME(PARAM-TYPE PARAM-NAME) { BODY } * What can go in the body? * A return statement, that returns a value of the appropriate type (and of the appropriate value) * The declaration of a variable, typically TYPE NAME; GraduateAssistant greg; TYPE NAME = EXPRESSION; TYPE NAME = new TYPE(); GraduateAssistant greg = new GraduateAssistant(...); * Manipulate variables (objects) NAME.NAMEMETHODNAME() greg.describe(new Polar(1,2)); NAME = EXPRESSION // Name can be variable, parameter, or field this.angle = 0; * Call other methods within the same object this.METHODNAME(PARAMS) * What else could go in the body? * Conditionals: Do this or that * Loops: Do this this many times * Reports of errors (Exceptions): I can't do this because ... Observations: * Types play a central role in Java programming. Whenever you introduce a new name, you assign a type to that name. * Java has three "kinds" of types * "Primitive types" (e.g., double): Express values, don't supply methods (3.4.convertToInteger()) * Built-in classes (e.g., PrintWriter): Usually build with new, supply methods (pen.println()) * User-defined classes (e.g., Point): Usually build with new, call methods (vec.getX()). * Java provides "built in" operations for primitive types (usually with different syntax) double x; double y; z = x + y; z = x - y; z = x / y; z = x * y; * What primitive types have we encountered? * double (real) ; variants: float * int (integer) ; variants: long, short, byte * What other primitive types are there? * char (character) - the building block of strings * boolean (Boolean) - true and false /Boolean/ * Expressions that build boolean values NUMERIC_EXP < NUMERIC_EXP (less than) NUMERIC_EXP <= NUMERIC_EXP (less than or equal to) NUMERIC_EXP >= NUMERIC_EXP (greater than or equal to) NUMERIC_EXP > NUMERIC_EXP (greater than) NUMERIC_EXP == NUMERIC_EXP (equal to) NUMERIC_EXP != NUMERIC_EXP (not equal to) * You can also compare chars with all of those (uses "natural" ordering). (By UNICODE value.) * You can also compare booleans with == and != * YOU SHOULD NOT USE ANY OF THESE OPERATIONS WITH OBJECTS * For equality comparison of objects, use obj1.equals(obj2) * This will only work if you've bothered to implement it. * For other comparisons, use obj1.compareTo(obj2) Returns negative for "less than" Returns 0 for "equal to" Returns positive for "greater than" When your objects use these, you might want to indicate that they implement Comparable * You can combine Boolean expressions in three ways * or: bexp1 || bexp2 * and: bexp1 && bexp2 * not: !bexp * Question: How would you say "This point is in quadrant one?" public boolean isInQuadrantI() { return (this.x > 0) && (this.y > 0); } // isInQuadrantI() * How do you express true and false true false /Conditionals/ * Scheme: (if TEST TRUEPART FALSEPART) Meaning: Evaluate TEST; If the result holds trueish, evaluate and return TRUEPART, otherwise, evaluate and return FALSEPART * Scheme: (if 1 (+ 1 2) (+ 2 3)) * Anything that is not false "holds trueish" * Scheme: (if TEST TRUEPART) Meaning: Evaluate TEST; If the result is trueish, evaluate and return TRUEPART, otherwise return the special value # (list (if false 1) (if false 2)) * Focus in Scheme is "returning values" * Focus in Java is "doing stuff" * Simplest if statement if (TEST) { STUFF-TO-DO; } * Meaning: Evaluate the TEST. If the TEST evaluates to true (no trueish garbage), do STUFF-TO-DO. Otherwise, do nothing. In both cases, go on to the next statement. * Note: If STUFF-TO-DO is a single statement, the curly braces are not needed. if (TEST) STUFF-TO-DO; * Note: You can even write it on one line if (TEST) STUFF-TO-DO; * Observation from years of experience: Even when you start with one line, you're likely to add another. * Next simplest if statement if (TEST) { STUFF } else { OTHER-STUFF } * If the TEST holds true, do STUFF. If the TEST holds false, do OTHER-STUFF. No other TEST is permissible. * Side note: The vertical bar appears over the enter key on MathLAN keyboards. * There is no real equivalent to the cond in Java. if (TEST1) { STUFF1 } else if (TEST2) { STUFF2 } else if (TEST3) { STUFF3 } else { DEFAULT } * Exercise: How would we write a method, getQuadrant, that returns the number of the quadrant in which a Point falls? (Assume within the Point class.) /** * Determine the quadrant in which this point falls. * If the point falls in no quadrant (e.g., if it is * on an axis or at the origin), returns 0. */ public int getQuadrant() { if ((this.x > 0) && (this.y > 0)) { return 1; } else if ((this.x < 0) && (this.y > 0)) { return 2; } else if ((this.x < 0) && (this.y < 0)) { return 3; } else if ((this.x > 0) && (this.y < 0)) { return 4; } else { return 0; } } // getQuadrant() * Exercise: How would we write a method, getQuadrant, that returns the number of the quadrant in which a Polar falls? /** * Determine the quadrant in which this point falls. * If the point falls in no quadrant (e.g., if it is * on an axis or at the origin), returns 0. */ public int getQuadrant() { if ((this.angle > 0) && (this.angle < Math.PI/2)) { return 1; } ... } // getQuadrant() * Can we write 0 < this.angle < Math.PI/2 * NO