לפניכם המחלקות First ו-Second. Second יורשת מ-First.
1public class First {
2 private static int count = 0;
3 protected int x;
4 protected int y;
5
6 public First (int num) {
7 this.x = num;
8 this.y = num;
9 count ++;
10 System.out.println ("First 1");
11 }
12 public First (int num1, int num2) {
13 this.x = num1;
14 this.y = num2;
15 count ++;
16 System.out.println ("First 2");
17 }
18 public static int getCount() {
19 return count;
20 }
21 public int getX() { return x; }
22 public int getY() { return y; }
23 public int sum() {
24 return this.x + this.y;
25 }
26 public void add(First other) {
27 this.x += other.x;
28 this.y += other.y;
29 System.out.println("x = "+ this.x + "y = "+ this.y);
30 }
31}
32
33public class Second extends First {
34 private int z;
35 public Second (int num) {
36 super (num);
37 this.z = num;
38 System.out.println ("Second");
39 }
40 public int sum() {
41 return super.sum() + this.z;
42 }
43 public void add (First other) {
44 this.x += other.getX();
45 this.y += other.getY();
46 if (other instanceof Second)
47 this.z += ((Second)other).z;
48 System.out.println("x = "+ this.x + " y = "+ this.y + " z = "+ this.z);
49 }
50}
51
52public class Tester {
53 public static void main(String[] args) {
54 First f1 = new First (40);
55 First f2 = new First (40, 50);
56 First f3 = new Second (100);
57 Second s1 = new Second (100);
58 Second s2 = new Second (100);
59 //***
60 }
61}נתונה המחלקה Tester:
java1public class Tester { 2 public static void main(String[] args) { 3 First f1 = new First (40); 4 First f2 = new First (40, 50); 5 First f3 = new Second (100); 6 Second s1 = new Second (100); 7 Second s2 = new Second (100); 8 //*** 9 } 10}
פרטו אילו עצמים נוצרים בפעולה main — לכל עצם ציינו מאיזו מחלקה הוא נוצר ומה ערכי תכונותיו — וכתבו את הפלט של הפעולה.
הציבו כל אחת מן הפקודות 1-10 שלהלן בפעולה main במקום //***.
כתבו את מספר הפקודה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין — כתבו את הפלט, ואם הוא אינו תקין — הסבירו מדוע.
הערה: אין קשר בין הפקודות. כלומר, יש להתייחס לכל פקודה כאילו היא היחידה בפעולה main.
System.out.println ("Total = " + First.getCount());System.out.println ("Total = " + Second.getCount());System.out.println ("sum = " + s1.sum());System.out.println ("sum = " + f3.sum());s1 = new First (100);f1.add (s2);s1.add (s2);s2.add (f3);((First)s1).add (f1);s1 = new Second (100, 100);לפניכם המחלקות First ו-Second. Second יורשת מ-First.
1public class First {
2 private static int count = 0;
3 protected int x;
4 protected int y;
5
6 public First (int num) {
7 this.x = num;
8 this.y = num;
9 count ++;
10 System.out.println ("First 1");
11 }
12 public First (int num1, int num2) {
13 this.x = num1;
14 this.y = num2;
15 count ++;
16 System.out.println ("First 2");
17 }
18 public static int getCount() {
19 return count;
20 }
21 public int getX() { return x; }
22 public int getY() { return y; }
23 public int sum() {
24 return this.x + this.y;
25 }
26 public void add(First other) {
27 this.x += other.x;
28 this.y += other.y;
29 System.out.println("x = "+ this.x + "y = "+ this.y);
30 }
31}
32
33public class Second extends First {
34 private int z;
35 public Second (int num) {
36 super (num);
37 this.z = num;
38 System.out.println ("Second");
39 }
40 public int sum() {
41 return super.sum() + this.z;
42 }
43 public void add (First other) {
44 this.x += other.getX();
45 this.y += other.getY();
46 if (other instanceof Second)
47 this.z += ((Second)other).z;
48 System.out.println("x = "+ this.x + " y = "+ this.y + " z = "+ this.z);
49 }
50}
51
52public class Tester {
53 public static void main(String[] args) {
54 First f1 = new First (40);
55 First f2 = new First (40, 50);
56 First f3 = new Second (100);
57 Second s1 = new Second (100);
58 Second s2 = new Second (100);
59 //***
60 }
61}נתונה המחלקה Tester:
java1public class Tester { 2 public static void main(String[] args) { 3 First f1 = new First (40); 4 First f2 = new First (40, 50); 5 First f3 = new Second (100); 6 Second s1 = new Second (100); 7 Second s2 = new Second (100); 8 //*** 9 } 10}
פרטו אילו עצמים נוצרים בפעולה main — לכל עצם ציינו מאיזו מחלקה הוא נוצר ומה ערכי תכונותיו — וכתבו את הפלט של הפעולה.
הציבו כל אחת מן הפקודות 1-10 שלהלן בפעולה main במקום //***.
כתבו את מספר הפקודה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין — כתבו את הפלט, ואם הוא אינו תקין — הסבירו מדוע.
הערה: אין קשר בין הפקודות. כלומר, יש להתייחס לכל פקודה כאילו היא היחידה בפעולה main.
System.out.println ("Total = " + First.getCount());System.out.println ("Total = " + Second.getCount());System.out.println ("sum = " + s1.sum());System.out.println ("sum = " + f3.sum());s1 = new First (100);f1.add (s2);s1.add (s2);s2.add (f3);((First)s1).add (f1);s1 = new Second (100, 100);