100 likes | 246 Views
תרגול 12 מעקב אובייקטים. Course Syllabus : includes all the material. Our exams material :. http://www.cs.bgu.ac.il/~ipc131/wiki.files/Syllabus_IPJ_131.pdf. Previous exams ( Java language): http://www.cs.bgu.ac.il/~intro131/Previous_Exams Previous exams ( C language):
E N D
תרגול 12 מעקב אובייקטים
Course Syllabus : includes all the material. Our exams material : http://www.cs.bgu.ac.il/~ipc131/wiki.files/Syllabus_IPJ_131.pdf Previous exams (Java language): http://www.cs.bgu.ac.il/~intro131/Previous_Exams Previous exams (Clanguage): http://www.cs.bgu.ac.il/~clang092/Previous_Exams
1. Tracing publicclass X { privateintnum; privatecharc; public X() { this.num=2; this.c='A'; } public X(int n) { this.num=n; this.c='B'; } public X(int n, charch) { this.num=n; this.c=ch; } public X(X other) { this.num=other.num; c=other.c; } publicintgetN() { returnthis.num; } publicchargetCh() { returnthis.c; } publicvoid inc() { this.num++; this.c++; } public String toString() { String s = ""; for(int i = 0; i < this.num; i++) s = s + this.c; return s; } }
publicclass Y extends X { private X x; public Y() { super(); x = new X(); } public Y(int n) { super(n); x = new X(); } public Y(X other) { super(); x = new X(other); } public Y(X other, int n) { super(other); x = new X(n); } publicvoid inc() { this.x.inc(); } public X makeA() { returnnew X(one(this.x.getN(), this.getN()), one(this.x.getCh(), this.getCh())); } privateint one(int n, int m) { if(n > m) return n; return m; } privatechar one(char ch1, char ch2) { if(ch1 < ch2) return ch1; return ch2; } public String toString() { returnthis.x.toString(); } }
publicclass Test51 { publicstaticvoid main(String[ ] args) { X a1 = new X(3,'C'); X a2 = new X(4); Y b1 = new Y(a1); a1.inc(); a1.inc(); System.out.println(a1); System.out.println(a2); System.out.println(b1); } } What is the output?
Output EEEEE BBBB CCC
2. Tracing publicclass X { privatestaticintnumX = 0; privateintm1; privateintm2; public X(int m1, int m2) { this.m1 = m1; this.m2 = m2; this.numX++; System.out.println("X(" + m1 + "," + m2 + "),#" + numX); } }
publicclass Y extends X { privatestaticintnumY = 0; privatedoubledb; public Y(doubledb, int x) { super(x,x); this.db = db; numY++; System.out.println("Y(" + db + "," + x + "),#" + numY); } public Y(doubledb, int x, int y) { super(x,y); this.db = db; numY++; System.out.println("Y(" + db + "," + x + "),#" + numY); } }
publicclass Z { privatestaticintnumZ = 0; private Z aa; private X bb; public Z(Z aa, X bb) { this.aa = aa; this.bb = bb; numZ++; System.out.println("Z Constructor, #" + numZ); } } publicclass Test52 { publicstaticvoid main(String[] args) { X x1 = new X(2,3); X x2 = new Y(1.5, 6); X x3 = new Y(2.3, 8, 9); Z z4 = new Z(null, x1); Z z5 = new Z(z4, x3); } } What is the output?
Output X(2,3),#1 X(6,6),#2 Y(1.5,6),#1 X(8,9),#3 Y(2.3,8),#2 Z Constructor, #1 Z Constructor, #2