Docsity

CS 110 Notes - Basics II: Operators, Math Library, String Data Type, Formatting Doubles - , Study notes of Computer Science

Toggle sidebar
bg1
CS 110 Notes โ€“ Basics II 1
Copyright C. Tanner 2009
โ€ข 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 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-- + c++ - d;
๎šƒ a = --b + ++c โ€“d;
๎šƒ 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)
pf3

Partial preview of the text

Download CS 110 Notes - Basics II: Operators, Math Library, String Data Type, Formatting Doubles - and more Study notes Computer Science in PDF only on Docsity!

  • 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 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-- + c++ - d; ยƒ a = --b + ++c โ€“d; ยƒ 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)

ยƒ 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
  • 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. ยƒ 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 +
  • 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 ยƒ 1 st^ 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 o First character โ€“ index 0 o Last character โ€“ index of length- o Operations ยƒ + -- concatenate ยƒ charAt(index) โ€“ returns the character at the index
  • charAt(0) โ€“ returns the first character