object oriented programming (first)

Post on 10-May-2015

147 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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 directives• Access-Modifiers class className{• // variables• // Method ( functions) definitions• }

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

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"\

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

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

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

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

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

visiblity

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

declared• }• return 0;• }

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

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

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

Find the error

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

Find the error

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

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

Find the error

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

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

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

• }

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

• }

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

• }

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

• }

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

Defined the object

• MyClass objref = new MyClass(value);

constructur

Example on objects

What the output

What the error

this

top related