Java Programming - 240327 - 081727
Java Programming - 240327 - 081727
• Object-oriented
• Compiled and Interpreted
• Secure
• Multi-threaded
• Garbage collected
• No support for multiple inheritance
How to use Java?
4
1-Download Java
Download Java for Windows
2-Download an IDE
Eclipse Downloads | The Eclipse Foundation
Apache NetBeans Releases
Download IntelliJ IDEA: The Capable & Ergonomic Java IDE by JetBrains
Or any other IDE
Java Hierarchy
5
• Class
Package
• Class
• Class
Main Method
6
• System is a class
• Out is a stream
• Println is a method that prints a line of text to the screen
Comments
10
Single-line Multi-line
comments comments
Exercise
12
• Byte
• 8-bit
• Example: byte a = 10
Data types
17
• Short
• 16-bit
• Example: short s = 10000
Data types
18
• int
• 32-bit
• Example: int a = 100000
Data types
19
• long
• 64-bit
• Example: long a = 100000;
Data types
20
• float
• 32-bit
• Example: float f1 = 234.5f;
Data types
21
• double
• 64-bit
• Example: double d1 = 123.4;
Data types
22
• boolean
• char
• String
• Final
Primitive Non-Primitive(Reference)
• Limited • Not Limited
• Byte • String
• Short • Arrays
• Int • Objects
• Long
• Float
• Double
• Char
• Boolean
Exercise
27
• Example 1: • Example 2:
int x = 34; int x = 34;
int y = ++x; int y = x++;
Assignment Operators 34
int num1 = 2 ;
num2 + = num1 ;
35
Conditionals
If Statement
36
Comparison Operators
Less than <
Greater than >
Not equal to !=
Equal to ==
Less than or equal to <=
Greater than or equal to >=
If Statement
37
If - Else Statement
If – Else If Statements
Nested if Statements
40
Combining more than one condition
Switch Statement
control statement allows us to make a
Switch statement decision from the number of choices
43
If don’t match with any case .. Default case will run
Example 44
Loops
46
Important Note
Do .. While Loop
• Do at least one iteration even condition is false
Nested Loops
52
If a loop exists inside the body of another loop, it's called a nested loop
Example
53
Use the nested loop to iterate through each day of a week for 3 weeks
Break & Continue
54
1-Break Statement
The break statement is typically used to exit early from a loop ( for , while , do while)
55
2-Continue Statement
The continue statement skips the remaining statements and go to the next iteration
Example
56
-While Loop
-For Loop
-Do…While loop
Examples
57
2-Write a program that prints the sum of the even and odd integers.
3-Write a do-while loop that asks the user to enter two numbers. The numbers should be
added and the sum displayed. The loop should ask the user if he wishes to perform the
operation again. If so, the loop should repeat; otherwise it should terminate
58
Functions(Methods)
Function
59
Tips
1- Function should be for only one specific task
Note:
Parameter Arguments
• In function declaration • In function call
abs method
73
(random()*((max-min)+1))+min;
int r = (int) (random()*((max-min)+1))+min;
max method
80
max(3,5)
max(2,(max(3,5))
Method Overloading
81
Scope of a variable
Local variable : declared within a function (or block)
Example
Calculate and print out The sum and the Average of 3 student marks.
calculateSum()
Hint:
calculateAverage()
print ()
87
Arrays
Arrays
88
Syntax
data_type [ ] arrayName = new data_type [ Size ]
• Data_type : the data type of the array, which is the same type of all elements in the array.
• arrayName : is the reference variable.
• new : operator for initiation array
• Size : the number of elements in the array (must be int).
Example 89
arrayName[index]
list[5] = 20;
93
Array Initialization
• Arrays can be initialized during declaration : In this case, it is not necessary to specify the size of
the array.
• Example 1: int items [ ] = { 1 , 2 ,3,4,5 }
• Example 2 : int items [ ] = { 5 , 7 , 10 }
**Also you can use the For .. Loop to initialize it with values submitted after declaring the array**
Use Loop
96
Example
Write a program that ask the user to enter 10 Employee salaries and store them.
Write a program that ask the user to enter 10 Employee salaries and store them
then add a bonus of 10 for each employee and print out salary after adding
bonus
97
Example
Std1
Std2
Std3
Std4
Std5
Two dimensional Array declaration 100
10
Two dimensional Array Initialization 102
• Arrays can be initialized during declaration : In this case, it is not necessary to specify the size of
the array.
• float marks [4 ][3 ] ={{ 20,30,35},
{40,45,65},
{60,65,75},
{80,65,45}};
**Also you can use the two nested For .. Loop to initialize it with values submitted after declaring the
array**
Example
Write a program that build a matrix of 5 rows and 3 columns As the use to enter
the values for all the matrix items print out the sum of all matrix items and print
out the sum of the diagonal items
104
Handling Exceptions
106
107
Handling Exceptions
• An exception is an object that is generated as the result of an error or an unexpected
event.
int x = 5 , y = 0 ;
System.out.println (x/y);
Error
Handling Exceptions
• To handle an exception, you use a try statement.
try
{
(try block statements...)
}
catch (ExceptionType ParameterName)
{
(catch block statements...)
}
Exception Handlers
• There can be many polymorphic catch clauses.
110
try
{
(try block statements...)
}
catch (ExceptionType ParameterName)
{
(catch block statements...)
}
finally
{
(finally block statements...)
}