Java Double Sum Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the sum() method of the Double data type and how to use it to add two numbers. The sum() method is very useful when we want to add two floating-point numbers. It returns the numeric sum of the two double values passed as arguments. The sum() method is part of the java.lang package.

Create a Java file

Create a java file named DoubleSum.java in the ~/project directory and open it.

touch ~/project/DoubleSum.java

Import Required Packages

In the first line of the program, import the required packages as shown below:

import java.lang.Double;
import java.util.Scanner;

Create a main() method

Create the main() method. This method accepts two double values from the user and adds them using the Double.sum() method.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter first number: ");
    double num1 = input.nextDouble();
    System.out.print("Enter second number: ");
    double num2 = input.nextDouble();
    double sum = Double.sum(num1, num2);
    System.out.println("The sum is: " + sum);
}

Compile and Run the Program

Compile the program using the javac command:

javac ~/project/DoubleSum.java

Once the compilation is successful, run the program using the java command:

java DoubleSum

Test the Program

Enter two floating-point numbers when prompted by the program. The program should add the two numbers and display their sum.

Enter first number: 1.5
Enter second number: 2.5
The sum is: 4.0

Test with Invalid Input

If you enter invalid input, such as a string or character, the program should display an error message.

Enter first number: a
Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:864)
        at java.util.Scanner.next(Scanner.java:1485)
        at java.util.Scanner.nextDouble(Scanner.java:2413)
        at DoubleSum.main(DoubleSum.java:9)

Test with Negative Numbers

Test the program with negative numbers as input. The program should correctly add and return the sum for negative values.

Enter first number: -1.5
Enter second number: 2.5
The sum is: 1.0

Test with Decimal Values

Test the program with decimal values as input. The program should correctly add and return the sum for decimal values.

Enter first number: 2.25
Enter second number: 3.75
The sum is: 6.0

Modify the Program

Modify the program to allow multiple pairs of numbers to be added. Add a loop to get user input and add the numbers until the user enters a negative number.

Scanner input = new Scanner(System.in);
double sum = 0;
while (true) {
    System.out.print("Enter a number (or a negative number to exit): ");
    double num = input.nextDouble();
    if (num < 0) {
        break;
    }
    sum = Double.sum(sum, num);
    System.out.println("Current sum: " + sum);
}
System.out.println("Total sum: " + sum);

Compile and Run the Modified Program

Compile the modified program using the javac command:

javac ~/project/DoubleSum.java

Once the compilation is successful, run the modified program using the java command:

java DoubleSum

Summary

In this lab, you learned how to use the sum() method of the Double data type to add two floating-point numbers and return the result in numeric sum. You also learned how to get user input and display the sum of multiple numbers. The sum() method is very useful when we want to add two floating-point numbers.

Other Java Tutorials you may like