Question

In: Computer Science

This question is related to making an object method call by reference instead of by value....

  1. This question is related to making an object method call by reference instead of by value. Use the (CallValueTest.java). Convert the calculation method (calc(..) ) in the Test class. You need to modify the Test class attributes and constructor to support this new cal (…) method. Your program should display:

Before call obj.a 15 obj.b is 20

Before call obj.a 30 obj.b is 10

CallValueTest.java

package com.Assignment3.Q1;

class Test {
  
   void calc(int a, int b) {
       a *= 2;
       b /= 2;
       System.out.println(" in calc a = " + a + " b = " + b);
   }
  
}
public class CallByValueTest {
   public static void main (String args[]) {
       Test ob = new Test();
       int a =15, b = 20;
       System.out.println ("Before calc :a = " + a + " b " + b);
      
       ob.calc (a, b );
      
       System.out.println(" After calc: a = " + a + " b " + b);
      
   }
}

Solutions

Expert Solution

//Java code

package com.Assignment3.Q1;

/**
 * Since in call by value, We are passing primitive type(int, char..) Which supports pass by value.
 * To convert the it into call by reference, We need to wrap them into reference type (Object).
 */
class Test {
    //attributes 
    int a,b;

    void calc() {
        a *= 2;
        b /= 2;
        System.out.println(" in calc a = " + a + " b = " + b);
    }

}
public class CallByRefTest {
    public static void main (String args[]) {

        Test ob = new Test();
            ob.a=15;
            ob.b =20;
        System.out.println ("Before calc :a = " + ob.a + " b " + ob.b);
        ob.calc ( );

        System.out.println(" After calc: a = " + ob.a + " b " + ob.b);

    }
}

//output

//If you need any help regarding this solution ......... please leave a comment ...... thanks


Related Solutions

Making reference to an organisation that you are familiar with: QUESTION 1 Provide a detailed explanation...
Making reference to an organisation that you are familiar with: QUESTION 1 Provide a detailed explanation with examples of the market/task environment of the organisation you selected. QUESTION 2 Identify and detail the activities and initiatives of the following functional areas of management in the organisation you chose.  The Human Resources Function  The Marketing Function  Purchasing Function  Operations function QUESTION 3 Provide a detailed discussion of FIVE (5) management challenges faced by the selected organisation. it...
Making reference to an organisation that you are familiar with: QUESTION 1.1 Provide a detailed explanation...
Making reference to an organisation that you are familiar with: QUESTION 1.1 Provide a detailed explanation with examples of the market/task environment of the organisation you selected. QUESTION 1.2 Identify and detail the activities and initiatives of the following functional areas of management in the organisation you chose.  The Human Resources Function  The Marketing Function  Purchasing Function  Operations function QUESTION 1.3 Provide a detailed discussion of FIVE (5) management challenges faced by the selected organisation. it...
For all of these questions don't use the p value method instead use the "5 steps...
For all of these questions don't use the p value method instead use the "5 steps of hypothesis testing." Or I will get all of these wrong and I really need the help!! 4) 18.3% of incoming freshmen indicate they will major in business or a related field. A random sample of 350 incoming college freshmen were asked their preference and 65 replied they were considering business as a major. Estimate the true proportion of freshman business majors with 98%...
push(self, value: object) -> None: This method adds a new element to the top of the...
push(self, value: object) -> None: This method adds a new element to the top of the stack. Output: STACK: 0 elements. [] STACK: 5 elements. [1, 2, 3, 4, 5] pop(self) -> object: Output: Exception: <class ' main .StackException'> 5 4 3 2 1 Exception: <class ' main .StackException' top(self) -> object: Output: No elements in stack <class ' main .StackException'> STACK: 2 elements. [10, 20] 20 20 STACK: 2 elements. [10, 20] #Code from dynamic_array import * class StackException(Exception):...
Don't use the p value method instead use the "5 steps of hypothesis testing." Or I...
Don't use the p value method instead use the "5 steps of hypothesis testing." Or I will get all of these wrong and I really need the help!! 9) An agent claims there is no difference between the pay of safeties and linebackers in the NFL. A survey of 15 randomly selected safeties found an average salary of $510,580. The standard deviation for the sample was $18,000. A survey of 15 randomly selected linebackers found an average salary of $527,360...
Physical Units Method, Relative Sales-Value-at-Split-off Method, Net Realizable Value Method, Decision Making Sonimad Sawmill, Inc. (SSI),...
Physical Units Method, Relative Sales-Value-at-Split-off Method, Net Realizable Value Method, Decision Making Sonimad Sawmill, Inc. (SSI), purchases logs from independent timber contractors and processes them into the following three types of lumber products: Studs for residential construction (e.g., walls and ceilings) Decorative pieces (e.g., fireplace mantels and beams for cathedral ceilings) Posts used as support braces (e.g., mine support braces and braces for exterior fences around ranch properties) These products are the result of a joint sawmill process that involves...
Physical Units Method, Relative Sales-Value-at-Split-off Method, Net Realizable Value Method, Decision Making Sonimad Sawmill, Inc. (SSI),...
Physical Units Method, Relative Sales-Value-at-Split-off Method, Net Realizable Value Method, Decision Making Sonimad Sawmill, Inc. (SSI), purchases logs from independent timber contractors and processes them into the following three types of lumber products: Studs for residential construction (e.g., walls and ceilings) Decorative pieces (e.g., fireplace mantels and beams for cathedral ceilings) Posts used as support braces (e.g., mine support braces and braces for exterior fences around ranch properties) These products are the result of a joint sawmill process that involves...
Physical Units Method, Relative Sales-Value-at-Split-off Method, Net Realizable Value Method, Decision Making Sonimad Sawmill, Inc. (SSI),...
Physical Units Method, Relative Sales-Value-at-Split-off Method, Net Realizable Value Method, Decision Making Sonimad Sawmill, Inc. (SSI), purchases logs from independent timber contractors and processes them into the following three types of lumber products: Studs for residential construction (e.g., walls and ceilings) Decorative pieces (e.g., fireplace mantels and beams for cathedral ceilings) Posts used as support braces (e.g., mine support braces and braces for exterior fences around ranch properties) These products are the result of a joint sawmill process that involves...
Physical Units Method, Relative Sales-Value-at-Split-off Method, Net Realizable Value Method, Decision Making Sonimad Sawmill, Inc. (SSI),...
Physical Units Method, Relative Sales-Value-at-Split-off Method, Net Realizable Value Method, Decision Making Sonimad Sawmill, Inc. (SSI), purchases logs from independent timber contractors and processes them into the following three types of lumber products: Studs for residential construction (e.g., walls and ceilings) Decorative pieces (e.g., fireplace mantels and beams for cathedral ceilings) Posts used as support braces (e.g., mine support braces and braces for exterior fences around ranch properties) These products are the result of a joint sawmill process that involves...
Physical Units Method, Relative Sales-Value-at-Split-off Method, Net Realizable Value Method, Decision Making Sonimad Sawmill, Inc. (SSI),...
Physical Units Method, Relative Sales-Value-at-Split-off Method, Net Realizable Value Method, Decision Making Sonimad Sawmill, Inc. (SSI), purchases logs from independent timber contractors and processes them into the following three types of lumber products: Studs for residential construction (e.g., walls and ceilings) Decorative pieces (e.g., fireplace mantels and beams for cathedral ceilings) Posts used as support braces (e.g., mine support braces and braces for exterior fences around ranch properties) These products are the result of a joint sawmill process that involves...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT