Professional Documents
Culture Documents
CsNOTES PDF
CsNOTES PDF
1
One switch → one bit (2 ) → 0
→1
2
Two switch → two bit (2 ) → 0
→1
→0
→1
Decimal → Binary
e.g 24
128 64 32 16 8 4 2 1
0 0 0 1 1 0 0 0
Binary → Decimal
e.g 110101
128 64 32 16 8 4 2 1
0 0 1 1 0 1 0 1
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
1111 15 F
Object Oriented Programming
PART 1
e.g
Bank class → Attributes: - name
- account number
- balance/amount
→ Methods: - debit
- credit
- transfer
Obj. 1 Obj. 2 Obj. 3
Alex John Sam
8397320 2847328 4782373
$1000 $500 $2000
$800 $700 $2500
$2300
- Debit Alex $200
- Credit Sam $500
- Transfer $200 from Sam to John
- Add interest 10% to Sam’s account. → ERROR - does not exist in methods.
To print →
System.out.println(“Hello”) ;
println → new line
print → same line
Escape Sequence
“\n” → new line
“\t “→ tab
“\\” → \
“\”” → “
e.g
System.out. println(“\\\\”);
\\
System.out. println (“////”):
////
Declare, initialize
int x = 5 ;
^ opens a memory location of type integer for x
double y + 4.5 ;
char v = ‘c’ ;
boolean computer = true ;
String name = “Dima” ;
Concatenation :
String x = “World”
System.out.print(“Hello” + x) ;
Hello World
PART 3
Division
int/int → int
double/int → double
int/double → double
double/double → double
e.g
3/2 = 1
3.0/2 = 1.5
3/2.0 = 1.5
3.0/2.0 = 1.5
Modulo → %
e.g
5%4 = 1 3%8 = 3
1 0
4⟌5 8⟌3
4 0
=1 =3
Overwriting :
e.g
int x = 4 ;
4
x=x+1;x=4+1=5
45
x = x*2 ; x = 5*2 = 10
5 10
x = x-4 ; x = 10 - 4 = 6
10 6
System.out.println(“x=” +x) ;
x= 6
int x =
x+ = 1 = x+1 = x++
x* = 2
x- = 4
x/ = 2
int x = 2
int y = 3
System.out.print(“sum= “+x+y) ;
sum= 23
System.out.print(“sum= “+(x+y));
sum= 5
ERRORS IN JAVA
2. Runtime error
diving by 0 → 1/0
3. Logical error
e.g
F = 10 + m
m=2
F = 30
Answer is false and/or illogical.
PART 4
THE STRING CLASS - lang library
e.g
String Dima = new String (“Computer Science”) ;
Invoke a method
Methods :
String D = new String (“IB Computer”) ;
SOP(D.substring(3, 6)) ; → cuts string from 3 (included) until 6 (not included) - String
Com
SECOND OCCURRENCES:
Find the second occurrence of the letter a.
String S = “Tamaya” ;
int x = S.indexOf(‘a’) ;
String S = S.substring(x+1) ;
int y = S.indexOf(‘a’) ;
System.out.println(“The index of the second occurrence of the letter a is “ + (x+y+1)) ;
PART 5
THE SCANNER CLASS - util library
To use :
Import Scanner → import java.util.Scanner
double x =
e.g
To get random #s from specific intervals
1 to 6
(int)(Math.random( )*6+1) → [1,7[
● Casting to give an integer exclusively
● Parenthesis or else answer will always = 1
PART 7
RANDOM CLASS
import java.util.Random
Instantiate →
Methods:
r.nextDouble( ) ; → [0, 1[
r.nextInt( ) ; → [-∞, +∞] limited by 32 bits
r.nextInt(#) ; → [0, # - 1]
Import java.text.DecimalFormat
Instantiate:
DecimalFormat fmt = new DecimalFormat (“0.##”) ;
Method:
fmt.format(double) ; → rounds decimal place to instantiated parameter
WRAPPER CLASS
NO IMPORT OR INSTANTIATE
Methods:
Integer.parseInt(String) ; → String to integer
Integer.toString(integer) ; → Integer to string
Double.parseDouble(String) ; → String to double
Double.toString(double) ; → Double to string
→ if statement
→ if-else statement
if (condition)
statement1 ;
else
statement2 ;
Equality operators
== → equal to
!= → not equal to
Relational operators
> → greater than
< → less than
>= → greater than or equal to
<= → less than or equal to
Logical operators
! → not, not false is true, not true is false
&& → and, only true if BOTH are true
|| → or, true if one of the statements is true
int a = 5 ;
int b = 6 ;
int c = 7 ;
boolean k = ! (a==7) ; a==7 is false but ! means not false so k=true
boolean z = (a>=4) && (b<=8) ; a>=4 is true and b<=8 is true so z=true
boolean d = (c>5) || (a==4) ; c>5 is true, a==4 is false, so d=true
String comparison
.equals
.equalsIgnoreCase
LOOPS
Execute programs over and over again
Based on a boolean expression which determines how many times the statement is executed
→ while statement
→ for statement
while (condition)
statement ;
while → loop
condition → boolean
statement → executed as long as condition is true
Syntax of an if statement
if (initialization; condition; increment/decrement) ;
statement ;
1 true o
2 true m
3 true p
4 true u
5 true t
6 true e
7 true r
8 false EXIT
How to split digits? Print only positive digits.
Unboxing only in true or false.