אתם מתרגלים שאלה מתוך בגרות מדעי המחשב — מבני נתונים (שאלון 899271)מבחן 2025 · קיץ מועד א · שאלה 10כל שאלות המבחן ←
תכנות מונחה עצמיםירושה

מערכת גן חיות מנהלת חמישה סוגים: Animal (המחלקה הבסיסית - color, weight, בנאי ו-toString נתונים ואסור לשנותם), ו-Bird, Parrot, Fish, Snake הבנויות במקור כשטוחות: כל אחת חוזרת על color/weight משלה, ול-Bird ול-Parrot יש כל אחת עותק זהה של zoo() שמדפיסה "Hello". יש לתכנן ירושה נכונה בין המחלקות, להוסיף להן בנאים, לדרוס toString כך שהפלט יתאים לתיבה הנתונה, ולבדוק/לתקן פעולה שמפעילה zoo() על מערך מוצהר מטיפוס Animal.

public class Animal {
    private string color;
    private int weight;
    public Animal (string color, int weight)
    {
        this.color = color;
        this.weight = weight;
    }
    public override string ToString() {
        return "My color is " + this.color + "! And I weigh " + weight + " kilos!";
    }
}
public class Bird {
    private string color;
    private int weight;
    private bool isFlying;
    public void Zoo () {
        Console.WriteLine ("Hello");
    }
}
public class Parrot {
    private string color;
    private int weight;
    private bool isFlying;
    private bool isTalking;
    public void Zoo () {
        Console.WriteLine ("Hello");
    }
}
public class Fish {
    private string color;
    private int weight;
    private string waterType;      // דג ים/דג מים מתוקים
}
public class Snake {
    private string color;
    private int weight;
    private int length;
    private bool isVenomous;     // ?האם הנחש ארסי
}

// --- המחלקה Tester (סעיף ב) ---
public class Tester {
    public static void Main (string[] args) {
        Animal[] animals = new Animal [5];
        animals [0] = new Bird ("white", 4, false);
        animals [1] = new Fish ("blue", 3, "sweet water");
        animals [2] = new Parrot ("brown", 12, true, true);
        animals [3] = new Snake ("gray", 2, 6, true);
        animals [4] = new Snake ("black", 3, 4, false);
        //***
    }
}

עמוד 15 (Java) ועמוד 17 (C#), סעיף ג(1): תיבת הפלט

the printed output box (identical in the Java version, p.15, and the C# version, p.17):
I'm a bird! My color is white! And I weigh 4 kilos!
My color is blue! And I weigh 3 kilos!
I'm a parrot! My color is brown!
I'm a snake! I'm venomous, be careful! My color is gray! And I weigh 2 kilos!
I'm a snake! I'm not venomous! My color is black! And I weigh 3 kilos!
סעיף א

כתבו מחדש את הכותרות, התכונות והפעולות של המחלקות: Bird , Parrot , Fish ו־Snake , לפי עקרונות תכנות מונחה עצמים (אין לשנות את המחלקה Animal). בסעיף זה אין צורך לכתוב פעולות בונות. הניחו שהפעולות get ו־set קיימות. נוסף על כך, סרטטו תרשים היררכייה בין כל המחלקות. יש לסמן ירושה באמצעות החץ ◁——— .

public class Bird : Animal { ... } public class Parrot : Bird { ... } public class Fish : Animal { ... } public class Snake : Animal { ... }

סעיף ב

נתונה המחלקה Tester : [הקוד ב-code_context] הוסיפו פעולות בונות למחלקות Bird , Parrot , Fish ו־Snake , המאתחלות את תכונות המחלקות (כך שהפעולה main תרוץ ללא שגיאות). הערה: אין לשנות את הפעולה הבונה הנתונה של Animal .

סעיף ג(1)

בפעולה main נוסף קטע הקוד שלפניכם (במקום המסומן ב־***): for (int i = 0; i < animals.length; i++) { System.out.println (animals[i]); } קטע הקוד תקין ומדפיס את הפלט שלפניכם: [תיבת הפלט ב-figures] הוסיפו את הפעולה toString במחלקות (רק היכן שיש צורך) לפי עקרונות תכנות מונחה עצמים, כך שהפלט יהיה מתאים. הערה: אין לשנות את הפעולה toString במחלקה Animal . תשובה שבה יהיה שינוי לא תזוכה בנקודות.

for (int i = 0; i &lt; animals.Length; i++) { Console.WriteLine (animals[i]); }

סעיף ג(2)

במחלקה Tester נוספה הפעולה שלפניכם: public static void hello (Animal [] arr) { for (int i = 0; i < arr.length; i++) { arr[i].zoo(); } } אם הפעולה תקינה, כתבו את הפלט שלה עבור המערך animals (הנתון לעיל), ואם הפעולה לא תקינה – תקנו אותה (כך שהקריאה ל־zoo בתוך הלולאה תהיה תקינה) וכתבו את הפלט עבור המערך animals . הערות: – אין לשנות את המחלקות עצמן. תשובה שבה יהיה שינוי במחלקות לא תזוכה בנקודות. – הפעולה צריכה לרוץ ללא שגיאות עבור כל מערך מטיפוס Animal .

public static void Hello (Animal [] arr) { for (int i = 0; i &lt; arr.Length; i++) { arr[i].Zoo(); } }

שאלות ותגובות על השאלה

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

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

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