נתונות שלוש מחלקות – Mammal (יונק), Antelope (אנטילופה) ו-Beaver (בונה):
נתונות שלוש מחלקות – Mammal (יונק), Antelope (אנטילופה) ו-Beaver (בונה):
public class Mammal {
protected int weight;
public Mammal (int w) {
weight = w;
}
public int getWeight () {
return weight;
}
public boolean isSame (Mammal other) {
System.out.println ("In Mammal");
return (this == other);
}
}
public class Antelope extends Mammal {
public Antelope (int w) { super (w); }
public boolean isSame(Antelope other) {
System.out.println ("In Antelope");
return ((other != null) && (this.weight == other.weight));
}
}
public class Beaver extends Mammal {
public Beaver (int w) { super (w); }
public boolean isSame (Mammal other) {
System.out.println ("In Beaver");
return ((other != null) && (other instanceof Beaver) && (this.weight == ((Beaver)other).weight));
}
}
נתונה המחלקה Program:
public class Program {
public static void main (String[] args) {
Antelope a1 = new Antelope (10);
Object a2 = new Antelope (10);
Beaver b1 = new Beaver (10);
Mammal b2 = new Beaver (10);
*******
}
}
המחלקה Program נמצאת בחבילה (Package) שונה מן המחלקה Mammal.
הציבו כל אחת מן השורות 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));
סעיף א
כתבו במחברת את מספר השורה וציינו אם הקוד תקין או לא תקין. אם הקוד תקין – כתבו את הפלט, ואם הוא לא תקין – הסבירו מדוע.
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.