Java

The provided Java code showcases a simple demonstration of mathematical operations encapsulated within a class named MathDemo. This class contains two methods: add for adding two integers and square for calculating the square of an integer.

Understanding the code :

1.   Method add: This method takes two integer parameters, one and two, and prints their sum.

2.   Method square: This method takes an integer parameter number and returns the square of that number.

3.   Main Method: Inside the main method, an instance of the MathDemo class is created. It then demonstrates the usage of the add method by adding two numbers (5 and 6) and printing the result. Additionally, the square method is called with the number 7, and the square value is stored in a variable and printed.

public class MathDemo {
   
    public void add(int one,int two){
        System.out.println("Addition is "+(one+two));
    }


    public int square(int number){
        return number*number;
    }


    public static void main(String[] args) {
        MathDemo md=new MathDemo();
        md.add(5, 6);
        int squareValue=md.square(7);
        System.out.println("Square is "+squareValue);
    }


}

Conclusion :In conclusion, the MathDemo class provides a concise example of encapsulating mathematical operations in Java methods. By encapsulating related functionalities within methods, the code achieves modularity and reusability. The add method showcases how to perform a basic arithmetic operation, while the square method demonstrates a more complex mathematical computation. This approach enhances code readability and maintainability, as each method serves a specific purpose.

Related Posts

Table Of Contents

;