DEV Community

Ajay Sundar
Ajay Sundar

Posted on

The Secret Agents of Java : Methods

The Magic Toolbox: How Java Methods Solve Problems

Imagine you are an engineer in a huge workshop. You have a big task: fixing different types of machines. Every time a machine breaks down, you could grab all the tools lying around, figure out how to fix it from scratch, and waste a lot of time.

But luckily, you own a magic toolbox.

Inside this toolbox, each drawer is already prepared with the right tools for a specific task. One drawer fixes engines, another repairs belts, and another one polishes gears acting as a secret agent in Java. Instead of starting over each time, you simply open the correct drawer, use it, and the problem is solved quickly.

In the world of Java, this magic toolbox is nothing but Methods.

A method is like a drawer in your toolbox.

Each method is designed to perform one specific job.

Whenever you face a problem, instead of writing the entire solution again, you just “call” the method.

What is a Method in Java ?

A method is a block of code that performs a specific task.
Instead of repeating code multiple times, you can write it once inside a method and then reuse it whenever needed.

Understanding Methods by Arithmetic operations

now we will learn how to declare and call a method in java by using arithmetic operators

public class Main  {
    public  static void main (String[] args){
        Addition();
    }
    public static void Addition(){
        int a= 200;
        int b= 100;
        int c= 50;
        int d=a+b+c;
        System.out.println("Addition of the numbers :"+a + "+" +b + "+" +c + "=" +d);

        subtraction();
    }

    public static void subtraction(){
        int a=500;
        int b=100;
        int c=50;
        int d=a-b-c;
        System.out.println("Subtraction of the numbers :"+a +"-" +b +"-" +c + "=" +d);

        multiplication();
    }
    public static void multiplication(){
        int a=100;
        int b=100;
        int c=100;
        int d=a*b*c;
        System.out.println("Multiplication of the  numbers :"+a +"*" +b +"*" +c +"=" +d);

    division();
}
    public static void division(){
    int a=30;
    int b=5;
    int c=2;
    int d=a/b/c;
    System.out.println("Division of  numbers :"+a +"/" +b +"/" +c +"=" +d);

    modulus();
}
   public static void modulus(){
    int a=30;
    int b=5;
    int c=2;
    int d=a%b%c;
    System.out.println("Modulus of numbers :"+a +"%" +b +"%" +c +"=" +d);
    }
}

OUTPUT

Addition of the numbers :200+100+50=350
Subtraction of the numbers :500-100-50=350
Multiplication of the  numbers :100*100*100=1000000
Division of  numbers :30/5/2=3
Modulus of numbers :30%5%2=0

Enter fullscreen mode Exit fullscreen mode

Understanding Method Declaration and Method Calling in Java

In Java, methods allow us to organize code into reusable blocks that perform specific tasks. This improves code readability, reduces redundancy, and makes debugging easier. In this example, we demonstrate how to declare methods and call them from one another using basic arithmetic operations: addition, subtraction, multiplication, division, and modulus.

Final Thoughts

Methods help you structure your Java programs logically. By declaring and calling methods, you can avoid redundancy, improve readability, and build scalable applications.

Top comments (0)