object oriented programming (first)

29
Object oriented programming (first ) By Eng.abed albaset hamam

Upload: hvatrex-hamam

Post on 10-May-2015

147 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Object oriented programming (first)

Object oriented programming (first )

By Eng.abed albaset hamam

Page 2: Object oriented programming (first)

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• }

Page 3: Object oriented programming (first)

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

Page 4: Object oriented programming (first)

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("ab\t\"hello\"\\");The output abd "hello"\

Page 5: Object oriented programming (first)

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

Page 6: Object oriented programming (first)

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• }

Page 7: Object oriented programming (first)

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

Page 8: Object oriented programming (first)

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;• }• }

Page 9: Object oriented programming (first)

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

Page 10: Object oriented programming (first)

visiblity

• static int Fun(int x){• int z=1;• {• int x=4;// Naming collision error, x is already

declared• }• return 0;• }

Page 11: Object oriented programming (first)

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);• }• }

Page 12: Object oriented programming (first)

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]);• }• }• }

Page 13: Object oriented programming (first)

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");• }

Page 14: Object oriented programming (first)

Find the error

• public class MyClass {• public static void main(String[] args) {• int x=5;• {• int x=6;• }• System.out.println(x);• }• }

Page 15: Object oriented programming (first)

Find the error

• public class MyClass {• public static void main(String[] args) {• int x=fun();• }• public int fun(){• System.out.println("hello");• return 1;• }• }

Page 16: Object oriented programming (first)

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);• }• }

Page 17: Object oriented programming (first)

Find the error

• public class MyClass {• public static void main(String[] args) {• fun(3);• }• static void fun(int z){• int z=3;• }• }

Page 18: Object oriented programming (first)

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;• }• }

Page 19: Object oriented programming (first)

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);• }

• }

Page 20: Object oriented programming (first)

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);• }

• }

Page 21: Object oriented programming (first)

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();• }

• }

Page 22: Object oriented programming (first)

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();• }

• }

Page 23: Object oriented programming (first)

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); } }

Page 24: Object oriented programming (first)

Defined the object

• MyClass objref = new MyClass(value);

Page 25: Object oriented programming (first)

constructur

Page 26: Object oriented programming (first)

Example on objects

Page 27: Object oriented programming (first)

What the output

Page 28: Object oriented programming (first)

What the error

Page 29: Object oriented programming (first)

this