A method is normally created to execute some repetitive code. For example, lets say you want to perform the quadratic formula on 10 different instances; Instead of writing the quadratic formula 10 different times, you create a method, pass the necessary parameters (in this case a, b and c) and have the code method return the value.
I honestly use it to make my code look cleaner and for re-usability across multiple classes.
In the following example, we'll find the greatest common divisor by utilizing the following steps:
I honestly use it to make my code look cleaner and for re-usability across multiple classes.
In the following example, we'll find the greatest common divisor by utilizing the following steps:
- Instantiate the Scanner class
- Prompt for first integer and store
- Prompt for second integer and store
- Call the gcd() method to calculate the greatest common divisor
- Return the value and display it to the screen
Comments
Post a Comment