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 Write two definitions of a boolean method called equals(). The method compares the instance...
JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance variables of the class for equality. One is in the Purchase class and the other is a static method of the main. Give sample calls for each. public class PurchaseDemo { public static void main(String[] args) { Purchase oneSale = new Purchase(); oneSale.readInput(); oneSale.writeOutput(); System.out.println("Cost each $" + oneSale.getUnitCost()); System.out.println("Total cost $" + oneSale.getTotalCost()); } } import java.util.Scanner; /**    Class for the purchase...
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the...
JAVA CODE Define a method called changeGroupPrice that could be added to the definition of the class Purchase. This method has one parameter that is of type double, and is named salePercent. This number represents the percent reduction in the groupPrice amount.   The method uses this number to change the groupPrice. The code of the method should check to make sure the range of salePercent is between 0 and 50%. If it is, the groupPrice should be adjusted accordingly. If...
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...
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.
The ( ) method will be called if a method is called from within a subclass...
The ( ) method will be called if a method is called from within a subclass that overrides a super class method. In Java the (          ) keyword is used to prevent changes being made to a variable. The property of ( ) refers to the ability of an object to take on many forms. In Java, a character constant’s value is its integer value in the ( ) character set. An interface requires each of the interface’s methods to...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT