class BigBrother רשימה מועמדים לתחרות // candidates; private List<Candidate> publc void MakeVote(String name) הפעולה מקבלת שם, מוצאת את המתמודד ומוסיף לו הצבעה אחת // Node<Candidate> p = candidates.getfirst(); while (p!=null &&! p.getinfo().iscandidate(name)) if (p!= null) p.getinfo().addvote(); public String[] TwoLosers () פעולה המחזירה מערךהמכילה את שמות שני השחקנים בעלי ההצבעות הנמוך ביותר // הנחות : יש לפחות 2 שחקנים // Node<Candidate> p = candidates.getfirst(); Candidate c1 =p.getinfo(); Candidate c2; if (p.getnext().getinfo().getvotes()< c1.getvotes()) c2 = c1; c1 = p.getnext().getinfo(); else c2= p.getnext().getinfo(); p = p.getnext().getnext(); while (p!= null) if (p.getinfo().getvotes() < c2.getvotes()) c1 = c2; c2 = p.getinfo(); else if (p.getinfo().getvotes() < c1.getvotes()) c1 = p.getinfo() string [] two= c1.getname(), c2.getname(); return two; public static List <Integer> makelist (List<Integer> list) פעולה המקבלת רשימה ומחזירה רשימת מספרים ללא - 9 // List<Integer> result = new List<Integer>(); Node<Integer> p = list.getfirst(); Node<Integer> n = null; שאלה 2 שאלה 3
while (p!=null) int x = 0; while (p!= null && p.getinfo()!= -9) x = x*10 + p.getinfo(); n = result.insert (n,x); if (p!= null) return result; public static boolean IsZigZag(List<Integer> list) פעולה המקבלת רשימה לפי תבנית סעיף א ' ומחזירה אמת עם לאחר ביצוע פעולה מסעיף // א ' מתקבל רשימה לפי סדר זיגזג // List<Integer> temp = makelist (list); Node <Integer> p = temp.getfirst(); boolean up; if (p== null p.getnext() == null) return true; up = p.getinfo () > p.getnext().getinfo(); while (p.getnext()!= null) if (up == p.getinfo() > p.getnext().getinfo()) return false; up =!up; return true; public static void RemoveFromList(Queue<Integer> que, List<Integer> list) פעולה המקבלת רשימה ותור ומוציאה את כל המופעים של התור בתוך הרשימה // הנחות : גם הרשימה והתור מאותחלים // Node<Integer> p = list.getfirst(); int size = GetSize (que); while (p!= null) p = GetNextAppearance (p, que); if (p!= null) p=remove (list, p, size); שאלה 4
public static Node<Integer> GetNextAppearance(Node<Integer> p, Queue<Integer> que) פעולה המקבלת חוליה ותור ומחזירה את החוליה שהחל ממנו כל איברי התור מופיעים בו // הפעולה מתחילה לבדוק מהחוליה שהתקבל // אם התור אינו מופיע, הפעולה מחזירה // null while(p!= null) Queue <Integer> helper = Clone (que); while (p!=null && p.getinfo()!= helper.head()) p = p.getnext(); if (p == null) return null; Node<Integer> q = p; while (q!=null &&!helper.isempty() && q.getinfo()== helper.remove()) q = q.getnext(); if (helper.isempty()) return p; return null; public static int Count (Queue <Integer> que) פעולה המקבלת תור ומחזירה כמה איברים יש בתור // Queue<Integer> helper= Clone (que); int c=0; while (!helper.isempty()) c++; return c; public static Queue <Integer> Clone (Queue<Integer>que) Queue <Integer> q1 = new Queue<Integer>(); Queue<Integer> q2 = new Queue <Integer>(); while (!que.isempty()) q1.insert (que.head()); q2.insert (que.remove()); while (!q2.isempty()) que.insert (q2.remove()); return q1;
public static Node<Integer> Remove (List<Integer> list, Node<Integer> p, int count) פעולה המקבלת רשימה, חוליה ומספר ומוציאה מהרשימה החל מהחוליה מספר צמתים // הנחות : יש מספיק צמתים ברשימה // while (p!= null && count > 0) p=list.remove (p); count--; return p; class Room קומה בה נמצאת החדר // ; floor private int מס החדר // roomno; private int החדר תפוס // taken; private boolean [] האם יש נוף לים // hasseaview; private boolean public Room (int floor, int roomno, boolean hassea שאלה 5 מחלקת חדר סטנדרטי: א. מערך המייצג עבור כל יום בשנה האם פעולה המקבלת קומה מס ' חדר והאם יש נוף לים // public int HowManyBeds (). פעולה המחזירה כמה מקומות שינה יש בחדר // public boolean IsFree(int day) פעולה המקבלת יום בשנה ומחזירה האם החדר פנוי בתאריך של היום // public Suite :Room כמה חדרים יש בסוויטה // NoOfRooms; private int public Suite (int floor, int roomno, boolean hassea, int noofrooms) שיטה בונה המקבלת קומה, מס' חדרת האם יש נוף וכמה חדרים //.public int HowManyBeds( ) פעולה המחזירה כמה מיטות שי בחדר // class Hotel שם המלון hotelname;// private string שם העיר city;// private string אוסף החדרים במלון rooms;// private Room [] public Hotel (string name, string city, int howmany) this.hotelname = name; this.city = city; rooms = new Room[howMany]; ב. מלון
ג. Room Hotel Suite ד. שייך למחלקת Hotel public boolean AreThereFreeRooms (int noofpeople, int dayinyear,int noofdays) for (int i=0; i< rooms.length; i++) if (rooms[i].howmanybeds () >= noofpeople && rooms[i].isfree(dayinyear)) int j; for (j= 1; j <noofdays&& rooms[i].isfree (dayinyear+ j); j++); if (j==noofdays) return true; return false; שייך למחלקת מלון public int GetHowManyStandardRooms () int count = 0; for (int i=0; i < rooms.length; i++) if (rooms[i] instanceof Suite == false) count++; return count; שאלה 6
Foo f Goo g1 Goo g2 DoubleGoo g5 DoubleGoo g6 Hi Boy Bye Girl Hi GoodBye Hello GoodBye Goo g3 Goo g4 Gooey Hi Gooey Bye Gooey Hi Gooey Hello Goey GoodBye Goey Bye +++++++++++++++++++++++++++++++++++++++++++++ Gooey Boy Gooey Girl Gooey Hi Gooey Hello Goey GoodBye Goey Girl ב. יש שינוי באתחול שני חלקי ה- DoubleGoo הראשון יוצר אובייקט חדש ומעתיק אליו את הערך של ה-.Goo השני שומר הפניה ל- Goo קיים. ג. Foo Goo DoubleGoo
class Item implements Comparable class ShoppingList implements Comparable, Countable class Family implements Countable שאלה 7 א. ב. שורה 1 לא תגרום שגיאה. MyList הוא מסוג ShoppingList והוא מממש את הממשק Comparable שורה 2 תגרום שגיאה משום ש - item1 הוא מסוג Item ואינו יורש מ- Countable ולכן לא ניתן לבצע את ההשמה שורה 3 משום ש- count2 שבהשמה עושים המרה ל- item זה יעבור קומפילציה אבל count2 הוא מסוג Family והוא אינו תומך ב- Item יהיה שגיאת ריצה שורה 4 תקין. עושים ל- comp1 המרה כלפי מעלה ואח"כ עושים המרה כלפי מעלה ל countable שורה 5 תקין. ל- MyFamily יש פעולה Count משום שהוא מממש את Countable שורה 6 אינו תקין כי למשתמה comp1 שהוא מסוג Comparable אין לו פעולת coun() ג. class Family implements Countable private int parents; private int children; public Family (int p, int c) this.parents = p; this.children = c; public int count() return parents + children; public void updatenewresult(int result) finished = true; if (result < bestresult) bestresult = result; שאלה 8 א. ב. לא ניתן לשנות את מספר המשתתפים המקסימלי משום שהמאפיין מוגדר כ- final ג. public void AddRunner (Runner run) if (count < runners.length) runners[count ++] = run; public void RunnerFinishRace(int num, int result)
Runner r = FindRunner(num); if (r!= null) r.updatenewresult (result); if (winningresult == 0 result < winningresult) winningresult = result winningnumber = num; private MarathonRunner FindRunner (int num) for (int i=0; i < count; i++) if (runners[i].getnum()==num) return runners[i]; return null; public boolean IsOver () for (int i=0; i < count; i ++) if (!runners[i].isfinished()) return false; return true; public MarathonRunner GetWinner() return FindRunner (winningnumber); public static void Main (string[] arg) Marathon m = Marathon.GetInstance(); MarathonRunner run1 = new MarathonRunner ("Yossi",3,60); MarathonRunner run2 = new MarathonRunner ("Sara",6,50); m.addrunner (run1); m.addrunner (run2); m.runnerfinishrace (6,45); m.runnerfinishrace (3, 48); if (m.getresults() == m.getwinner().getbestresults()) System.out.println ("Improved"); else System.out.println ("Didn't Improve"); ד.
what (1234) k = what (124) = 412 num = 412 * 10 + 3= 4123 שאלה 1 k= what (14) = 41 num = 41 * 10 + 2 = 412 k= what (4) = 4 num = 4 * 10 + 1 = 41 return 4 התוצאה הסופית 4123 ב. הפעולה עושה הזזה של המספר ימינה כאשר הספרה האחרונה הולכת לראש המספר Sod ( 9231,1,1234, 999) y = 9231 S2 = Sod ( 1,1234, 999 = 1,999 IF (9231 > = 1923) S2 = 9231,1,999 S= 9231,1,1234, 999 ג. 9231,1,1234,999 y = 1 s2 = Sod ( 1234, 999) = 999 if (1 >= 1) S2= 1,999 S= 1,1234,999 y = 1234 s2 = Sod (999) = 999 if (1234 > = 4123) s= 1234, 999
y = 999 מחסנית ריקה ()// Sod s2 = if (999 >= 999) s2=999 s= 999 מחסנית ריקה return ד. הפעולה מקבלת מחסנית ומכניסה למחסנית חדשה את כל האיברים שה"סיבוב ב- 1 ימינה" קטן מהמספר במחסנית. בסוף הפעולה המחסנית המקורית נשארת שלמה.