Presentation is loading. Please wait.

Presentation is loading. Please wait.

תרגול 12 מעקב אובייקטים 1. Our exams material : Course Syllabus : includes all the material.

Similar presentations


Presentation on theme: "תרגול 12 מעקב אובייקטים 1. Our exams material : Course Syllabus : includes all the material."— Presentation transcript:

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

2 Our exams material : http://www.cs.bgu.ac.il/~ipc131/wiki.files/Syllabus_IPJ_131.pdf Course Syllabus : includes all the material. Previous exams (Java language): http://www.cs.bgu.ac.il/~intro131/Previous_Exams Previous exams (C language): http://www.cs.bgu.ac.il/~clang092/Previous_Exams

3 1. Tracing public class X { private int num; private char c; public X() { this.num=2; this.c='A'; } public X(int n) { this.num=n; this.c='B'; } public X(int n, char ch) { this.num=n; this.c=ch; } public X(X other) { this.num=other.num; c=other.c; } public int getN() { return this.num; } public char getCh() { return this.c; } public void 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 public class 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); } public void inc() { this.x.inc(); } public X makeA() { return new X(one(this.x.getN(), this.getN()), one(this.x.getCh(), this.getCh())); } private int one(int n, int m) { if(n > m) return n; return m; } private char one(char ch1, char ch2) { if(ch1 < ch2) return ch1; return ch2; } public String toString() { return this.x.toString(); } }

5 public class Test51 { public static void main(String[ ] args) { X a1 = new X(3,'C'); X a2 = new X(4); Y b1 = new Y(a1); 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 public class X { private static int numX = 0; private int m1; private int m2; public X(int m1, int m2) { this.m1 = m1; this.m2 = m2; this.numX++; System.out.println("X(" + m1 + "," + m2 + "),#" + numX); }

8 public class Y extends X { private static int numY = 0; private double db; public Y(double db, int x) { super(x,x); this.db = db; numY++; System.out.println("Y(" + db + "," + x + "),#" + numY); } public Y(double db, int x, int y) { super(x,y); this.db = db; numY++; System.out.println("Y(" + db + "," + x + "),#" + numY); }

9 public class Z { private static int numZ = 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); } public class Test52 { public static void 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


Download ppt "תרגול 12 מעקב אובייקטים 1. Our exams material : Course Syllabus : includes all the material."

Similar presentations


Ads by Google