<p>Methods are small blocks of statements that make it easier to solve a problem. It usually focuses on solving a small part of the overall problem.</p>
<p>Usually in methods you provide some sort of input and get some output out of it.</p>
<h3>Advantages</h3>
<ul>
<li>Code readability</li>
<li>Modular program development (break up the problem in chunks)</li>
<li>Incremental development</li>
<li>No redundant code!</li>
</ul>
<h3>Method definition</h3>
<p>Consists of a method name, input and output and the block of statements.</p>
<p>Usually this is succinctly written using JavaDocs which is what you see in the JavaAPI</p>
<h3>Method Call</h3>
<p>A method call is the execution of the method. The statements defined in the method is what will execute.</p>
<h3>Method Stubs</h3>
<p>Recall from method definition the parts of the method definition. Now look at the following method</p>
<p>The program should have a list of grocery prices. It should be able to calculate the total cost of groceries. The store gives a student discount of 5%. The program should calculate this discount and update the total, it should calculate and add the 2.5% tax.</p>
<ul>
<li>First you should add it all up</li>
<li>Then compute the discount</li>
<li>Then add the tax</li>
</ul>
<h2>Parts of a method definition</h2>
<pre><codeclass="language-java">public static int timesTwo(int num) {
int two = num * 2;
return two;
}</code></pre>
<p>It first starts off by declaring the visibility <code>public</code></p>
<p>The return type if <code>int</code></p>
<p>The method name is <code>timesTwo</code></p>
<p>The input parameter is <code>int num</code></p>
<p>Between the curly braces is the <em>body</em> of the method</p>
<h2>Calling a Method</h2>
<pre><codeclass="language-java">int a = 5;
int b = 3;
int ans = multiply(a, b)</code></pre>
<p>The method call is <code>multiply(a, b)</code> and the result is stored in the variable <code>ans</code></p>