CSC152 2005F, Class 14: Conditionals (1): "Lecture" Admin: * Sorry about the reading problem * Sorry about the delay in grading * Sorry in general * Questions on the new homework? * Help available 7-8 p.m. tomorrow * Tutors 9-10 each night Overview: * Class fields and methods * Three kinds of classes * Boolean values and expressions * Conditionals * Recursion Review: * Five parts of a class: Fields, Constructors, Methods, Class Fields, Class Methods Class fields and class methods * Just like regular fields and methods EXCEPT * Preceded by the keyword "static" (appears after protection and before the type) * May not directly reference object fields or methods * Example (I hope): Create from polar coords (See Vec2D.java and TestVec2D.java) * Side note: If you provide a toString method, Java calls it when you try to print the object, EVEN IF YOU DO NOT CALL IT EXPLICITLY Observation: The Java we've seen to date suggests three ways to use classes * Main classes: Define a main method (and little more) Control the working of the program * Template classes: Define objects (fields, methods, constructors) Used by main classes to define the "players" within the program * Utility classes: Provide primarily class/static methods which manipulate other objects (e.g., Math) /On to conditionals (whee!)/ * Used to make decisions in program * Past evidence suggests that programs that don't make many decisions are, well, BORING * Decisions made with the conditional statement if (TEST) { STUFF-TO-DO-IF-TEST-SUCCEEDS } * Were we to create an "absolute value" method for vectors double resultx = this.xcoord; if (resultx < 0) { resultx = -resultx; } * Detour: A semicolon by itself represents the empty statement. * Note if (TEST) ; means "run the test and ignore the result" * Usually provide alternatives if (TEST) { STUFF-TO-DO-IF-TEST-SUCCEEDS } else { STUFF-TO-DO-IF-TEST-FAILS } * Include semicolons after each command in both succeeds and fails * No equivalent to Scheme's "cond": To do lots of tests, use lots of ifs. if (TEST1) { STUFF-TO-DO-IF-TEST1-SUCCEEDS } else if (TEST2) { STUFF-TO-DO-IF-TEST1-FAILS-AND-TEST2-SUCCEEDSs } else { STUFF-TO-DO-IF-BOTH-TESTS-FAIL } * If you don't include the first else, it treats the conditionals as separate if (TEST1) { STUFF-TO-DO-IF-TEST1-SUCCEEDS } if (TEST2) { STUFF-TO-DO-IF-TEST2-SUCCEEDS_NO_MATTER_WHAT_TEST1_DID } else { STUFF-TO-DO-IF-TEST2-FAILS } /How do we write the tests?/ * Arithmetical: ==, <, <=, >, >=, != * All infix if (a op b) * Boolean: and (&&), or (||), not (!) * && and || are infix if (bool1 && bool2) * not is prefix if (!(a op b)) * Boolean methods (normally written as combinations of the above) public boolean inFirstQuadrant() { return (x > 0) && (y > 0); //if ((x > 0) && (y > 0)) { // return true; //} //else { // return false; //} } * Expecation that many classes provide an int compareTo(Object other ) method returns a negative number if this object naturally precedes the param object returns 0 if the two objects are naturally the same returns a positive number if this object naturally follows the param BigInteger ba; BigInteger bb; ... if (ba.compareTo(bb) < 0) { pen.print(ba); pen.print(" appears to be smaller than "); pen.println(bb); }