0% found this document useful (0 votes)
34 views22 pages

CsNOTES PDF

This document provides information about computer science concepts such as binary, hexadecimal, and decimal number systems. It also covers topics like data types in Java, the String and Scanner classes, and loops and conditionals. The key points covered are binary, hexadecimal, and decimal representations of numbers; primitive data types in Java like int and double; methods for manipulating String objects like length(), toUpperCase(), and indexOf(); and conditional and loop structures in Java like if/else statements and for loops.

Uploaded by

Tamaya Zaidan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views22 pages

CsNOTES PDF

This document provides information about computer science concepts such as binary, hexadecimal, and decimal number systems. It also covers topics like data types in Java, the String and Scanner classes, and loops and conditionals. The key points covered are binary, hexadecimal, and decimal representations of numbers; primitive data types in Java like int and double; methods for manipulating String objects like length(), toUpperCase(), and indexOf(); and conditional and loop structures in Java like if/else statements and for loops.

Uploaded by

Tamaya Zaidan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

COMPUTER SCIENCE NOTES

Binary - Hexadecimal - Decimal


A bit is the smallest unit in computing.
Represented by a 0 or 1.

A group of 8 bits is a byte.

Binary (BASE-2 numbering) stores data through the digits 0 or 1

Denary (decimal) is a numbering system with 10 digits. → 0 1 2 3 4 5 6 7 8 9


It is BASE-10.

Hexadecimal has 16 symbols. → 0 1 2 3 4 5 6 7 8 9 A B C D E F


It is BASE-16, often used to represent very big numbers.

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

1. Put 1 at equivalent or greatest less than.


1 at 16.
2. Subtract greatest less than from given decimal.
24-16 = 8
3. Put 1 at answer or greatest less than.
1 at 8.
4. Subtract.
8-8 = 0
5. Put 0 at empty bits.

Binary → Decimal
e.g 110101
128 64 32 16 8 4 2 1
0 0 1 1 0 1 0 1

Add the value of each bit.


32 + 16 + 4 + 1 = 53

Decimal / Binary → hexadecimal


e.g 229
128 64 32 16 8 4 2 1
1 1 1 0 0 1 0 1

1. If decimal, switch to binary first.


2. Fill register.
3. Count 4 from the right to left.
[0101] → 5
[1110] → E
229 = 11100101 = E5
BINARY DECIMAL HEXADECIMAL

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

Class → Attributes = characteristics


→ Methods = actions

Instantiating an object from a class


= creating an example
Invoking a method
= doing an action
Encapsulation
= hiding attributes of a class and only being able to change them through methods

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 (“////”):
////

Single line comments → // comment


Multiline comments → /* comment */
PART 2
JAVA VARIABLES
→ Primitive data types:
Integers - int : numbers
Floating point - double : decimals
Characters - char : letters
Boolean - boolean : true/false
→ Objects
String : words/sentences

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 :

Primitive data type


Declare int x ;
Initialize x=4;
Overwrite x=5;

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

final int x = 7 → x is constant, does not change.


● String + number = String
int x = 11
int y = 2
System.out.println(x + y + 1 + x + “c” + x + y + y) ;
addition vs. concatenation
16c1122

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

1. Syntax Error → code will not work if this error occurs.


missing ;
system.out.print
string

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

Declare and instantiate an object with new operator.

class object = new class ( class


name name name constructor )

e.g
String Dima = new String (“Computer Science”) ;

Invoke a method

object . method (...) ;


name name
dot parameters
operator

Methods :
String D = new String (“IB Computer”) ;

SOP(D.length( )) ; → number of characters - String


11

SOP(D.toUpperCase( )) ; → everything to upper case - String


IB COMPUTER

SOP(D.toLowerCase( )) ; → everything to lowercase - String


ib computer

INDEX → counters characters starting from 0 (including spaces)

SOP(D.charAt(4)) ; → insert index in parameters, get char at index 4 - char


o

SOP(D.indexOf(‘B”)) → insert char in parameters, get index of char - int


1

SOP(D.indexOf(y)) ; → if letter is not in string, will give -1


-1
SOP(D.charAt(12)) ; → if index does not reach 12, runtime error
Runtime Error

SOP(D.substring(3, 6)) ; → cuts string from 3 (included) until 6 (not included) - String
Com

SOP(D.substring(2)) ; → cuts string from 2 until the end - String


Computer

SOP(D.replace(‘u’, ‘y’)) ; → replaces all u with y - String


IB Compyter

Declare two strings :


String S = “IB Computer” ;
String F = “ib computer” ;
SOP(D.equals(S)) ; → determines whether two strings are equal
boolean t = D.equals(S2) → if strings are equal it will give true, if not it will give false
true
SOP(D.equals(F)) ;
boolean = D.equals(F) ;
false
SOP(D.equalsIgnoreCase(F)) ; → ignores the upper/lower cases
boolean = D.equalsIgnoreCase(F) ;
true

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

Declare and instantiate a class

Scanner T = new Scanner (System.in) ;

System.in → allows user to insert information

String x = T.nextLine( ) ; → inserting a word/sentence

int y = T.nextInt( ) ; → interesting an integer

double p = T.nextDouble( ) ; → inserting a double

To print the integer of a double → Casting


double x = 3
System.out.println((int)x);
PART 6
MATH CLASS
NO NEED TO IMPORT OR INSTANTIATE

Math → lang library

Math.random( ) ; → gives a random number [0,1[


Math.random( )*100 ; → [0,100[
Math.abs(#) ; → gives absolute value of the #
Math.sqrt(#) ; → gives the square root of that number
Math.sin(double) ; → gives the sine of the angle(double)
Math.cos(double) ; → gives the cosine of the angle(double)
Math.tan(double) ; → gives the tangent of the angle(double)
Math.max(double a, double b) ; → gives the greater number between a and b
Math.min(double a, double b) ; → gives the lesser number between a and b
Math.pow(double n, exponent) ; → gives n to the power of the exponent

TRIGONOMETRY WITH MATH CLASS


Angle in radians → degrees

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 →

Random r = new Random( );

Methods:
r.nextDouble( ) ; → [0, 1[
r.nextInt( ) ; → [-∞, +∞] limited by 32 bits
r.nextInt(#) ; → [0, # - 1]

DECIMAL FORMAT CLASS

Import java.text.DecimalFormat

Instantiate:
DecimalFormat fmt = new DecimalFormat (“0.##”) ;

(“000”) → digits before decimal point


leading zeros
(“0.##”) → digits after decimal point
no leading zero

ROUNDING to specified amount of decimal places.

Input type → double


Return type → String

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

To return format method as a double/int:


SOP(Double.parseDouble(fmt.format(f))+1) ;

AUTOBOXING ⇒ The automatic conversion of primitive data type into its


corresponding wrapper class

UNBOXING ⇒ The automatic conversion of wrapper type into its corresponding


primitive type.
PART 8
LOOPS AND CONDITIONALS

Conditionals: A conditional statement or selection statement lets us choose which statement


will be executed next

→ if statement
→ if-else statement

if (condition)
statement1 ;
else
statement2 ;

For multiple statements → use braces { }

The condition → boolean


If true → statement1
If false → statement2
All conditionals and loops are based on conditional expressions called Boolean
expressions

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 ;

initialization → initial value


condition → boolean
increment/decrement → the change in initialization after every loop
e.g a++ or a--
Step 1: Start at initialization
Step 2: Apply condition, if true, proceed to statement
Step 3: After statement, go to increment/decrement and apply to initialization
Step 4: Apply condition to new value and repeat
Step 5: If false or until false, exit loop

Embedded for statements

Step 1: Start at initialization


Step 2: Apply condition, if true, proceed to second if statement
Step 3: Start at initialization
Step 4: Apply condition, if true, proceed to statement
Step 5: After statement, apply increment/decrement
Step 6: Apply condition to new value and repeat
Step 7: When false, return to initial if statement
Step 8: Repeat from step 1
To make solving loops easier, use table :
(Examples from ex 1 in Group Work worksheet)

String Initialization int Condition Output


Word i i < Word.length( )/2

“Computer Science” 0 true C

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.

You might also like