Computer Programming 2 Lecture 9: Object Oriented Programming Array Of Objects Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY (CST) KHANYOUNIS- PALESTINE
و من يتقِ الله... عليك بتقوى الله ان كنت غافلا يأتيك بالأرزاق من حيث لا تدري فكيف تخاف الفقر والله رازقا فقد رزق الطير والحوت في البحر ومن ظن أن الرزق يأتي بقوة ما أكل العصفور شيئا مع النسر تزول عن الدنيا فإنك لا تدري إذا جن عليك الليل هل تعيش إلى الفجر فكم من صحيح مات من غير علة وكم من سقيم عاش حينا من الدهر وكم من فتى أمسى وأصبح ضاحكا وأكفانه في الغيب تنسج وهو لا يدري فمن عاش ألفا وألفين فلا بد من يوم يسير إلى القبر Mahmoud Rafeek Alfarra 2 Downloaded from قال الشاعر عن التقـوى:
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? Example
What is Array of object? Mahmoud Rafeek Alfarra Downloaded from Downloaded from 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 objects? Mahmoud Rafeek Alfarra Downloaded from Downloaded from 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 array We can make all the manipulation operations
How to create an array of objects? Mahmoud Rafeek Alfarra Downloaded from Downloaded from 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 objects? Mahmoud Rafeek Alfarra Downloaded from Downloaded from 7 Array of employees MainClass EmployeeClass 4. Manage 2. Create objects of Obj 1 Obj 2 Obj 3 Obj 4 3. objects 1. Create objects of
Example: Array Of PC Mahmoud Rafeek Alfarra Downloaded from Downloaded from 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 PC Mahmoud Rafeek Alfarra Downloaded from Downloaded from 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 PC Mahmoud Rafeek Alfarra Downloaded from Downloaded from public class RecordPC { public static void main(String[] args) { 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 Lecture … Mahmoud Rafeek Alfarra Downloaded from Downloaded from 11 Java Applet