البرمجة الهدفية بلغة جافا - مصفوفة الكائنات

16
O O P Array of objects Object Oriented Programming Prepared & Presented by: Mahmoud Rafeek Alfarra 2012 Chapter 2

Upload: mahmoud-alfarra

Post on 21-Jul-2015

170 views

Category:

Education


11 download

TRANSCRIPT

Page 1: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OO

PArray of objects

Object Oriented Programming

Prepared & Presented by: Mahmoud Rafeek Alfarra

2012

Chapter 2

Page 2: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PP

http://mfarra.cst.ps

Contents

Class: User defined type1

Array of type (class)2

Example: Contact book 3

static Class Members4

Page 3: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPClass: User defined type

As the defined class is a new data type, we can defined any variables or arrays of

it.

http://mfarra.cst.ps

Page 4: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPArray of objects of class

http://mfarra.cst.ps

int sal;int id;String name;…

Employee classe1

e2

e3

e4

e5

e6

e7

e8

Employee [] x ;

In this case, each item in the array will be used by using its properties and methods.

Page 5: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPExample: Contact book

int serial;String name;String mobile;String address;…

Contact class

int count;void search(){}void print(){}void insert(){}…

ManagArray class

e1

e2

e3

e4

e5

e6

e7

e8

Contact [ ] conArray ;

From type of contact

Manages the book

http://mfarra.cst.ps

Page 6: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPExample: Contact book

http://mfarra.cst.ps

.

.

.

Management of contacts

Address Book

Page 7: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPContact class

http://mfarra.cst.ps

class Contact {private String name;private String mobile;private String address;public Contact (String name, String mobile, String address){

this.name = name;this.mobile = mobile;this.address = address;

} // Set methods of all properties public void setName(String name){

this.name = name;}

public void setMobile(String mobile){ … } public void setAddress(String Address){ … }

// Get methods of all propertiespublic String getMobile(){

return mobile;}

public String getAddress() { … } public String getName(){ … }

public void printData(){System.out.println("Name: "+name+"\n Mobile:" +mobile+" \n Address: "+address);} }

Page 8: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPManagArray class

http://mfarra.cst.ps

class ManagArray {private Contact [] contBook; // Declaration of array of contact classprivate int count;

public ManagArray(int size){count = 0;contBook = new Contact[size]; // initialize the size of array(# of contacts)

}public void insert(Contact c){

contBook[count] = c; // insert statementcount++;System.out.println("congratulation, a new address is inserted");

} public void printData(){

if (count ==0)System.out.println("Sorry, the contact book is empty");

else{

for(int i=0; i< count; i++){

System.out.println("================");contBook[i].printData(); // Print the data of object i

}System.out.println("Print Data is done!!");} }

// rest of methods}

Page 9: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPMain class

http://mfarra.cst.ps

public static void main(String[] args) { ManagArray book1 = new ManagArray(5);

// Create objects of contact

Contact c1 = new Contact("Ali", "0599","Khan"); Contact c2 = new Contact("Ola", "0598","gaza"); Contact c3 = new Contact("Eyad", "0597","rafah");

// insert object to the book

book1.insert(c1); book1.insert(c2); book1.insert(c3);

// Print all the contacts’ information

book1.printData(); }

Page 10: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPSummary: Contact book

http://mfarra.cst.ps

3- m

anag

e

objects o

f con

tact

2- create/ contains objects of contact

Obj1

Obj2

Obj3

Obj4

1- create object of

management class

Page 11: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPstatic class members

A static variable represents class wide information

all objects of the class share the same piece of

data.

The declaration of a static variable begins with the

keyword static.

http://mfarra.cst.ps

Use a static variable when all objects of a class must use the same copy of the variable.

Page 12: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPStatic class members

Static variables have class scope.

A class's public static members can be accessed

through a reference to any object of the class, or

they can be accessed by qualifying the member

name with the class name and a dot (.), as in

Math.random().

A class's private static class members can be

accessed only through methods of the class.

http://mfarra.cst.ps

Page 13: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPExample: static class members

http://mfarra.cst.ps

class Contact {private String name;private String mobile;private String address;private static int counter =0;

public Contact (String name, String mobile, String address){this.name = name;this.mobile = mobile;this.address = address;counter++;

}//….}

The value of this variable will be incremented by

each creation of new object.

When we declare the variable as static, all objects will read the same value.

Page 14: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPSummary: static class members

http://mfarra.cst.ps

static int x;static int x;

class Test

obj1obj1

X = 0X = 1

obj2obj2

X = 2

obj3obj3

X = 3

Now: Print Obj1.XPrint Obj1.X

3

Print Obj1.XPrint Obj1.X

3

Print Obj1.XPrint Obj1.X

3

Every time, all objects have the same value of static variable

Every time, all objects have the same value of static variable

Page 15: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PPاستغفروا ربكم

: ذكره تعالى ا قال

ن { إ ه ل ل ا روا ف غ ت س وافورا غ ن كا ه ل ل ا

حيما} ر

: ]106النساء [

http://mfarra.cst.ps

Page 16: البرمجة الهدفية  بلغة جافا - مصفوفة الكائنات

OOOO

PP

QUESTIONS?QUESTIONS?http://mfarra.cst.ps

Thank You …Thank You …