Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Methods Practice | Methods
Java Extended

Methods Practice

Practice

Methods are not the easiest topic to understand, so this chapter will be filled with several examples of using different methods for different tasks. This way, you will be able to see more of how do methods work and where they can be used. We will also explore examples of using and writing two or more methods and their utilization.

Example 1

Let's start with a method that takes an array of type int and reverses it so that the last value becomes the first and the first becomes the last:

java

Main.java

  • First, let's pay attention to the method flipIntArray, which uses a loop that counts from the end of the array and stores its values in a newly created array of the same size. There is also an index variable that increases to store the values in the result array;
  • Next, you can see the method from the previous chapter that I use to print the array in just one line of code;
  • Now, let's move on to something interesting. You can see that I am calling a method as a parameter inside another method. This is because I am using a method that returns int[] inside a method that takes int[] as a parameter, thus saving the memory allocated for creating variables. We can see that our methods are working and displaying the correct values.

Example 2

Let's move on to the next example. Suppose we need to write a method that calculates the factorial, taking an integer variable as input.

Note

The factorial is the product of all the numbers from 1 to the given number. For example, the factorial of 5 is calculated as 5 * 4 * 3 * 2 * 1, resulting in 120.

java

Main.java

In this example, we have written a method that multiplies the accumulating variable by numbers from 1 to the given number in order to calculate the factorial. It's a straightforward approach, and you can see how we use this method multiple times in the main method, which saves space and improves code readability. Next, we use our method inside System.out.println because the method returns an int value.

Example 3

Let's move on to the third example. We will write a method that creates an abbreviation from an array of strings.

Note

An abbreviation is a shortened form of longer words or phrases, where only the first letter of each word is written in uppercase. For example, "UN" stands for "United Nations."

java

Main.java

In this method, we use the StringBuilder class to call the append() method and add the uppercase first letter to an initially empty string. This way, we iterate through each element of the array, take the letter at the zeroth position (the first letter of each word), convert it to uppercase using the toUpperCase() method, and add it to our result using the append() method. Finally, we use the toString() method to convert our StringBuilder to String.

1. What is a return value in a method?
2. What does a void method return?
3. Can a method be used inside another method?

What is a return value in a method?

Select the correct answer

What does a void method return?

Select the correct answer

Can a method be used inside another method?

Select the correct answer

Everything was clear?

Section 2. Chapter 5

Methods Practice

Practice

Methods are not the easiest topic to understand, so this chapter will be filled with several examples of using different methods for different tasks. This way, you will be able to see more of how do methods work and where they can be used. We will also explore examples of using and writing two or more methods and their utilization.

Example 1

Let's start with a method that takes an array of type int and reverses it so that the last value becomes the first and the first becomes the last:

java

Main.java

  • First, let's pay attention to the method flipIntArray, which uses a loop that counts from the end of the array and stores its values in a newly created array of the same size. There is also an index variable that increases to store the values in the result array;
  • Next, you can see the method from the previous chapter that I use to print the array in just one line of code;
  • Now, let's move on to something interesting. You can see that I am calling a method as a parameter inside another method. This is because I am using a method that returns int[] inside a method that takes int[] as a parameter, thus saving the memory allocated for creating variables. We can see that our methods are working and displaying the correct values.

Example 2

Let's move on to the next example. Suppose we need to write a method that calculates the factorial, taking an integer variable as input.

Note

The factorial is the product of all the numbers from 1 to the given number. For example, the factorial of 5 is calculated as 5 * 4 * 3 * 2 * 1, resulting in 120.

java

Main.java

In this example, we have written a method that multiplies the accumulating variable by numbers from 1 to the given number in order to calculate the factorial. It's a straightforward approach, and you can see how we use this method multiple times in the main method, which saves space and improves code readability. Next, we use our method inside System.out.println because the method returns an int value.

Example 3

Let's move on to the third example. We will write a method that creates an abbreviation from an array of strings.

Note

An abbreviation is a shortened form of longer words or phrases, where only the first letter of each word is written in uppercase. For example, "UN" stands for "United Nations."

java

Main.java

In this method, we use the StringBuilder class to call the append() method and add the uppercase first letter to an initially empty string. This way, we iterate through each element of the array, take the letter at the zeroth position (the first letter of each word), convert it to uppercase using the toUpperCase() method, and add it to our result using the append() method. Finally, we use the toString() method to convert our StringBuilder to String.

1. What is a return value in a method?
2. What does a void method return?
3. Can a method be used inside another method?

What is a return value in a method?

Select the correct answer

What does a void method return?

Select the correct answer

Can a method be used inside another method?

Select the correct answer

Everything was clear?

Section 2. Chapter 5
some-alt