Simple Addition Program in Java

In this Java program, the user is prompt to input two integer numbers and displays the sum in Graphical User interface (GUI).
  • Problem: Write and applicaton the will display the sum, inputs by the user in Graphical User Interface(GUI).

import javax.swing.*;

public class  Addition{

    public static void main(String args[]){

        //Declaring the variables
        String fNumber, sNumber;
        int num1, num2, sum;

        //Reads the input values by the user
        fNumber = JOptionPane.showInputDialog("Enter first number: ");
        sNumber = JOptionPane.showInputDialog("Enter second number");

        //Convert the values int integers

        num1 = Integer.parseInt(fNumber);
        num2 = Integer.parseInt(sNumber);

        //Initialize and calculate
        sum = num1 + num2;

        //Displays the output in Graphical User Interface
        JOptionPane.showMessageDialog(null, "The result is: " + sum, "Addition Program",
                JOptionPane.PLAIN_MESSAGE);

        System.exit(0);

    }
}

Here is the output. In this case assumes user inputs the number "2", "3"




Comments

Popular posts from this blog

Calculate the Sum, Product, Difference & Quotient in Java

Calculates the Squares and Cubes in Java

Determine Two Numbers which is the largest or Both Numbers are Equal in Java