When we are writing different algorithms, many parts of the program may have the same functionality or purpose that may lead to rewriting the same computations repeatedly. In this section, we study methods in Java and how they can be used to introduce modularity and the ability to reuse code in our software.
Declaring Methods
What are methods and how can we create them using Java? In this short video we introduce basic concepts related to method declaration.
Calling Methods
Once we create a method devoted to a certain purpose and type of functionality, we can keep reusing the method in our program to provide simplicity and a modular high-level program design. Let’s introduce now the syntax and principles of invoking, or calling methods to use their functionalities in different parts of our program.
Scope
Decisions related to the design of a method often involve also restricting the extent to which it will be accessible for future use, either by the developer themselves or others granted access to the software for execution or development purposes. The design of different information used in the programs, such as variables, methods or even the class itself can be restricted by defining the scope of each construct. Here we define scope in Java programs and how they can be used to integrate more control over our programs.
Overloading
Java gives us the flexibility to use methods in application to variations in the type and amount of arguments. Here we define the concept of overloading and how we can use it in different applications.
Understanding Method Abstraction and Stepwise Refinement
Methods are powerful tools to aid us in developing concise, easy-to-read and modular high-level programs. There are many ways we can design methods, but we must consider some general principles to help us in the design process in order to make the right decisions in developing simple and elegant software solutions.
Exercise: ATM Machine
Build a Java program to represent possible operations using an ATM machine, where a User has to choose input from the options displayed on the Screen. The available options on the Screen include operations such as:
- Withdraw,
- Deposit,
- Check balance,
- Exit
In your solution, you can use the static main method to implement the user interface, and additionally your class should have four more methods, withdraw(), deposit(), check_balance() and exit(), to perform each corresponding operation.
Solution
We can start implementing the ATM Machine application from the main method that manages the main menu that the user interacts with. Here we can begin the program by asking the user the preferred operation, where each operation can simply be represented with a predefined integer. A selective clause like a switch-case can then proceed to invoke corresponding methods depending on the chosen operation.
A common requirement of each operation, including the withdrawal, deposit and balance checking, is the account balance information. We design the program to store the balance information in a global variable, which is passed through each method, and updated and returned if necessary. The first operation, withdrawal, can start execution by asking the user the desired amount, and returning the balance after withdrawal to store for further processing. Here an important step to include before giving back the money to the user is to check whether there is sufficient funds to provide the desired amount. Here since we change the balance we return the updated balance and assign back in the main method to the balance variable. We can use the double type to represent the funds.
The deposit method can be similarly implemented by retrieving the user input, adding the new mount to the current balance and returning the new balance back to the main method where it is stored with its updated value.
If the user only wants to check their balance, we can invoke the check_balance() method, which only needs the balance information to read and display on the screen. Since we do not need to change the balance value during this operation, the method can be of void type, thus, does not return anything.
Finally, the user exits the application with a message reminding the user to take back their card before they leave.
Please feel free to implement your own solutions in the way you find appropriate and contact in case of any questions.