search
HomeJavajavaTutorialJava program to find the area of ​​a square using method overloading

Java program to find the area of ​​a square using method overloading

Sep 17, 2023 pm 10:21 PM
areaMethod overloadingsquare

Java program to find the area of ​​a square using method overloading

We can use method overloading to calculate the area of ​​a square in Java. "Method overloading" is a feature in Java that allows people to write multiple method names in the same class using the same method name. It will allow us to declare multiple methods with the same name but with different signatures, i.e. the number of parameters in the methods may be different or the data types of the parameters may be different. Method overloading helps us increase the readability of our code so that we can use the same method in different ways.

Now, let us take "Area of ​​a Square" as an example to implement method overloading in Java.

Area of ​​a square

The area of ​​a square is the defined area it occupies on a two-dimensional plane. We can calculate the area of ​​a square by multiplying the side length * side length.

Area of Square =  s*s
where	 
s: side of square                              

In the following example, we will take the area of ​​a square as an example and implement method overloading in Java by changing the data type of the parameter.

algorithm

Step 1 - Write a custom class to find the area of ​​a square.

STEP 2 - Initialize a pair of two variables of different data types in the main method of the public class.

Step 3 - Create an object of the custom class in the main method of the public class.

Step 4 − Call a specific method to find the area of ​​the square using the custom object you created.

The Chinese translation of

Example

is:

Example

In this example, we calculate the area of ​​a square using basic formulas and implement method overloading in Java.

Method overloading is achieved by changing the parameter type in the "areaOfSquare" method. Now, when the user inputs a parameter value of integer type to the areaOfSquare method, the first areaOfSquare method of the Area class is called and outputs the result. If the user enters a parameter of double type, the second areaOfSquare method is called and executed.

//Java Code to achieve Method Overloading in Java by Area of Square.
import java.io.*;
class Area {
   // In this example area method is overloaded by changing the type of parameters.
   public void areaOfSquare(int side) {
      int area = 0;
      area = side * side;
      System.out.println("Area of the square is :" + area);
   }
   public void areaOfSquare(double side) {
      double area= 0;
      area = side*side;
      System.out.println("Area of the square is:" + area);
   }
}
public class Main {
   public static void main(String args[]) {
      Area Object  = new Area();
      int side_1= 3;
      Object.areaOfSquare(side_1);
      double side_2 = 4.5;
      Object.areaOfSquare(side_2);
   }
}

Output

Area of the square is :9
Area of the square is:20.25                             

Time complexity: O(1) Auxiliary space: O(1)

The Chinese translation of

Example

is:

Example

In this example, we use the Math.pow() function to calculate the area of ​​a square and implement method overloading in Java.

Method overloading is achieved by changing the parameter type in the "areaOfSquare" method. Now, when the user inputs a parameter value of integer type to the areaOfSquare method, the first areaOfSquare method of the Area class is called and outputs the result. If the user enters a parameter of double type, the second areaOfSquare method is called and executed.

//Java Code to achieve Method Overloading in Java by Area of Square.
import java.io.*;
class Area {
   // In this example area method is overloaded by changing the type of parameters.
   public void areaOfSquare(int side) {
      int area = 0;
      area =(int) Math.pow(side,2);
      System.out.println("Area of the square is :" + area);
   }
   public void areaOfSquare(double side) {
      double area= 0;
      area = Math.pow(side,2);
      System.out.println("Area of the square is:" + area);
   }
}
public class Main {
   public static void main(String args[]) {
      Area Object  = new Area();
      int side_1= 3;
      Object.areaOfSquare(side_1);
      double side_2 = 4.5;
      Object.areaOfSquare(side_2);
   }
}

Output

Area of the square is :9
Area of the square is:20.25                            

Time complexity: O(1) Auxiliary space: O(1)

So, in this article, we take finding the area of ​​a square as an example and learn how to implement method overloading in Java by changing the data type of the parameter.

The above is the detailed content of Java program to find the area of ​​a square using method overloading. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft