נתונות המחלקות האלה: A, B, D ו-Driver. בכל אחד מהסעיפים הבאים (*****) תוחלף לקטע קוד רלוונטי.
נתונות המחלקות האלה: A, B, D ו-Driver. בכל אחד מהסעיפים הבאים (*****) תוחלף לקטע קוד רלוונטי.
public abstract class A
{
protected int x;
public A() {
this.x = 1;
}
public abstract int f(int v);
public void f(A a) {
x = a.x;
}
public String toString(){
return "x =" + this.x;
}
} //end of class A
public class B extends A {
public B() {
super();
}
public B(int val) {
this.x = f(val);
}
public int f(int val) {
return this.x + val;
}
public void f(B b) {
this.x = this.x * b.x;
}
public String toString(){
return "B: " +
super.toString();
}
} //end of class B
public class D extends B {
public D(int val) {
this.x = val - this.x;
}
public void f(A a) {
this.x = this.x + a.x + 1;
}
public void f(B b) {
this.x = this.x * b.x;
}
public void f(D d) {
this.x = d.x - 1;
}
public String toString(){
return "D: " +
super.toString();
}
} //end of class D
public class Driver
{
public static void main
(String [] args){
A a;
B b;
int m;
// ************
}
}
סעיף א
מהו הפלט של קטע הקוד שלפניכם:
codeSnippet:
m = 11;
a = new B(m);
System.out.println(a);
a = new D(m);
System.out.println(a);
סעיף ב
codeSnippet:
b = new B(3);
m = b.f(4);
System.out.println(m);
m = b.f(m);
System.out.println(m);
סעיף ג
a = new B(5);
int m = a.f(a.f(2));
System.out.println(m);
סעיף ד
codeSnippet:
a = new D(10);
b = new B(20);
a.f(b);
System.out.println(a);
סעיף ה
אחרי ביצוע קטע הקוד הבא, הערך של התכונה x של העצם d יהיה 5. מהו הערך המשתנה של m?
codeSnippet:
D d = new D(m);
b = new B();
((A)d).f(b);
שאלות ותגובות על השאלה
🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות
שאלו כאן — ותקבלו מענה מוסמך.