Question

In: Computer Science

How is this method coded in Java?

How is this method coded in Java?

Solutions

Expert Solution

First of all, let's see what the this() method is:
this(); is a constructor which is used to call another constructor in a class.

this() can have many uses. Some of them are as follows:

  1. this can be passed as an argument in the method call.
  2. this can be passed as an argument in the constructor call.
  3. this can be used to return the current class instance from the method.
  4. this can be used to refer current class instance variable.
  5. this can be used to implicitly invoke a current class method

Please refer to the comments of the program for more clarity and to understand how this method is coded.

class Solution{
  public Solution(int,int)
   { this(10, 20); // Here this will call default constructor
          // Logic goes here
   }
 public Solution()
   {
     //code
   }
 public Solution(float,float)
   { this(); // Here this will call default type constructor
     // Logic goes here
   }
}

So, as we can see this() method is nothing but a method which is ready-made for us to use. Implementation wise it is as similar to a normal method.
Note: These things can be a little intimidating in the beginning. But analysing it, again and again, will make it simpler.

Please let me know in the comments in case of any confusion. Also, please upvote if you like.


Related Solutions

How is this method coded in java? int differential(int[] post) differential computes the virtual differential for...
How is this method coded in java? int differential(int[] post) differential computes the virtual differential for a single post record and returns the differential. The differential is simply the difference between ups and downs. differential is virtual because it is not stored in a post record. /// Compute the differential between "ups" (at index 1) and "downs"   /// (at index 2). The differential is not maintained in the data but is a   /// virtual field derived by the calculation performed...
How is this method coded in Java? int[] sortQuicksort(int[][] view) sortQuicksort performs a quicksort sort on...
How is this method coded in Java? int[] sortQuicksort(int[][] view) sortQuicksort performs a quicksort sort on a view (a referential view of the master set of posts). sortQuicksort returns an array of profile information as detailed in the Data Structures section above. You may implement additional functions to support this sorting operation; however, take care that the profile information is accurately maintained and accounted for over any subsequent function calls. /// Sorts (shallow) a set of references to posts in...
How is this method coded in Java? int[] sortBubble(int[][] view) sortBubble performs a bubble sort on...
How is this method coded in Java? int[] sortBubble(int[][] view) sortBubble performs a bubble sort on a view (a referential view of the master set of posts). sortBubble returns an array of profile information as detailed in the Data Structures section above. You may implement additional functions to support this sorting operation; however, take care that the profile information is accurately maintained and accounted for over any subsequent function calls. /// Sorts (shallow) a set of references to posts in...
Data Structure in Java The following java method counts how many triples of integers in an...
Data Structure in Java The following java method counts how many triples of integers in an array of n distinct integers sum to zero. public static int count(int[] a) { int n = a.length; int count = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { for (int k = j+1; k < n; k++) { if (a[i] + a[j] + a[k] == 0) count++; } } }...
THE FOLLOWING MUST BE CODED IN C THE FOLLOWING MUST BE CODED IN C THE FOLLOWING...
THE FOLLOWING MUST BE CODED IN C THE FOLLOWING MUST BE CODED IN C THE FOLLOWING MUST BE CODED IN C   THE FOLLOWING MUST BE CODED IN C Write a program that demonstrates handling pointers. Create two integer variables m and n, and two pointers, pm pointing to m, and pn pointing to n. Produce the following output: Address of m: 0x7ffcc3ad291c Value of m: 29 Address of n: 0x7ffcc3ad291d Value of n: 34 Now pm is pointed to m...
how do you send the values of a method to the main in obj java. im...
how do you send the values of a method to the main in obj java. im having syntax errors and im not sure how to fix them or what the problem is. class Order { private int orderNum; private double orderAmount; private double orderDiscount;    public Order(int orderNumber, double orderAmt, double orderDisc) { orderNum = orderNumber; orderAmount = orderAmt; orderDiscount = orderDisc; } public double getOrderAmount() { return orderAmount; } public double getOrderDisc() { return orderDiscount; } public static void...
How is sensory information coded? That is, how can your CNS distinguish the type of stimulus,...
How is sensory information coded? That is, how can your CNS distinguish the type of stimulus, the location of the stimulus, and the intensity of the stimulus?
Write the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT