Java Program to Display Area taking Side Value from Parameterized Method

Java Program to Display Area taking Side Value from Parameterized Method

We can use methods in Java programs. In this program, I'm going to use parameterized method, which means the method has parameters. 
The problem is to display the area according to the side value given as the argument while the method calls. 
I'm using Example1 class where side and area are taken as two member variables. Both the variables are private here. Inside the class, myMethod() is the parameterized method that takes the side value and calculates the area, and then displays it.
As the side is taken double data typed, thus we are providing 3.12 as the side value and getting 9.7344 as the area value. 

The program is given below, with the proper output. The explanations of the coding are given here with the line comments.

Program Name: MyClassProgram.java

class Example1
{
    private double side, area; // member variables
    public void myMethod(double x) // member method - 1 parameter - parameterized method
    {
        side = x;  // side initialized
        area = side * side; // area calculated
        System.out.println("The area of the square is " + area); // area of square
    }    
}

public class MyClassProgram //main class
{
    public static void main(String[] args)
    {
        Example1 test = new Example1(); // object test is created
        test.myMethod(3.12); // calling method by object with 1 argument
    }
}

Output:

The area of the square is 9.7344

If you like the program kindly subscribe to the site for more programs. And don't forget to give your valuable comments before leaving.