אתם מתרגלים שאלה מתוך מה"ט מבני נתונים ותכנות מונחה עצמים — הנדסאי תוכנהמבחן 2021 · קיץ מועד א · שאלה 6כל שאלות המבחן ←
תכנות מונחה עצמים›פולימורפיזם ומחלקות מופשטות

java

public class Apple { private int weight; public Apple (int w) { weight = w; } public int getWeight () { return weight; } public boolean equals(Apple other) { return ((other!=null) && (weight == other.weight)); } } public class Banana { private int weight; public Banana (int w) { weight = w; } public int getWeight () { return weight; } public boolean equals (Object other) { return ((other != null) && (other instanceof Banana) && (weight == ((Banana)other).weight)); } }

סעיף א

האם קיימת העמסה (Overloading) או דריסה (Overriding) של הפעולה equals במחלקות Apple ו-Banana? הסבירו את תשובתכם.

סעיף ב

נתונה המחלקה Program הבאה: public class Program { public static void main (String[] args) { System.out.println ("************"); Apple a1 = new Apple (10); Object a2 = new Apple (10); Banana b1 = new Banana (10); Object b2 = new Banana (10); ******* } } בהתייחס לכל אחת מהשורות הבאות, כתבו מה יקרה בעקבות הוספתה לשיטה main שלעיל (לאחר ההצהרות על האובייקטים, במקום שמסומן בכוכביות). הערה: אם הוספת פקודת קוד גורמת לשגיאה יש להסביר מהי השגיאה (שגיאת קומפילציה או שגיאת זמן ריצה).

  1. System.out.println (a1.weight);
  2. System.out.println (((Banana)a2).getWeight());
  3. System.out.println (a1.equals(a2));
  4. System.out.println (a2.equals(a1));
  5. System.out.println (b1.equals(b2));
  6. System.out.println (b2.equals(b1));
  7. System.out.println (a1.equals((Banana)b2));
  8. System.out.println (a1.equals((Apple)a2));
  9. System.out.println (b1.equals((Apple)a2));
  10. System.out.println (b1.equals((Banana)a2));
שאלות ותגובות על השאלה

🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות

שאלו כאן — ותקבלו מענה מוסמך.

🎓 מרצה לתכנות עונה כאן — תקבלו מענה מקצועי