02 Oop
02 Oop
Ziyati Houssaine
29. September 2020
Java
1
Overview
1. Recalling last session
2. Controll Statements
Ite
for
while
3. OOP in Java
General information
Methods
Return Value
Constructor
4. Conclusion
An Example
2
Recalling last session
Conclusion
Datatypes
• int, long
• float, double
• String
3
Controll Statements
Controll Statements
4
If Then Else
1 if ( condition ) {
2 // do something if condition is true
3 } else if ( another condition ) {
4 // do if " else if " condition is true
5 } else {
6 // otherwise do this
7 }
5
If Then Else example
6 if ( myNumber == 3) {
7 System . out . println ( " Strange number " ) ;
8 } else if ( myNumber == 2) {
9 System . out . println ( " Unreachable code " ) ;
10 } else {
11 System . out . println ( " Will be printed " ) ;
12 }
13 }
14
15 }
6
Conditions?
• == Equal
• != Not Equal
• > Greater Than
• >= Greater or Equal than
7
for
8
for example
10 }
9
while
1 while ( condition ) {
2 // do code while condition is true
3 }
10
while example
11 }
11
OOP in Java
Object Oriented Programming
12
Class Student
3 // Attributes
4 private String name ;
5 private int m a t r i c u l a t i o n N u m b e r ;
6
8 // Methods
9 public void setName ( String name ) {
10 this . name = name ;
11 }
12
13 public int g e t M a t r i c u l a t i o n N u m b e r () {
14 return m a t r i c u l a t i o n N u m b e r ;
15 }
16
17 }
13
Creation
The object derived from a class is also called instance. The variable is
called the reference.
14
Calling a Method
13 }
The class Student has two methods: void printTimetable() and void
printName().
15
Calling a Method
10 }
16
Calling a Method
15 }
17
Methods with Arguments
16 }
18
Methods with Return Value
19
Calling Methods with a return value
14 }
20
Constructors
6 public Calc () {
7 summand1 = 0;
8 summand2 = 0;
9 }
10
11 }
21
Constructors with Arguments
11 }
1 [...]
2 Calc myCalc = new Calc (7 , 9) ;
22
Conclusion
An Example
23
Class Student
24