1 / 10

תרגול 12 מעקב אובייקטים

תרגול 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):

Télécharger la présentation

תרגול 12 מעקב אובייקטים

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. תרגול 12 מעקב אובייקטים

  2. 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

  3. 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; } }

  4. 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(); } }

  5. 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?

  6. Output EEEEE BBBB CCC

  7. 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); } }

  8. 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); } }

  9. 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?

  10. 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

More Related