Home
Answered Questions
Computer Science
(3) (10 Points) This Question Is About Understanding The Difference Between Passing A Primitive Data

(3) (10 points) This question is about understanding the difference between passing a primitive data value verse an object reference through a method (argument). For the question, you are asked to convert a program that uses the "pass by value" to "pass by reference" as an argument to a given method with an object. You can download the "pass by value" method program (CallValue Test.java) from eLearn assignment3. The program runs "as it": Before calling t.calc(a,b): a = 15 b = 20 inside t.calc(a, b): a = 30 b = 10 exit from t.calc(a,b): a = 15 b = 20 Convert the calculation method (calc(..)) in the CallValue Test class to use "pass by reference" method. You also need to modify the Test Class attributes for this program to support this. Your program should display: Before calling tx.calc(a, b) 15 b = 20 inside tx.calc(a, b): a = 30 b = 10 exit from tx.calc(a, b): a=30 b = 10
class Test { void calc(int x, int y) { x*= 2; y/=2; System.out.println(" inside t.calc(a, b):\ta="+x+"b="+y); public class CallByValue Test { public static void main (String args[]) { Test t= new Test(); int a =15, b= 20; System.out.println ("Before calling t.calc(a,b): a="+a+"b=" + b); t.calc (a, b); System.out.println(" exit from t.calc(a,b):\ta="+a+"b="+b);

Here is the answer.. one way CODE: class Test{ int a,b; Test(int a,int b){ this.a=a; this.b=b; } void calc(Test obj){ obj.a*=2;