©2004 Brooks/Cole Chapter 3 Assignment
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Assignment In the last chapter we learned how to declare variables Now, we need to learn to give them values We use the assignment operator, =, to give a variable a value variable = expression; –Compute the value of expression –Store the result in memory associated with variable
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Values Stored in the Variables factor = 10.6; weight = 155.0l; totalWeight = factor * weight;
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Reusing a variable sum = 25; sum = sum + 10;
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Accumulation We often need to accumulate values into a variable –adding up ten numbers entered by a user in order to be able to compute the average SubTotals.java –concatenating multiple strings together in successive statements BuildAString.java
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Increment and Decrement Successively adding/subtracting one to/from a variable is common enough to have a special operator –prefix increment ++n or decrement --count k = ++n;n = n + 1; k = n; –posfix increment n++ or decrement count-- k = n++;k = n; n = n + 1;
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Compound Assignment Assignments like var = var oper expr are common enough that there are special operators to express them += -= *= /= %= Examples x *= y; ---> x = x * y; count -= 1; ---> count = count - 1;
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Assignment operator = is considered to be an operator –It is right associative a = b = c = 25; ---> a = (b = (c = 25)); –It has the lowest precedence of all the operators
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Coercion What happens if the expression to the right of the = is a different type than the variable on the left? –if the variable has type with a wider range than the expression, the value of the expression will be coerced to the variables type –if the variable's type has a narrower range, the compiler will report an error double x = 3 * 5 / 4;// OK int n = ;// NOT OK
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Mixed-mode arithmetic Expressions can have more than one type in them 2 * 3.4f / 6.78; The type of the expression is that of the widest-range operand –The type of individual subexpressions is determined by the type of the two operands in the subexpression It may have a narrower range than the complete expression 2 / 3 * 2.5 has an integer division in it even though the final result is double
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Math class Math is a class in the java.lang package which contains a number of useful functions –most of the functions you find on a calculator - square root, powers, logs, trig and inverse trig functions, –a function to round floating point numbers to the nearest int –a constant (Math.PI) to represent the value of pi
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Math methods MethodDescriptionReturns abs(x)absolute valuesame type as argument pow(x1, x2)x1 x2 double sqrt(x)square root of xdouble log(x)natural log of xdouble round(x)round xint if x float, long if x double random()random number between 0.0 and 1.0 double sin(x) cos(x) tan(x) x should be in radians double
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using and Passing Data to a Math Class Method Calling syntax Math.methodName( data) Math identifies the class the method is defined in methodName identifies the method data contains the data the method is to act on –can be a literal value,a variable or an expression Examples Math.abs(-6.7) Math.sqrt(y) Math.pow( x * y + z, i * 3)
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Passing Data to the Math Class’ abs() Method
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Casting What if we need to convert a variable of one type to a narrower type? –We can use a cast operation (dataType)expression –Casting has a high precedence (int)v1 op v2 //casts v1 only –Floating point values are truncated when cast to an integer type (int)3.75// result is 3
Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Constants We often use values in a program that have a specific meaning – is the value of pi –12 is the number of months in the year Programs can be more readable if these values are given names. Since these values don't change, the should be constants –use the modifier final in a declaration of a variable that should not change –By convention, names of constants are all uppercase