Source of Addition.java


  1: // Fig. 2.7: Addition.java
  2: // Addition program that displays the sum of two numbers.
  3: import java.util.Scanner; // program uses class Scanner
  4: 
  5: public class Addition 
  6: {
  7:    // main method begins execution of Java application
  8:    public static void main( String args[] )
  9:    {
 10:       // create Scanner to obtain input from command window
 11:       Scanner input = new Scanner( System.in );
 12: 
 13:       int number1; // first number to add
 14:       int number2; // second number to add
 15:       int sum; // sum of number1 and number2
 16: 
 17:       System.out.print( "Enter first integer: " ); // prompt 
 18:       number1 = input.nextInt(); // read first number from user
 19: 
 20:       System.out.print( "Enter second integer: " ); // prompt 
 21:       number2 = input.nextInt(); // read second number from user
 22: 
 23:       sum = number1 + number2; // add numbers
 24: 
 25:       System.out.printf( "Sum is %d\n", sum ); // display sum
 26: 
 27:    } // end method main
 28: 
 29: } // end class Addition
 30: 
 31: /**************************************************************************
 32:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 33:  * Pearson Education, Inc. All Rights Reserved.                           *
 34:  *                                                                        *
 35:  * DISCLAIMER: The authors and publisher of this book have used their     *
 36:  * best efforts in preparing the book. These efforts include the          *
 37:  * development, research, and testing of the theories and programs        *
 38:  * to determine their effectiveness. The authors and publisher make       *
 39:  * no warranty of any kind, expressed or implied, with regard to these    *
 40:  * programs or to the documentation contained in these books. The authors *
 41:  * and publisher shall not be liable in any event for incidental or       *
 42:  * consequential damages in connection with, or arising out of, the       *
 43:  * furnishing, performance, or use of these programs.                     *
 44:  *************************************************************************/