לפניכם המחלקות First ו-Second שהמחלקה Second יורשת מהמחלקה First, והמחלקה Tester עם הפעולה main ובה חמש הצהרות עצמים ולאחריהן הערה // ***, המסמנת מיקום להוספת פקודה.
לפניכם המחלקות First ו-Second שהמחלקה Second יורשת מהמחלקה First, והמחלקה Tester עם הפעולה main ובה חמש הצהרות עצמים ולאחריהן הערה // ***, המסמנת מיקום להוספת פקודה.
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);
// ***
}
}
סעיף א
ציירו את העצמים שנוצרו בפעולה main, וכתבו את הפלט של הפעולה.
סעיף ב
הציבו כל אחת מן הפקודות 1-10 שלהלן בפעולה main במקום המצוין לעיל ב-***. כתבו את מספר הפקודה, ואם הקוד תקין - כתבו את הפלט, ואם אינו תקין - הסבירו מדוע. הערה: אין קשר בין הפקודות, כלומר יש להתייחס לכל פקודה כאילו היא היחידה שמוקמה בפעולה main.
- Total=First.getCount(). 2. Total=Second.getCount(). 3. sum=s1.sum(). 4. sum=f3.sum(). 5. s1=new First(100). 6. f1.add(s2). 7. s1.add(s2). 8. s2.add(f3). 9. ((First)s1).add(f1). 10. s1=new Second(100,100).
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.