Methods

What is a Method?

So far, we’ve learned about how to store data in variables, print data and sentences, and make decisions with if-statement, for-loop, and while-loop.

From the last exercise, we know the following block of code calculates the sum from 1 to 100:

int total = 0;
int num = 1;
while(num <= 100){
    total = total + num;
    num = num + 1;
}
System.out.println("Sum: " + total);

But what if we want to calculate the sum from 1 to 77 instead? How do we do it without writing the whole blocks of code again?

We want to create a ✨ magic box ✨ in code that does the calculation for us no matter which 2 numbers we want to create a sum for! Num 1 and Num 2 with arrow pointing into a circle with the words Magic Box and an arrow pointing out of the circle pointing to Sum of Num 1 to Num 2

In Java, a method is like the magic box that performs a specific task by running a block of code that can use user inputs.

There are 2 parts in a method: header and body:

Method Header

To define a method, we need to first write out its method header. A method header has four main parts:

Part NameDescriptionExamples or possible options
access specifierprovides the level of access to the methodpublic tells the computer that anyone can use this method. private tells the computer it can only be called within a class. (We’ll learn about classes on the next page!) protected tells the computer it can be called by objects of the same class.
return typedata type that is returned to the calling functionuse void if the method doesn’t return anything.
method namename of the method used to call ituser defined based on what the method does
list of parameterslist of inputs that must be provided when the method is usedformat of (type input name, type input name, … ).
// an example
public void sing (String songName)
   1.    2.   3.        4.

Note: The variable names for the parameters do not have to match the variable name of the data being passed into the method.


Method Body

Next, we put the block of code associated with the method in the method body, which is between { and } after the method header.

To return some data, we put the keyword return followed by a variable name or a value to be returned.

Note: Once you return, nothing else after that in the method runs.

This is an example of how to define a method that sum of numbers from numA through numB:

/* Method Name: sumNum
 * Input/Parameter: 2 numbers of type int
 * Funtionality: return the sum from numA through numB
 */
public int sumNum(int numA, int numB){
    int total = 0;
    int num = numA;      // we access the first input with the name numA
    while(num <= numB){  // we access the first input with the name numB
        total = total + num;
        num = num + 1;
    }
    return total; // return statement
}


Method Call

Lastly, to execute a method in your code, we need to make a method call. We write out the method name with appropriate input. For example:

sumNum(1, 3); // one line of code that calls method sumNum() with the return value 6

Knowing sumNum(1, 3) returns an int with the sum from 1 to 3, we can do the following to store the value:

int sum1to3 = sumNum(1, 3); 

Count the Pyramid!🔺

pyramid with layers of colorful beads. Bottom layer is yellow, next blue, next brown, next white, next pink, next light blue, next orange, next coral, next green, last red

(photo credit: aliexpress.com/item/32306945847.html)

The above is an image of square-number pyramid where each level is a perfect square of the number of current level counting from the top.

That is the top level has 1 * 1 bead, the 2nd level has 2 * 2 beads.


Let’s write a method that takes in the number of total levels and output the total number of beads in the pyramid!

Once you solved the challenge, you will see the following message:

Congratulations! Challenge Solved!