Can you also explain how the x value has been reassigned? This question refers t
ID: 3583908 • Letter: C
Question
Can you also explain how the x value has been reassigned?
This question refers to the following class: public class IntObject {private int num; public IntObject()//default constructor {num = n;} public IntObject(int n)//constructor {num = n;} public void increment()//increment by 1 {num^++;}} Here is a client program that uses this class: public class IntObjectTest {public static IntObject someMethod(IntObject obj) {IntObject ans = obj; ans.increment(); return ans;} public static void main(String[] args) {IntObject x = new IntObject(2); IntObject y = new IntObject (7); IntObject a = y; x = someMethod(y); a = someMethod(x);}} Just before exiting this program, what are the object values of x, y, and a, respectively?Explanation / Answer
Before exiting the program the values of the variables are
X= 8
y=7
a= 9
These are the values which are stored in those variables.
And coming to the variable x
At first when the program execution starts x value will be 2
But when the execution comes to the beliw statement
x= someMethod (y):
X value will be reassigned to the new value 8. How this x value reassigned is x in the above mentioned statement
X is assigned to someMethod(y), here y value is 7 which this value will be passed to the someMethod() method and in that method input value will be incremented because of increment function.
So finally after value is incremented the value will be 8.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.