Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose we have an Acct class in Java, which has an instance variable balance an

ID: 3822613 • Letter: S

Question

Suppose we have an Acct class in Java, which has an instance variable balance and also an instance method add (float amt), which adds the argument amt to balance. We have also the following deposit() method. void deposit (float amt, Acct ac) {ac.add (amt);} (i) Recall that the Java language supports only pass-by-value parameter passing, which is an in-mode mechanism. Consider the following Java code fragment. Assuming that myAC.balance is 100 before the call to deposit (), what is the value of myAC.balance after executing the following code fragment? Acct myAC = new Acct; ... deposit (37.5, myAC); Give detailed steps to explain how you obtain your answer, perhaps with the aid of memory diagrams. ii) Object variables in Java sometimes behave like pointer variables and sometimes like reference variables. Explain the behavior (as pointer or reference) of myAC in parameter passing stage of the method call, and also the behavior of ac in the body of deposit ().

Explanation / Answer

i) Balance will now be 137.5

100 existing balance and 37.5 added balance.

This balance is being added to myAc instance by call to method add.

ii) when myAc is passed as parameter a copy of my account is sent. Any change to myAc (ac in the program will not reflect on myAc object)

In body because using copy internal method of myAc will be visible and hence changes to internal data can be made.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote