ملفات مساق البرمجة الهدفية (الشيئية) التي يتم تدريسها لطلبة بكالوريوس تكنولوجيا المعلومات وبكالوريوس تطوير نظم الحاسوب في الكلية الجامعية للعلوم والتكنولوجيا.
الملف يضم مفهوم مصفوفة الكائنات Array of Objects
إعدادي وتدريسي
3. OO
OO
PP
Class: 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
4. OO
OO
PP
Array of objects of class
http://mfarra.cst.ps
int sal;
int id;
String name;
…
Employee class
e1
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.
5. OO
OO
PP
Example: 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
7. OO
OO
PP
Contact 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 properties
public String getMobile(){
return mobile;
}
public String getAddress() { … }
public String getName(){ … }
public void printData(){
System.out.println("Name: "+name+"n Mobile:" +mobile+" n Address: "+address);
} }
8. OO
OO
PP
ManagArray class
http://mfarra.cst.ps
class ManagArray {
private Contact [] contBook; // Declaration of array of contact class
private 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 statement
count++;
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
}
9. OO
OO
PP
Main 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();
}
11. OO
OO
PP
static 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.
12. OO
OO
PP
Static 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
13. OO
OO
PP
Example: 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.
14. OO
OO
PP
Summary: 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