אתם מתרגלים שאלה מתוך מה"ט מבני נתונים ותכנות מונחה עצמים — הנדסאי תוכנהמבחן 2025 · אביב מועד ב · שאלה 10כל שאלות המבחן ←
תכנות מונחה עצמיםירושה

בבית החולים, לכל רופא מתמחה הוצמד רופא ותיק מנוסה. כדי לנהל את תהליך ההתמחות פותחו המחלקות האלה: Doctor, Intern, Test.

המחלקה Doctor מתארת רופא:

public class Doctor {
    protected String name;// שם
    protected String specialization;// התמחות
    protected int numOfPatients; // מספר חולים
    public Doctor(String name, String spec) {
        this.name = name;
        this.specialization = spec;
        this.numOfPatients = 0;
    }
    public Doctor(String name, String spec, int num) {
        this.name = name;
        this.specialization = spec;
        this.numOfPatients = num;
    }
    public Doctor(Doctor other) {
        this.name = other.name;
        this.specialization = other.specialization;
        this.numOfPatients = other.numOfPatients;
    }
    public void addPatients(int num)
    {
        if(numOfPatients + num >=0)
            this.numOfPatients += num;
    }
    public String toString(){
        return "Doctor:" + name + ", " + specialization+ ","
            +numOfPatients;
    }
}

במחלקה הוגדרו גם פעולות set/Get לכל התכונות.

המחלקה Intern מתארת מתמחה:

public class Intern extends Doctor {
    private Doctor mentor;
    public Intern(String name, String spec, Doctor mentor){
        super(name, spec);
        this.mentor = mentor;
        this.numOfPatients = mentor.numOfPatients/2;
    }
    public Doctor getMentor(){return mentor;}
    public String toString() {
        return "Intern: " + name + ", " + specialization +
               ", Mentor: " + mentor.name +", " +
               mentor.numOfPatients +"," + numOfPatients;
    }
}

הפעולה הראשית main (במחלקה Test):

public static void main(String[] args) {
    Doctor[] d = new Doctor[9];
    d[0] = new Doctor("Dr. Cohen", "Cardiology",12);
    d[1] = new Doctor("Dr. Levy", "Neurology");
    d[2] = new Doctor("Dr. Sharon", "Pediatrics",8);
    d[3] = new Intern("Dani", "Cardiology", new Doctor(d[0]));
    d[4] = new Intern("Yael", "Surgery", d[0]);
    d[5] = new Intern("Avi", "Pediatrics", new Doctor(d[2]));
    d[6] = new Intern("Ruth", "Oncology", d[2]);
    d[7] = new Intern("Noam", "Cardiology", new Doctor(d[1]));
    d[8] = new Intern("Maya", "Neurology", new Doctor(d[0]));
    for (int i=0; i<d.length; i++) {
        System.out.println(d[i]);
    }
    d[0] = new Doctor("Dr. Goldman","Neurology",20);
    d[2].setName("Dr. Galper");
    d[2].addPatients(100);
    d[3].addPatients(200);
    d[5].addPatients(100);
    System.out.println("After change:");
    for (int i=0; i<d.length; i++) {
        System.out.println(d[i]);
    }
}
סעיף א

עקבו אחרי הביצוע של הפעולה main ורשמו מה יהיו ערכי התכונות של כל עצם שנוצר במהלך הביצוע.

סעיף ב

מהו הפלט של הפעולה main?

סעיף ג

כתבו פעולה חיצונית המקבלת מערך רופאים ומדפיסה את שמותיהם של כל המתמחים אשר ההתמחות שלהם שונה מההתמחות של המנחים שלהם.

כותרת הפעולה:

public static void printNotSameSpec(Doctor[] d)
שאלות ותגובות על השאלה

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

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

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