Draw the object diagram if the client code is: Director d = new Director (); Thi
ID: 3826568 • Letter: D
Question
Draw the object diagram if the client code is: Director d = new Director (); This type of object diagram is known as a callback, where 2 objects have references to each other. Now, show what is displayed if the following statements are executed: d.addOne(); d.addOne(); d test(); Feel free to share your work on the forum. public class Director{private int x; private Partner p; public Director (){x = 0; p = new Partner(this);} public void addOne(){x++;} public int getX() {return x;} public void test(){p.doSomething(); p display(); this display();} public void display(){System.out println("value of c is"); System out.println (p.getC());} public class Partner{private Dirctor dir; private int c; public Partner(Director d){dir = d; c = 2; dir.addOne();} public void doSomething(){c+= dir.getX();} public int getC() {return c;} public void display(){System.out println("value of x is:" + dir.getX());}}Explanation / Answer
1.
// create object of Director
Director d = new Director()
Directors constructor is executed
and
x will be assigned 0, new object p of partner will be created
partners decualt constrcutore will be called
and
dir will be assigned d
private integer c will be assigned 2
addOne() method will be called
and x will be incremented to 1
2.
d.addOne();
increment x again to 2
3.
d.addOne();
increment x to 3
4.
d.test();
flow of test method of director goes like this..
doSomething() method of partner will be called
c will be increment by x since getX() will be called
c = c + 3
c = 2 + 3
c = 5
display() method will be called of partner class
value of x will be printed
=> Value of x is : 3
display() method will be called of director class
value of c will be printed
=> Value of c is :
=> 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.