Methods

1. 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!

In Java, a method is like the magic box that performs a specfic task by running a block of code using 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:

  1. access specifier. public tells the computer that anyone can use this method. private if it can only be called within a class. (We’ll learn about classes on the next page!)

  2. return type. void means the method doesn’t return anything.

  3. method name.

  4. list of parameters/ user inputs in the format of (type input name, type input name, … ).

// an example
public void sing (String songName)
   1.    2.   3.        4.


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.

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); 

2. Count the Pyramid!🔺

(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!