An Object Oriented Approach of using Math.addExact( ) method in Java

This is a tutorial on an object-oriented approach to understand and implement the use of Math.addExact( ) method in Java.  Math.addExact( ) is a method which returns the sum of the arguments of that method. For example Math.addExact(10, 20) returns 30.

All about the Math.addExact( ) method

  • It is a method in the Math class of the java.lang package.
  • The method is static. Thus it does not require the creation of any object.
  • The return type is int or long.
  • The data type of the arguments is either int or long.
  • It throws an ArithmeticException. (more on this later..!)

Syntax:

public static int addExact(int a, int b) //When the return type is int
public static long addExact(long a, long b) //When the return type is long

The Exception

The exception encountered is an ArithmeticException. The size of an integer variable in Java is 4 bytes, and thus it can hold integers from -2,147,483,648 to 2,147,483,647 to +2,147,483,648 to 2,147,483,647. So if the return value of the method exceeds this limit, the method throws an integer overflow ArithmeticException.
Similarly, the size of a long variable is 8 bytes, and thus it can hold integers from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,808 . So if the return value exceeds this limit, the method throws a similar ArithmeticException.
An illustration of the Exception is given below:
https://drive.google.com/open?id=1HL_R8ArX70POZXGWyv0cByu04hCLDQ-i

IMPLEMENTATION

The method is implemented in two ways.

  1.  the addExact( ) method is used directly in the main( ) method.
  2. an object of the class containing the addExact( )  method is created in the main( ) method.

1. The Simple Way:

import java.lang.Math;
import java.util.Scanner;

class UseofAddExact{
  public static void main(String args[]){
    int a, b;
    Scanner s = new Scanner(System.in);
    System.out.println("\nEnter a: ");
    a = s.nextInt();
    System.out.println("\nEnter b: ");
    b = s.nextInt();
    System.out.println("\nThe sum of " + a + " and " + b + " is " + Math.addExact(a,b));
  }
}
OUTPUT:
Enter a:
8
Enter b:
10
The sum of 8 and 10 is 18

 

2. An Object-Oriented Approach:

import java.lang.Math; 
import java.util.Scanner;

public class UseofAddExact{  
  int firstNumber, secondNumber; 
  public UseofAddExact(int firstNumber, int secondNumber){  //UseofAddExact class constructor
    this.firstNumber = firstNumber;
    this.secondNumber = secondNumber;
  }
  public int addParameters(int firstNumber, int secondNumber){  //addParameters method, which uses AddExact() method
    return Math.addExact(firstNumber,secondNumber);  //Math.addExact( ) method
  }
}

class Test{
  public static void main(String args[]){
    int num1, num2;
    Scanner s = new Scanner(System.in); //Scanner class object is created
    System.out.println("\nEnter first parameter value");
    num1 = s.nextInt(); //Input 1
    System.out.println("\nEnter second parameter value");
    num2 = s.nextInt(); //Input 2
    UseofAddExact ob = new UseofAddExact(num1,num2); //object of UseofAddExact class is created
    int result = ob.addParameters(num1, num2); //addParameters( ) method is called.
    System.out.println("The sum of " + num1 +" and "+ num2 + " is "+result);
  }
}
OUTPUT
Enter first parameter value
8
Enter second parameter value
10
The sum of 8 and 10 is 18

Also, read: Sorting comma-separated numbers in a string in Java

The use of functions has always shortened the lines of code and has always ensured code reusability. So, next time whenever you require to add two numbers within your code, instead of declaring two separate variables or using a separate variable to save the result, use this method – it saves time as well as space.

Good Luck!

 

Leave a Reply

Your email address will not be published. Required fields are marked *