CSC153, Class 33: Conditionals in Java Overview: * Review of Java to date * Boolean expressions * The if statement * The switch statement * Lab: J4.2 and J4.4 Notes: * Exam 1 to be returned tomorrow. * Latest homework still not graded. * Meet in Saints Rest sometime next week? (Probably next Tuesday) * Read about loops (J5). * Looking ahead: This week is Java stuff. Next week we start talking about data structures. (After that, I may rearrange things.) ---------------------------------------- Review: What do you remember about Java? * Forgotten: How do I edit, compile, and all that stuff. + Edit: Either (1) open a terminal window and type gedit + To compile: In the terminal window, type javac asdasdfasd.java produces asdasdfasd.class + To run, java asdasdfasd (Don't type .class or .java) + If this doesn't work, learn the mystical command * Remember: Hatred (it's not DDR or Scheme) To create program, you need to create one or more classes. One of those classes must include a method called main public static void main(String[] args) { } // main(String[]) To create a class public class ClassName { // Other functions, which are typically not called main // Fields, which can be accessed by those functions // Constructors, special functions which tell how to // set up a new object } // class ClassName Sam will fail you if you don't document well You can write tests in Java if (some-silly-expression-that-tests-something) { stuff-to-do-if-test-holds } else { stuff-to-do-if-test-doesn't-hold } stuff-to-do-always * The else is optional. * Is there an equivalent to Scheme's cond? No. Write "nested ifs" if (some-silly-expression-that-tests-something) { stuff-to-do-if-test-holds } else if (some-other-expression) { other-stuff-to-do } ... else { stuff-to-do-if-test-doesn't-hold } stuff-to-do-always * Java and C++ have the less-than-amazing switch statement switch (expression) { case val1: stuff-to-do-if-expression-matches-val1; break; case val2: stuff-to-do-if-expression-matches-val2; break; ... } Observation: If you don't write a break, the control continues into the next case Restrictions: Can you write case "Evan": case "Katherine": No. You can only use "enumerated" types: integers (and thier variants), Booleans, chars, ... No doubles or floats (approximated, so what does it mean to be equal?) Can you write: case func(adfas): xxx No. The values must be computable at *compile* time. Why? Possible to implement really efficiently. (Take Compilers or CSC195/CSC201 or both for more details.) ---- What kinds of things can you test in the if? if (test) * Basic numeric tests: (a < b), (a > b), (a <= b) (a == b), (a != b) * Boolean combinations And: && (infix) Or: || (infix) Not: ! (prefix) * Many classes have an equals(Object other) method * Many classes provide a wide variety of predicates (which, unfortunately, cannot end in a question mark) * In Scheme, if it's not false, it's true. * In Java, if it's not true and not false, it's an error * How is question mark used in Java? ( a ? b : c) If a holds, then use the value of b, otherwise use the value of c (if expression rather than an if statement) max = (a > b ? a : b); Lab J4.2, J4.4