CS151 lecture notes, Fall 1999 Week 2, Friday Reading: Chapter 3 Notes on Homework ----------------- What are variables good for? 1) give names to values -- improve readability int totalSeconds = 24 * 60 * 60; 2) store the results of a computation and use it repeatedly int secondsSince = hour * 3600 + minute * 60 + second; 3) make programs versatile: can deal with any time/date (this notion of versatility gets expanded in Cha 3) Style is important! ------------------- INDENTATION makes programs readable. Avoids syntax errors VARIABLE NAMES syntax and semantics SIMPLICITY write code that is obviously correct You don't need me to tell you if your programs are correct. The compiler and run-time system do that. The most useful thing I can contribute is suggestions on the practice of programming efficiently and accurately. Ok, on to some new Java ... Initialization -------------- You can declare and assign a variable at the same time: int x = 1; String empty = ""; Convenient. Doubles ------- New type in Java: double. double is short for "Double-precision floating-point" You don't have to know what that means, but you should know that I sometimes refer to doubles as floating-point numbers. Everything about doubles works like the other types: double pi; DECLARATION pi = 3.14159; ASSIGNMENT Just as there are integer and String values, there are double values. How can the compiler tell the difference between an integer value and a double value? int x = 1.1; // not legal because left is an int and right is double Strictly, integers are not a subset of the doubles, but often Java lets you get away with things. double y = 1; // shouldn't be legal, but in fact it is. Java implicitly converts the value 1 to the value 1.0 Implicit type conversion is sometimes handy, but it can be a source of confusion: double y = 1 / 3; Does not do what you expect. In this class, I will teach a fairly strict type system. You will probably relax it over time. Operations ---------- + - * / all do what you expect. The hardware for floating-point addition is totally different from the hardware for integer addition. They are really not the same operation at all, at least in mechanism. But they are abstractly the same, in the sense that they both represent the same mathematical operation. Thought question: why are integer and floating-point addition the same, but String concatenation is not. Answer: in addition, is there is correspondence between the operands, then there is correspondence between the results. Math library ------------ Java has built-in methods for evaluating common Math functions like sine, cosine, sqrt, etc. The way you invoke these methods is similar to the way you invoke println System.out.println ("Hello"); Math.cos (3.14159); An important difference, though, is that the Math methods are EXPRESSIONS: which means they have a type and a value. The type is usually double; the value is the result of the computation. double root = Math.sqrt (17.0); double angle = 1.5; double height = Math.sin (angle); Composition: you can use Math methods as part of larger expressions. Also, the value in parentheses is called an argument. The argument can be any expression, as long as it has type double. What happens when you invoke a method? ------------------------------------- 1) the argument(s) get evaluated 2) the arguments get "passed" to the named method 3) the method might do something (like print) 4) the method might "return" a value 5) the returned value gets used as part of a larger expression, or maybe assigned to a variable Can you invoke a method and then use the return value as an argument for another method? System.out.println (Math.cos (angle)); Sure! What's the name I use for this sort of capability? Hint: read method invocations from inside to out. ----- Next week: creating new methods! Read 3.3 through 3.6 repeatedly and carefully.