1 / 11

Computer Programming 2

MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY (CST) KHANYOUNIS- PALESTINE. Computer Programming 2. Lecture 9: Object Oriented Programming Array Of Objects. Prepared & Presented by: Mahmoud Rafeek Alfarra. و من يتقِ الله. قال الشاعر عن التقـوى:.

emmly
Télécharger la présentation

Computer Programming 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY (CST) KHANYOUNIS- PALESTINE Computer Programming 2 Lecture 9: Object Oriented Programming Array Of Objects Prepared & Presented by: Mahmoud Rafeek Alfarra

  2. و من يتقِ الله ... قال الشاعر عن التقـوى: عليك بتقوى الله ان كنت غافلا يأتيك بالأرزاق من حيث لا تدريفكيف تخاف الفقر والله رازقا فقد رزق الطير والحوت في البحر ومن ظن أن الرزق يأتي بقوة ما أكل العصفور شيئا مع النسرتزول عن الدنيا فإنك لا تدريإذا جن عليك الليل هل تعيش إلى الفجرفكم من صحيح مات من غير علة وكم من سقيم عاش حينا من الدهروكم من فتى أمسى وأصبح ضاحكا وأكفانه في الغيب تنسج وهو لا يدريفمن عاش ألفا وألفين فلا بد من يوم يسير إلى القبر Downloaded from http://staff.cst.ps/mfarra

  3. Out Lines • What is Array of object? • Why is Array of objects? • How to create an array of objects? • Example Downloaded from http://staff.cst.ps/mfarra

  4. What is Array of object? • 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 …… Downloaded from http://staff.cst.ps/mfarra

  5. Why is Array of objects? Class Obj1 Obj3 Book Create objects Obj2 Obj n How to search, sort, Print all ….? Obj1 [0] Class Obj2 [1] By index of array We can make all the manipulation operations Book Create objects Obj3 [2] …. Obj n [n] Downloaded from http://staff.cst.ps/mfarra

  6. How to create an array of objects? 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"); Downloaded from http://staff.cst.ps/mfarra

  7. How to create an array of objects? 1. Create objects of 2. Create objects of 4. Manage 3. objects Obj1 Obj3 Obj2 Obj4 Downloaded from http://staff.cst.ps/mfarra

  8. Example: Array Of PC • public class PC { • private static int PCcounter; • private static String code; • private static String YOM; • private static int cost; • public PC() { • PCcounter++; • code = "no code"; • YOM = "1990"; } • public PC(String code, String YOM, int cost) { • PCcounter++; • this.code =code; • this.YOM = YOM; • this.cost = cost; } • public void info(){ • System.out.println("---------------------------"); • System.out.println("Code: "+code+"\nYOM: "+YOM+"\ncost: "+cost); } } Downloaded from http://staff.cst.ps/mfarra

  9. Example: Array Of PC • public class Record { • PC [] rec = new PC[50]; • private int count; • public Record() { • count =0; } • public void insert(String code, String YOM, int cost){ • if(count<50){ • PC p = new PC(code, YOM,cost); • rec[count] = p; • count++;} • else System.out.println("Sorry, The record is complete"); } • public void insert(){ • if(count<50){ • PC p = new PC(); • rec[count] = p; • count++;} • else System.out.println("Sorry, The record is complete"); } • public void PrintAll(){ • System.out.println("Count of PC: "+count); • for(int i =0; i<count; i++) • rec[i].info(); } } Downloaded from http://staff.cst.ps/mfarra

  10. Example: Array Of PC • public class RecordPC { • public static void main(String[] args) { • Record rec1 = new Record(); • rec1.insert("pc123","2010",450); • rec1.insert("pc124","2010",455); • rec1.insert("pc125","2010",460); • rec1.PrintAll(); • } • } Downloaded from http://staff.cst.ps/mfarra

  11. Next Lecture … Java Applet Downloaded from http://staff.cst.ps/mfarra

More Related