Source of Addition.java


  1: // Fig. 11.2: Addition.java
  2: // Addition program that uses JOptionPane for input and output.
  3: import javax.swing.JOptionPane; // program uses JOptionPane
  4: 
  5: public class Addition 
  6: {
  7:    public static void main( String args[] )
  8:    {
  9:       // obtain user input from JOptionPane input dialogs
 10:       String firstNumber = 
 11:          JOptionPane.showInputDialog( "Enter first integer" );
 12:       String secondNumber =
 13:           JOptionPane.showInputDialog( "Enter second integer" );
 14: 
 15:       // convert String inputs to int values for use in a calculation
 16:       int number1 = Integer.parseInt( firstNumber ); 
 17:       int number2 = Integer.parseInt( secondNumber );
 18: 
 19:       int sum = number1 + number2; // add numbers
 20: 
 21:       // display result in a JOptionPane message dialog
 22:       JOptionPane.showMessageDialog( null, "The sum is " + sum, 
 23:          "Sum of Two Integers", JOptionPane.INFORMATION_MESSAGE );
 24:    } // end method main
 25: } // end class Addition
 26: 
 27: 
 28: /**************************************************************************
 29:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 30:  * Pearson Education, Inc. All Rights Reserved.                           *
 31:  *                                                                        *
 32:  * DISCLAIMER: The authors and publisher of this book have used their     *
 33:  * best efforts in preparing the book. These efforts include the          *
 34:  * development, research, and testing of the theories and programs        *
 35:  * to determine their effectiveness. The authors and publisher make       *
 36:  * no warranty of any kind, expressed or implied, with regard to these    *
 37:  * programs or to the documentation contained in these books. The authors *
 38:  * and publisher shall not be liable in any event for incidental or       *
 39:  * consequential damages in connection with, or arising out of, the       *
 40:  * furnishing, performance, or use of these programs.                     *
 41:  *************************************************************************/