Question

In: Computer Science

L4 (0.4 marks) Code a calling and called method that tries to demonstrate that a called...

L4 (0.4 marks) Code a calling and called method that tries to demonstrate that a called method cannot remember how many times it has been called if it’s limited to only using its formal parameters and local variables. For technical reasons this cannot be demonstrated. You will discover why in your attempt. Now use a class-level variable to do the job properly.

Solutions

Expert Solution

public class CallingMethod {
   private static void fun() {
       // here declaring the local variable
       int count = 0;
       //increasing the count
       count++;
       //printing the count
       System.out.println(count + " time Called..");
   }

   public static void main(String[] args) {
       //calling the method
       fun();
       //calling the method
       fun();
       //calling the method
       fun();
   }
}

In the above we can see every time it seeing as only 1 because local variable will be created every time when a function is called.

To change this we need to use class level variable so that it will not be created everytime

public class CallingMethod {
   static int count = 0;
   private static void fun() {
       // here declaring the local variable
       //increasing the count
       count++;
       //printing the count
       System.out.println(count + " time Called..");
   }

   public static void main(String[] args) {
       //calling the method
       fun();
       //calling the method
       fun();
       //calling the method
       fun();
   }
}

Now we are able to find the correct count of callings

Note : If you like my answer please rate and help me it is very Imp for me


Related Solutions

Method calling in c# I need to write methods for calling the min and max numbers...
Method calling in c# I need to write methods for calling the min and max numbers using this code; Console.WriteLine("Calling highest method."); Console.WriteLine("Highest number is: {0}", highest(3)); Console.WriteLine("Calling lowest method."); Console.WriteLine("Lowest number is: {0}", lowest(3));
JAVA CODE Give the definition of a static method called showArray that has an array of...
JAVA CODE Give the definition of a static method called showArray that has an array of base type char as a single parameter and that writes to the screen each character in the array on a separate line. It then writes the same array characters in reverse order. This method returns a character array that holds these two lines of characters in the order that you printed them.
JAVA CODE // Write a method called example that will do several things. // It has...
JAVA CODE // Write a method called example that will do several things. // It has two integer array parameters of unknown varying lengths. // It returns an integer array that contains each of the first array values // divided by two and each of the second array values added to the number 100. // It prints each value of the first array and then prints that value // divided by two on a separate line. It then prints each...
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Write a method that returns the result when the calling object is multiplied by a scalar...
Write a method that returns the result when the calling object is multiplied by a scalar value. For example, the PolyTerm 2.4x^3 multiplied by -1.2 should return the PolyTerm object representing -2.88x^3. Language: Java. Method name be like: scalarMultiply(double) Some Outputs: Test 1: Coefficient =1, Exponent = 1 scalarMultiply(1.2).coefficient return 1.2; scalarMultiply(1.2).exponent returns 1. Test 2: Coefficient =2.4, Exponent = 3 scalarMultiply(-1.2).coefficient returns -2.88 scalarMultiply(-1.2).exponent return 3 Test 3: Coefficient =-1.5 Exponent = 0 scalarMultiply(0).coefficient returns 0 scalarMultiply(0).exponent returns 3...
Question 1 Risk and Return (TOTAL: 25 MARKS) 1.1 Investors often use a method called the...
Question 1 Risk and Return (TOTAL: 25 MARKS) 1.1 Investors often use a method called the capital asset pricing method (CAPM) to measure the required rate of return. a) Define the capital asset pricing method b) Explain how CAPM is useful for measuring the required rate of return 1.2 Using an example, discuss how diversification of investments affects the riskiness and expected rate of return of a portfolio. 1.3 Explain how COVID-19 impacts the profitability, liquidity, and cash flow projections...
Question 1 Risk and Return (TOTAL: 25 MARKS) 1.1 Investors often use a method called the...
Question 1 Risk and Return (TOTAL: 25 MARKS) 1.1 Investors often use a method called the capital asset pricing method (CAPM) to measure the required rate of return. a) Define the capital asset pricing method b) Explain how CAPM is useful for measuring the required rate of return 1.2 Using an example, discuss how diversification of investments affects the riskiness and expected rate of return of a portfolio. 1.3 Explain how COVID-19 impacts the profitability, liquidity, and cash flow projections...
1. Coding Assignment the program will start the menu calling the method extractLargestAndSmallestYourName(), which is specified...
1. Coding Assignment the program will start the menu calling the method extractLargestAndSmallestYourName(), which is specified as below.  The method extractLargestAndSmallestYourName() will receive 2 array of integers as arguments, which are denoted as array number 1 and array number 2.  The method will then search for the largest value and the smallest value. The search will also track from which array(s) these values can be found; and  The information will be return an array, which has the...
In an Inclusion relationship, including a use case is similar to calling another use case, method,...
In an Inclusion relationship, including a use case is similar to calling another use case, method, function, or a process. Draw a use case model. In a pantry area a student warms up a cup of coffee in a microwave oven for 55 seconds. (Hint: Use Include relationship between the use cases) Identify actors and use cases Give all steps from opening the oven until the beep of the oven Draw a complete use case diagram using UML notations.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT