Fri October 4 lecture notes (1) Robots (2) Java Recap: * Classes: define behavior of type of "object", has data and methods * Methods: defining them, calling them, passing arguments to them * Variables: different types (int, double, String, ...) of data * Arguments: are a temporary name in definition, passed when called like in algebra formulas, i.e. f(x)=x+1 // here "x" is a temporary name f(2) // which is given a value In Java, this is --- in file Function.java --- Class Function { public double f(double x){ return x+1; } } --- in TestFunction.java --- class TestFunction { public static void main( String[] args ) { Function thisFunc = new Function(); double answer = thisFunc.f(2); System.out.println( " f(2) is " + answer ); } } Compile with " javac Function.java; javac TestFunction.java " Run with "java TestFunction" * Invoking methods : object.method(arguments) An example: ---- file GuyWhoLikesToTalk.java ----- // An object which can print out a string several times. public class GuyWhoLikesToTalk { // constructor for PrintStuff object GuyWhoLikesToTalk(){ } // A method of this object. // In other words, a recipe for something that a PrintStuff can do. public void sayThreeTimes(String whatToSay){ System.out.println( " 1st time: " + whatToSay ); System.out.println( " 2nd time: " + whatToSay ); System.out.println( " 3rd time: " + whatToSay ); } // Another method. public void tellAJoke(){ System.out.println( " Well, this guy walks into a bar. Thud. "); } } ---- file TestPrintStuff.java --------- // A class which calls the PrintStuff object public class TestPrintStuff { // main() method, i.e. what gets called when the program runs. public static void main( String[] args ){ PrintStuff george = new PrintStuff(); String hello = "Hello out there in the real world." System.out.println(" george will now speak. Say hello, george."); george.sayThreeTimes( hello ); System.out.println(" george will now tell a joke."); george.tellAJoke(); } } (3) New Stuff - see assignment (a) "if" statement int i=3; if ( i<5 ) { System.out.println( " i is less than 5. " ); } else { System.out.println( " i is not less than 5. " ); } The computer ignores the indentation. You shouldn't. (b) boolean expressions - evaluate to "true" and "false" double jimsHeight = 6.3; boolean jimIsSixFeetTall = ( jimsHeight >= 6.0 && jimsHeight <= 7.0 ); Do examples in class with <= >= == tests equality != && logical "and" || logical "or" ! logical "not" ( ) grouping WARNING: DO NOT TYPE "if (i=2) ... ", but instead use "if (2==i) ..." Talk a bit about the algebra of logic operators. (c) Equality of objects is different than equality of their values. Some classes (i.e. String) have methods for testing equality. String a = "Hello."; String b = "Hello."; if ( a == b ) { System.out.println( " 'a' and 'b' are the same. "); } else { System.out.println( " 'a' and 'b' are different. "); } as opposed to if ( a.equals(b) ) { System.out.println( " The values of 'a' and 'b' are the same."); } else { System.out.println( " The values of 'a' and 'b' are different.");} Note also the string.equalsIgnoreCase() method. (d) "while" syntax - loops int i=1; while ( i<=10 ) { System.out.println( " 'i' is " + i ); i = i+1; } (e) increment, decrement operators. i++; i--; (f) temporary variables within {} blocks. (This is called "scope".) ( This is an advanced topic. ) int i = 3; // what is the value of i here ? { int i=5; // what is the value of i here ? } // what is the value of i here? (And which 'i' is it?)