Object oriented programming
(first )
By Eng.abed albaset hamam
Introduction
• general depiction of a java class using a
programming environment such as eclipse :
• //Package
• //import ...
Simple example
public class tom {
public static void main(String[] args) {
System.out.println("simple");
}}
• Why static ?...
Questions for the previous example
• What main mean ? keyword which means an
address where the application or program will...
What the output ??
public class Mainx {
public static void main(String[] args) {
int x=3;
int y=4;
System.out.println(x+y)...
functions
• public class MyClass{
• public static void main(String[] args) {
• funA(); // direct function call statement
•...
Q for previous ex
• Questions :
• 1- What is the function name?
• 2- What is the function header?
• 4- Why the function do...
Overloading
• public class Mainx {
• public static void main(String[] args) {
• System.out.print(Fun(2.00));
• }
• static ...
Visibility
• public class tom {
• public static void main(String[] args) {
• {
• char c='a';
• }
• System.out.println(c);/...
visiblity
• static int Fun(int x){
• int z=1;
• {
• int x=4;// Naming collision error, x is already
declared
• }
• return ...
Recursive functions
• Example 10: Finding the factorial of an integer by using recursive function
calls
• public class Mai...
Arrays
• One dimensional array
• public class Mainx {
• public static void main(String[] args) {
• int a[]={1,2,3};
• for(...
Arrays
• Two dimensional arrays
• int a[][]={{1,2}, {3,4}};
• for(int i=0;i<2;i++){
• for(int j=0;j<2;j++){
• System.out.p...
Find the error
• public class MyClass {
• public static void main(String[] args) {
• int x=5;
• {
• int x=6;
• }
• System....
Find the error
• public class MyClass {
• public static void main(String[] args) {
• int x=fun();
• }
• public int fun(){
...
Find the error
• public class MyClass {
• public static void main(String[] args) {
• int x=1;
• if(x>=1){
• int y=3;
• }
•...
Find the error
• public class MyClass {
• public static void main(String[] args) {
• fun(3);
• }
• static void fun(int z){...
What the output
• public class MyClass {
• public static void main(String[] args) {
• int a[]={1,2,3};
• System.out.printl...
What the output
• public class tom {
• public static int fun(int x){
• return ++x;
• }
• public static void main(String[] ...
Find the error
• public class tom {
• public static int fun(int x){
• return x;
• }
• public static void main(String[] arg...
Class variable
• public class tom {
• static int z=2;
• public static void fun2(){
• System.out.println(++z);
• }
• public...
What the output
• public class tom {
• static int z=8;
• public static void fun2(){
• System.out.println(++z);
• }
• publi...
What the output
• public class MyClass {
• static int z=5;
• public static void fun1(int x){
• int z=4,y=5;
System.out.pri...
Defined the object
• MyClass objref = new MyClass(value);
constructur
Example on objects
What the output
What the error
this
Upcoming SlideShare
Loading in …5
×

Object oriented programming (first)

276 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
276
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Object oriented programming (first)

  1. 1. Object oriented programming (first ) By Eng.abed albaset hamam
  2. 2. Introduction • general depiction of a java class using a programming environment such as eclipse : • //Package • //import directives • Access-Modifiers class className{ • // variables • // Method ( functions) definitions • }
  3. 3. Simple example public class tom { public static void main(String[] args) { System.out.println("simple"); }} • Why static ? static is keyword indicating the method is static which means it has static address which must determined at compile time
  4. 4. Questions for the previous example • What main mean ? keyword which means an address where the application or program will start executing What the output ?? System.out.println("abt"hello""); The output abd "hello"
  5. 5. What the output ?? public class Mainx { public static void main(String[] args) { int x=3; int y=4; System.out.println(x+y); } } Output 7
  6. 6. functions • public class MyClass{ • public static void main(String[] args) { • funA(); // direct function call statement • System.out.print("bye"); • } • static void funA(){ • //function body begin • System.out.print("No value to return"); • }//function body ends • }
  7. 7. Q for previous ex • Questions : • 1- What is the function name? • 2- What is the function header? • 4- Why the function does not have a return statement? • Answers : • 1- FunA • 2- Static void funA() • 4- Because the function return data type field is void, therefore it return
  8. 8. Overloading • public class Mainx { • public static void main(String[] args) { • System.out.print(Fun(2.00)); • } • static int fun(int x){ • return 1; • } • static int fun(double x){ • return 1; • } • static int fun(int x, int y){ • return 1; • } • }
  9. 9. Visibility • public class tom { • public static void main(String[] args) { • { • char c='a'; • } • System.out.println(c);//error c not visible • } • } • In can see the out but out cant see the in
  10. 10. visiblity • static int Fun(int x){ • int z=1; • { • int x=4;// Naming collision error, x is already declared • } • return 0; • }
  11. 11. Recursive functions • Example 10: Finding the factorial of an integer by using recursive function calls • public class Mainx { • public static void main(String[] args) { • int z=4; • System.out.println(factorial(z)); • } • static int factorial(int x){ • if(x==0) • return 1; • else • return x*factorial(x-1); • } • }
  12. 12. Arrays • One dimensional array • public class Mainx { • public static void main(String[] args) { • int a[]={1,2,3}; • for(int i=0;i<3;i++){ • System.out.print(a[i]); • } • } • }
  13. 13. Arrays • Two dimensional arrays • int a[][]={{1,2}, {3,4}}; • for(int i=0;i<2;i++){ • for(int j=0;j<2;j++){ • System.out.print(a[i][j]); • } • System.out.print("n"); • }
  14. 14. Find the error • public class MyClass { • public static void main(String[] args) { • int x=5; • { • int x=6; • } • System.out.println(x); • } • }
  15. 15. Find the error • public class MyClass { • public static void main(String[] args) { • int x=fun(); • } • public int fun(){ • System.out.println("hello"); • return 1; • } • }
  16. 16. Find the error • public class MyClass { • public static void main(String[] args) { • int x=1; • if(x>=1){ • int y=3; • } • System.out.println(x+y); • } • }
  17. 17. Find the error • public class MyClass { • public static void main(String[] args) { • fun(3); • } • static void fun(int z){ • int z=3; • } • }
  18. 18. What the output • public class MyClass { • public static void main(String[] args) { • int a[]={1,2,3}; • System.out.println(fun(a)); • } • public static int fun(int a[]){ • int sum=0; • for(int i=0;i<3;i++){ • sum+=a[i]; • } • return sum; • } • }
  19. 19. What the output • public class tom { • public static int fun(int x){ • return ++x; • } • public static void main(String[] args) { • int z=4; • z=fun(fun(fun(z))); • System.out.println(z); • } • }
  20. 20. Find the error • public class tom { • public static int fun(int x){ • return x; • } • public static void main(String[] args) { • double z=4; • z=fun(fun(fun(z))); • System.out.println(z); • } • }
  21. 21. Class variable • public class tom { • static int z=2; • public static void fun2(){ • System.out.println(++z); • } • public static void fun3(){ • System.out.println(++z); • } • public static void main(String[] args) { • fun3(); fun2(); • } • }
  22. 22. What the output • public class tom { • static int z=8; • public static void fun2(){ • System.out.println(++z); • } • public static void fun3(){ • System.out.println(++z); • } • public static void main(String[] args) { • fun3(); fun2();fun3(); fun2(); • } • }
  23. 23. What the output • public class MyClass { • static int z=5; • public static void fun1(int x){ • int z=4,y=5; System.out.println(x+y+z); • } • public static void fun2(int k){ • int x=1, y=2; • System.out.println(x+y+k+z); • } • public static void main(String[] args) { fun1(1);fun2(2); } }
  24. 24. Defined the object • MyClass objref = new MyClass(value);
  25. 25. constructur
  26. 26. Example on objects
  27. 27. What the output
  28. 28. What the error
  29. 29. this

×