computer programming 2 lecture 9: object oriented programming array of objects prepared &...
DESCRIPTION
Out Lines Mahmoud Rafeek Alfarra Downloaded from Downloaded from 3 What is Array of object? Why is Array of objects? How to create an array of objects? ExampleTRANSCRIPT
Computer Programming 2
Lecture 9:Object Oriented Programming
Array Of Objects
Prepared & Presented by: Mahmoud Rafeek
Alfarra
MINISTRY OF EDUCATION & HIGHER EDUCATIONCOLLEGE OF SCIENCE AND TECHNOLOGY (CST)KHANYOUNIS- PALESTINE
و من يتِق الله ...عليك بتقوى الله ان كنت غافال يأتيك باألرزاق من حيث ال تدريفكيف تخاف الفقر والله رازقا
فقد رزق الطير والحوت في البحر ومن ظن أن الرزق يأتي بقوة ما أكل العصفور شيئا مع النسرتزول عن الدنيا فإنك ال تدري
إذا جن عليك الليل هل تعيش إلى الفجرفكم من صحيح مات من غير علة
وكم من سقيم عاش حينا من الدهروكم من فتى أمسى وأصبح ضاحكا
وأكفانه في الغيب تنسج وهو ال يدريفمن عاش ألفا وألفين
فال بد من يوم يسير إلى القبرMahmoud Rafeek Alfarra
2
Downloaded from http://staff.cst.ps/mfarraDownloaded from http://staff.cst.ps/mfarra
قال الشاعر عن التقـوى:
Out Lines Out Lines
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
3
What is Array of object? Why is Array of objects?How to create an array of objects?Example
What is Array of object?What is Array of object?
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
4
So far we have looked at an array of primitive types.
integers could also use doubles, floats, characters…
Often want to have an array of objects
Students, Books, Loans ……
Why is Array of objectsWhy is Array of objects??
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
5
Book
Class
Create objects
Obj1
Obj2
Obj3
Obj n
How to search, sort, Print all ….?
Book
Class
Create objects
Obj1
Obj2
Obj3
Obj n
.…
[0]
[1]
[2]
[n]
By index of arrayWe can make all the
manipulation operations
How to create an array of objectsHow to create an array of objects??
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
6
1. Declare the array•private Student studentList[]; //this declares studentList
2 .Create the array• studentList = new Student[10];
•this sets up 10 spaces in memory that can hold references to Student objects
3. Create Student objects and add them to the array: studentList[0] = new Student("Cathy", "Computing");
How to create an array of objectsHow to create an array of objects??
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
7
4.
Manag
e
2. Create objects of
Obj1Obj2
Obj3
Obj4
3. objects
1. Create objects of
Example: Array Of PCExample: Array Of PC
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
8
1. public class PC {2. private static int PCcounter;3. private static String code;4. private static String YOM;5. private static int cost;6. public PC() {7. PCcounter++;8. code = "no code";9. YOM = "1990"; }10. public PC(String code, String YOM, int cost) {11. PCcounter++;12. this.code =code;13. this.YOM = YOM;14. this.cost = cost; }15. public void info(){16. System.out.println("---------------------------");17. System.out.println("Code: "+code+"\nYOM: "+YOM+"\ncost: "+cost); }
}
Example: Array Of PCExample: Array Of PC
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
91. public class Record {2. PC [] rec = new PC[50];3. private int count;4. public Record() {5. count =0; }6. public void insert(String code, String YOM, int cost){7. if(count<50){8. PC p = new PC(code, YOM,cost);9. rec[count] = p;10. count++;}11. else System.out.println("Sorry, The record is complete"); }12. public void insert(){13. if(count<50){14. PC p = new PC();15. rec[count] = p;16. count++;}17. else System.out.println("Sorry, The record is complete"); }18. public void PrintAll(){19. System.out.println("Count of PC: "+count);20. for(int i =0; i<count; i++)21. rec[i].info(); } }
Example: Array Of PCExample: Array Of PC
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
101. public class RecordPC {2. 3. public static void main(String[] args) {4. 5. Record rec1 = new Record();6. rec1.insert("pc123","2010",450);7. rec1.insert("pc124","2010",455);8. rec1.insert("pc125","2010",460);9. rec1.PrintAll();10. }11. }
Next LectureNext Lecture… …
Mahmoud Rafeek AlfarraDownloaded from Downloaded from http://staff.cst.ps/mfarra
11
Java Applet