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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/booleans("`Booleans`") java/BasicSyntaxGroup -.-> java/break_continue("`Break/Continue`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/BasicSyntaxGroup -.-> java/while_loop("`While Loop`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117630{{"`Java Double Sum Method`"}} java/class_methods -.-> lab-117630{{"`Java Double Sum Method`"}} java/modifiers -.-> lab-117630{{"`Java Double Sum Method`"}} java/oop -.-> lab-117630{{"`Java Double Sum Method`"}} java/packages_api -.-> lab-117630{{"`Java Double Sum Method`"}} java/user_input -.-> lab-117630{{"`Java Double Sum Method`"}} java/wrapper_classes -.-> lab-117630{{"`Java Double Sum Method`"}} java/identifier -.-> lab-117630{{"`Java Double Sum Method`"}} java/arrays -.-> lab-117630{{"`Java Double Sum Method`"}} java/booleans -.-> lab-117630{{"`Java Double Sum Method`"}} java/break_continue -.-> lab-117630{{"`Java Double Sum Method`"}} java/data_types -.-> lab-117630{{"`Java Double Sum Method`"}} java/if_else -.-> lab-117630{{"`Java Double Sum Method`"}} java/operators -.-> lab-117630{{"`Java Double Sum Method`"}} java/output -.-> lab-117630{{"`Java Double Sum Method`"}} java/strings -.-> lab-117630{{"`Java Double Sum Method`"}} java/variables -.-> lab-117630{{"`Java Double Sum Method`"}} java/while_loop -.-> lab-117630{{"`Java Double Sum Method`"}} java/system_methods -.-> lab-117630{{"`Java Double Sum Method`"}} end

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