נתונות המחלקות 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}הציבו כל אחת מן השורות 1-10 שלהלן בפעולה main, במקום שמסומן בכוכביות *******.
כִּתבו את מספר השורה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין — כתבו את הפלט, ואם הוא לא תקין — הסבירו מדוע.
System.out.println(a1.weight);System.out.println(((Beaver)a2).getWeight());System.out.println(a1.isSame(a2));System.out.println(a2.isSame(a1));System.out.println(b1.isSame(b2));System.out.println(b2.isSame(b1));System.out.println(a1.isSame((Beaver)b2));System.out.println(a1.isSame((Antelope)a2));System.out.println(b1.isSame((Antelope)a2));System.out.println(b1.isSame((Beaver)a2));נתונות המחלקות 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}הציבו כל אחת מן השורות 1-10 שלהלן בפעולה main, במקום שמסומן בכוכביות *******.
כִּתבו את מספר השורה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין — כתבו את הפלט, ואם הוא לא תקין — הסבירו מדוע.
System.out.println(a1.weight);System.out.println(((Beaver)a2).getWeight());System.out.println(a1.isSame(a2));System.out.println(a2.isSame(a1));System.out.println(b1.isSame(b2));System.out.println(b2.isSame(b1));System.out.println(a1.isSame((Beaver)b2));System.out.println(a1.isSame((Antelope)a2));System.out.println(b1.isSame((Antelope)a2));System.out.println(b1.isSame((Beaver)a2));