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

A set of notes from cs 110 basics ii class covering various topics including assignment operators, increment/decrement operators, math library, string data type, and formatting double values. The concept of assignment operators, compound assignment operators, increment/decrement operators, and their usage. It also covers the math library, its methods for elementary math functions, and how to use them. The string data type, its operations such as concatenation, indexing, length, and substring, and formatting double values using the string.format() command.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-07e
koofers-user-07e 🇺🇸

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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