Presentation is loading. Please wait.

Presentation is loading. Please wait.

תירגול 9 אובייקטים.

Similar presentations


Presentation on theme: "תירגול 9 אובייקטים."— Presentation transcript:

1 תירגול 9 אובייקטים

2 היום בתרגול: המשך נושא מחלקות (classes) ואובייקטים (מופעים, objects).
אובייקטים המכילים אובייקטים נוספים כשדות. מערכים של אובייקטים.

3 משחק מזל עם קוביה של 100 פאות
ניתן להתייחס להגרלה של מספר שלם בתחום מ-1 עד n כאל זריקת קוביה בעלת n פאות. המשחק מתנהל בסיבובים. בכל סיבוב המחשב מגריל מספר בין 1 ל 3. אם המספר שהוגרל הוא 1, השחקן הראשון מאבד כמות נקודות אקראית בתחום 1 עד 100. אם המספר שהוגרל הוא 2, השחקן השני מאבד כמות נקודות אקראית בתחום 1 עד 100. אחרת כל אחד משני השחקנים מרוויח כמות נקודות אקראית בתחום 1 עד 100. (כל אחד יכול להרוויח כמות שונה). המנצח הוא השחקן שצבר יותר נקודות בתום 10 סיבובים. האובייקטים המרכיבים את התוכנית הם שחקן, קוביה ומשחק. על סמך תיאור זה, מהן המחלקות? מהם השדות ומהן השיטות של כל מחלקה? מה מידת החשיפה שנרצה שתהיה לכל משתנה

4 int addPoints(int points)
FIELDS Constructor String toString() int addPoints(int points) getters / setters

5

6 Constructor מהם הפרמטרים המועברים אליו?
FIELDS Constructor מהם הפרמטרים המועברים אליו?

7

8

9

10

11 Student Class Define class Student containing student’s name and ID, read-only accessors and a String conversion method. Should the relevant fields be final? When do we need to qualify field access with this. ? What does each Student object contain exactly? Does it contain methods in addition to fields? How do methods know which object do they work with? What is the difference between this and any other object of type Student?

12 Student Class

13 Student Class

14 Course Class Define class Course that’s similar to student, maintaining course name and number. What if we forget to set some fields in constructor, but those fields are final?

15 Course Class

16 Course Class Design a way for each Course to keep track of registered students. Signatures: Student getStudent(long id) void addStudent(Student s) void print() We will use an array that grows with each add student. Need to reject adding students with ID already in the list.

17 Course Class

18 Course Class

19 Course Class

20 Questions Why did we print students in method print, and did not instead modify toString? Would this still work if students also kept list of courses they registered to, and also had a modified toString? How would we implement removal of students from a course? What if we wanted to keep students in sorted order in a list? How would we sort them, what would be the comparisons during sorting process?

21 Modify each Student similarly to Course, to keep the list of courses.

22 Course Class

23 Course Class


Download ppt "תירגול 9 אובייקטים."

Similar presentations


Ads by Google