טוען...
טוען...
לפניכם המחלקות First ו-Second. Second יורשת מ-First.
public class First {
private static int count = 0;
protected int x;
protected int y;
public First (int num) {
this.x = num;
this.y = num;
count ++;
System.out.println ("First 1");
}
public First (int num1, int num2) {
this.x = num1;
this.y = num2;
count ++;
System.out.println ("First 2");
}
public static int getCount() {
return count;
}
public int getX() { return x; }
public int getY() { return y; }
public int sum() {
return this.x + this.y;
}
public void add(First other) {
this.x += other.x;
this.y += other.y;
System.out.println("x = "+ this.x + "y = "+ this.y);
}
}
public class Second extends First {
private int z;
public Second (int num) {
super (num);
this.z = num;
System.out.println ("Second");
}
public int sum() {
return super.sum() + this.z;
}
public void add (First other) {
this.x += other.getX();
this.y += other.getY();
if (other instanceof Second)
this.z += ((Second)other).z;
System.out.println("x = "+ this.x + " y = "+ this.y + " z = "+ this.z);
}
}
public class Tester {
public static void main(String[] args) {
First f1 = new First (40);
First f2 = new First (40, 50);
First f3 = new Second (100);
Second s1 = new Second (100);
Second s2 = new Second (100);
//***
}
}נתונה המחלקה 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.
public class First {
private static int count = 0;
protected int x;
protected int y;
public First (int num) {
this.x = num;
this.y = num;
count ++;
System.out.println ("First 1");
}
public First (int num1, int num2) {
this.x = num1;
this.y = num2;
count ++;
System.out.println ("First 2");
}
public static int getCount() {
return count;
}
public int getX() { return x; }
public int getY() { return y; }
public int sum() {
return this.x + this.y;
}
public void add(First other) {
this.x += other.x;
this.y += other.y;
System.out.println("x = "+ this.x + "y = "+ this.y);
}
}
public class Second extends First {
private int z;
public Second (int num) {
super (num);
this.z = num;
System.out.println ("Second");
}
public int sum() {
return super.sum() + this.z;
}
public void add (First other) {
this.x += other.getX();
this.y += other.getY();
if (other instanceof Second)
this.z += ((Second)other).z;
System.out.println("x = "+ this.x + " y = "+ this.y + " z = "+ this.z);
}
}
public class Tester {
public static void main(String[] args) {
First f1 = new First (40);
First f2 = new First (40, 50);
First f3 = new Second (100);
Second s1 = new Second (100);
Second s2 = new Second (100);
//***
}
}נתונה המחלקה 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);