CS 110 Notes – Basics II 1 Copyright C. Tanner 2010 • More on Assignment operators o Variable = expression; o Evaluate the expression and then store the result as the value of variable o Compound operators += -= /= *= %= Variable compound operator expression; a += 5 Î a = a+5; a*= 5 Î a = a * 5; a *= 5 + c; Î a = a * (5+c); o Page 111 #20 (3rd edition) Page 99 #20 (4th edition) Rewrite the assignment statement using a compound operator a). x = 2 * x; b). x = x + y -2; c). sum=sum + num; do d,e for non-collected homework • Increment/Decrement Operators o a = a+1; o a = a-1; o common, often used operation o built in machine instructions to cover these inc, dec o a++; Î a = a+1; o a--; Î a = a-1; o syntax allows ++a or a++, --a or a— o can use it within an expression a = b--; a = --b; these two are *not* equivalent never ++ or -- except in a stand alone statement • Math Library o Collection of methods for all elementary math functions o Such as, pow, sqrt, floor, ceil, abs, sin, cos etc o To use qualify the method with Math. o . – qualifying operator (member access operator)CS 110 Notes – Basics II 2 Copyright C. Tanner 2010 Math.pow(x,y) • In the math library use the method called pow • The . qualifies that pow can be found in the Math library • System.out.print – the print function for the out buffer from System class • Sample Programs --- Invest.java, Standard.java, Circlem.java • Random Numbers o Often times need to generate random numbers to be used within our programs o Math library has a function called random o Pseudo-random Number generator o Math.random() Returns a number between 0.0 and 1.0 To create a two digit random number (0-99) • Math.random() * 100 To create a 1 digit random number (0-9) • Math.random() * 10 To create a number between 1-y • Math.random() * y +1 • Sample Program Roulette.java • String Data Type o Sequence of 0 or more characters o Enclosed in “ “ “cs 110” “I hate Programming” o Index Characters position within the string 1st character has index 0 “CS 110” • C – index 0 • S – index 1 • - index 2 • 1 – index 3 • 1 – index 4 • 0 – index 5 o Length # of characters in the string “CS 110” – length 6 “I hate Programming” – length 18 • String s = “the blue screen of death”; • int len; • len = s.length(); o len Î 24 o First character – index 0 o Last character – index of length-1CS 110 Notes – Basics II 3 Copyright C. Tanner 2010 o Operations + -- concatenate • String wholeName, last, first; • wholeName = first + last; charAt(index) – returns the character at the index • charAt(0) – returns the first character • charAt(i) – returns the ith character • s.charAt(0) references the first character • s.charAt(s.length() -1) references the last character substring (b,e) • returns the characters from the b index to e index • b – beginning index • e – ending index • leave off e then it takes to the end of the string • String str = “CS 110” • str.substring(0,1) Î CS • str.substring(1) Î S 110 indexOf(‘‘) • returns the position of the first ‘’ it finds in the string • String str = “CS 110” • int x = str.indexOf(‘ ‘); • x Î 2 toLowerCase() • converts each character to its lower case equivalent • String str = “I HATE PROGRAMMING” • str.toLowerCase() Î I hate programming • to change just a character o Character.toLowercase(‘A’)Î ‘a’ toUpperCase() • converts each character to its upper case equivalent replace(source,target) • replaces the source string with the target string • String str = “CS 110” • str.replace(“110”,”111”) Î CS 111 • Sample Programs: Piglatin.java, previous outside lab assignments • Homework (*not* to be turned in) page 171 #6 #rd Edition, Page 158 #6 4th edition • Formatting Double values on Printing o Usually displays many decimal digits o Can be formatted using String.format() command o double netPay; o String formattedNetPay = String.format(“%.2f”,netPay); o %.#f – the # specifies the number of decimal placesCS 110 Notes – Basics II 4 Copyright C. Tanner 2010 o %.0f – integer result o Page 152-154 (3rd Edition) Page 139-142 (4th edition) for more
View Full Document