בי מאסטר (Bmaster)מאגר שאלות בגרותשאלות נפוצותמדריכיםארכיון בגרויות
תפריט

חומרי לימוד

מאגר שאלות בגרותשאלות נפוצותמדריכיםארכיון בגרויות
חזרה למאגר2022 קיץ מועד א

ניתוח פולימורפיזם במחלקות יונקים

שאלה קודמתשאלה הבאה
שאלה 15מבני נתונים2022 קיץ מועד א

ניתוח פולימורפיזם במחלקות יונקים

נתונות המחלקות Mammal, Antelope, Beaver, Program. המחלקה Program נמצאת בחבילה (Package) שונה מן המחלקה Mammal.

1public class Mammal {
2    protected int weight;
3    public Mammal(int w) {
4        weight = w;
5    }
6    public int getWeight() {
7        return weight;
8    }
9    public boolean isSame(Mammal other) {
10        System.out.println("In Mammal");
11        return (this == other);
12    }
13}
14public class Antelope extends Mammal {
15    public Antelope(int w) { super(w); }
16    public boolean isSame(Antelope other) {
17        System.out.println("In Antelope");
18        return ((other != null) && (this.weight == other.weight));
19    }
20}
21public class Beaver extends Mammal {
22    public Beaver(int w) { super(w); }
23    public boolean isSame(Mammal other) {
24        System.out.println("In Beaver");
25        return ((other != null) && (other instanceof Beaver) && (this.weight == ((Beaver)other).weight));
26    }
27}
28
29public class Program {
30    public static void main(String[] args) {
31        Antelope a1 = new Antelope(10);
32        Object a2 = new Antelope(10);
33        Beaver b1 = new Beaver(10);
34        Mammal b2 = new Beaver(10);
35        *******
36    }
37}

משימות

אמשימה אtext

הציבו כל אחת מן השורות 1-10 שלהלן בפעולה main, במקום שמסומן בכוכביות *******.

כִּתבו את מספר השורה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין — כתבו את הפלט, ואם הוא לא תקין — הסבירו מדוע.

  1. System.out.println(a1.weight);
  2. System.out.println(((Beaver)a2).getWeight());
  3. System.out.println(a1.isSame(a2));
  4. System.out.println(a2.isSame(a1));
  5. System.out.println(b1.isSame(b2));
  6. System.out.println(b2.isSame(b1));
  7. System.out.println(a1.isSame((Beaver)b2));
  8. System.out.println(a1.isSame((Antelope)a2));
  9. System.out.println(b1.isSame((Antelope)a2));
  10. System.out.println(b1.isSame((Beaver)a2));
שאלה 15מבני נתונים2022 קיץ מועד א

ניתוח פולימורפיזם במחלקות יונקים

נתונות המחלקות Mammal, Antelope, Beaver, Program. המחלקה Program נמצאת בחבילה (Package) שונה מן המחלקה Mammal.

1public class Mammal {
2    protected int weight;
3    public Mammal(int w) {
4        weight = w;
5    }
6    public int getWeight() {
7        return weight;
8    }
9    public boolean isSame(Mammal other) {
10        System.out.println("In Mammal");
11        return (this == other);
12    }
13}
14public class Antelope extends Mammal {
15    public Antelope(int w) { super(w); }
16    public boolean isSame(Antelope other) {
17        System.out.println("In Antelope");
18        return ((other != null) && (this.weight == other.weight));
19    }
20}
21public class Beaver extends Mammal {
22    public Beaver(int w) { super(w); }
23    public boolean isSame(Mammal other) {
24        System.out.println("In Beaver");
25        return ((other != null) && (other instanceof Beaver) && (this.weight == ((Beaver)other).weight));
26    }
27}
28
29public class Program {
30    public static void main(String[] args) {
31        Antelope a1 = new Antelope(10);
32        Object a2 = new Antelope(10);
33        Beaver b1 = new Beaver(10);
34        Mammal b2 = new Beaver(10);
35        *******
36    }
37}

משימות

אמשימה אtext

הציבו כל אחת מן השורות 1-10 שלהלן בפעולה main, במקום שמסומן בכוכביות *******.

כִּתבו את מספר השורה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין — כתבו את הפלט, ואם הוא לא תקין — הסבירו מדוע.

  1. System.out.println(a1.weight);
  2. System.out.println(((Beaver)a2).getWeight());
  3. System.out.println(a1.isSame(a2));
  4. System.out.println(a2.isSame(a1));
  5. System.out.println(b1.isSame(b2));
  6. System.out.println(b2.isSame(b1));
  7. System.out.println(a1.isSame((Beaver)b2));
  8. System.out.println(a1.isSame((Antelope)a2));
  9. System.out.println(b1.isSame((Antelope)a2));
  10. System.out.println(b1.isSame((Beaver)a2));