Java
Java
BEGINNERS
Or this one
Click Ok
The output is in the console
Hello world was printed
COMMENTS WITH // AND /* */
The comment line,
The line after // did nothing in this program
Try to replace the line after // with any text you want
and then run the program and nothing will change, the
program will still print Hello World! Every time.
Int key word is used to declare variables of type int which can store only
complete numbers.
-----------------------------------------------------------------------------------------------------
Int x = 10; (initialize a variable of type int)
The syntax is :
Variable_type variable_name = value ;
-----------------------------------------------------------------------------------------------------
Int x; (declaring a variable of type int)
The synatax is :
Variable_type variable_name ;
So far we have seen that:
System.out.println() can work with a :
string
Int ( a number)
And print them to the screen.
We can change x’s value
Line 9 – we change x’s value from 10 to 20
X =10 * 20; (200) multiply 10 by 20
X = 20/10; (2) divide 20 by 10
X = 10 + 20 (30) add x to 20
X = 10 – 20 (-10) substract 10 from 20
Operator Operation
+ Addition
- Substraction
* Multiplying
/ Division
OPERATORS SYNTAX
Int x;
X = 10 + 20 ;
Variable = oprerand1 operator operator2;
On the right side of = we perform the operations on numbers.
On the left side of = we have the variable we want to assign the result of the
operations to.
We can perform operations between
variables of the same type(int)
X – holds the lowest number an int can hold
Y – holds the biggest number an int can hold
We can use the + operator between a string
an int inside of system.out.println() to print
to the console the value of x at the same
time we’re printing a string.
We can use the + operator between a string
an int inside of system.out.println() to print
to the console the a number at the same
time we’re printing a string.
FLOATING NUMBERS
Float
Double – has more precision than float.
X Y X || Y
False False False
False True True
True False True
True True True
IF ELSE
If the condition inside if ( condition) is true the if block is executed
otherwise the else block is executed;
This code is the same as in the previous slide.
THE SWITCH STATEMENT
The break statement will cause the compiler to execute line 18 , 19 then
jump to 22 and skip the rest of the code until that line
Without break in line 19
The code will executed lines 20 and 21 as well
This code does the same as the code in the previous slide
FOR LOOP
FOR LOOP
• In the first run , i is declared and assigned the value 1(only on the first
run will the compiler execute this part of the code) then then
condition i<= number is checked it’s true so the first run is executed
(line 10) then the compiler jumped to i++;
• Then the compiler jumps to check i<= number (it’s true) so the second
run starts and line 10 is executed and the the compiler jumps to i++
and then jumped to check if i <= number and so on and son on
• The program will stop when i > number in the condition check and will
jump to line 12
Break will cause the for loop to stop
It will terminate the for run and jump to line 15.
The continue keyword will case the run to stop on line 11 and
jump to line 9 to execute the next iteration or run
Lines 12 13 and 14 are skipped on the run which if fulfills the
condition in.
WHILE LOOP
• The while loop functions the same way as the for loop does
TASK
• Write a program that accepts from the user an unlimited amount of positive
numbers and stops when the user inters a negative number.
• The program prints to the console the average of the numbers.
NESTED LOOPS
• We have a SORTED array and we want to find if a certain value is in the array.
• If it is return the index of the first appearance of the value in the array, else
return -1.
• Let’s check the algorithm for implementing the method.
BINARY SEARCH
10 20 1 5 4 2 5 3 90 99
10 3 1 5 4 2 5 20 90 99
5 3 1 5 4 2 10 20 90 99
2 3 1 5 4 5 10 20 90 99
2 3 1 4 5 5 10 20 90 99
2 3 1 4 5 5 10 20 90 99
2 1 3 4 5 5 10 20 90 99
1 2 3 4 5 5 10 20 90 99
MAX SORT / SELECTION SORT
10 20 1 5 4 2 5 3 90 99
10 3 1 5 4 2 5 20 90 99
5 3 1 5 4 2 10 20 90 99
2 3 1 5 4 5 10 20 90 99
2 3 1 4 5 5 10 20 90 99
2 3 1 4 5 5 10 20 90 99
2 1 3 4 5 5 10 20 90 99
1 2 3 4 5 5 10 20 90 99
BUBBLE SORT
• So we have seen Max sort that sorts an array by finding the maximum element
in the array.
• There is another algorithm to sort an array. This algorithm is called bubble
sort.
BUBBLE SORT ILLUSTRATION
BUBBLE SORT – 1 ST RUN
7 8 2 3 5 6 1 9 0 4
7 8 2 3 5 6 1 9 0 4
7 2 8 3 5 6 1 9 0 4
7 2 3 8 5 6 1 9 0 4
7 2 3 5 8 6 1 9 0 4
7 2 3 5 6 8 1 9 0 4
7 2 3 5 6 1 8 9 0 4
7 2 3 5 6 1 8 9 0 4
7 2 3 5 6 1 8 0 9 4
7 2 3 5 6 1 8 0 4 9
9 iterations
BUBBLE SORT – 2 ND RUN
7 2 3 5 6 1 8 0 4 9
2 7 3 5 6 1 8 0 4 9
2 3 7 5 6 1 8 0 4 9
2 3 5 7 6 1 8 0 4 9
2 3 5 6 7 1 8 0 4 9
2
2 3
3 5
5 7
6 6
1 1
7 8
8 0
0 4
4 9
9
2 3 5 6 1 7 8 0 4 9
2 3 5 6 1 7 0 8 4 9
2 3 5 6 1 7 0 4 8 9
8 iterations
BUBBLE SORT – 3 RD RUN
2 3 5 6 1 7 0 4 8 9
2 3 5 6 1 7 0 4 8 9
2 3 5 6 1 7 0 4 8 9
2 3 5 6 1 7 0 4 8 9
2 3 5 1 6 7 0 4 8 9
2 3 5 1 6 7 0 4 8 9
2 3 5 1 6 0 7 4 8 9
2 3 5 1 6 0 4 7 8 9
7 iterations
BUBBLE SORT ALGORITHM
• Write a program that prints a triangle of numbers like in the previous example
slide except that it prints the number in reverse order.
• The program excepts a number as input and prints a triangle in reversed order
• Example: input is 3
• output is:
123
12
1
• Write a program that gets 2 numbers from the user x and p. the program
calculates the power of x, p times.
• For example : input 2 3
• The output is 8 (2*2*2).
TASK
• Write a recursion method that takes a string and prints it in the reversed
order.
TASK