Method – Add Two numbers

This tutorial uses the same code as Add Two Numbers but includes a function/method to add two numbers together and return a value. In this case the value is a integer (int) and that is why the int is just before the name of the method (addIntegers) with two parameters passed to the method, these are the values that are being added together. I have called them a and b, so that they are different names to the variables within the main method.

The source code

// just import the BufferedReader and inputstream reader
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
class addtwonumbers_function
{
       // private = local to this class
       private static int addIntegers(int a, int b)
       {
              return (a+b);
       }
 
       public static void main(String[] args)
       {
              // system.in reader (e.g. the input from the console)
              InputStreamReader ISR = new InputStreamReader(System.in);       
              // buffer the console reader
              BufferedReader BR = new BufferedReader(ISR);                     
 
              // the default values for the two numbers
              int val1 = 0;
              int val2 = 0;
              try 
              {
                     // output the question.
                     System.out.print("Enter first number : ");
                     // read in the console intput one line (BR.readLine) and then convert to a integer
                     val1 = Integer.parseInt(BR.readLine());
                     System.out.print("Enter second number : ");
                     val2 = Integer.parseInt(BR.readLine());
              }
              catch (Exception ex)
              {
                     // if the input was a string.
                     System.out.println(ex.toString());
              }
              // output the answer of adding both of the values together
              System.out.println("Answer = " + addIntegers(val1, val2));
       }
}

save as addtwonumbers_function.java, this program will function the same as the previous tutorial apart from the inner working will call the method. The method programming allows for one method to be called multiple times, e.g. Within the example above there could be a method that was 5 lines in length and instead of writing the same 5 lines each time, you just write a method that is called.

5 thoughts on “Method – Add Two numbers”

  1. hi am ushan.. new to java.. can u say me some tips to upgrade my java knowledge.. it will helpful to me.

  2. I think that if you want to find out some more java knowledge, then if you look around this site I am planning on doing some more java tutorials soon as well :). so hopefully they will help you further .. what sort of examples/tutorials was you looking for ?

  3. Problem Description
    Please write a program to add two integers.

    Input/Output Specifications
    Input will consist of two numbers separated by a space character on a single line and output should just contain a number followed by a newline.

    Sample Input/Output
    Input1:
    -5 5
    Output1:
    0 Input2:
    5 5
    Output2:
    10 Input3:
    9 8
    Output3:
    17

  4. Simplest program to add two numbers:-

    public class AadNum{
     
         public static void main (String args[]){
     
         int a,b,c;
     
         a = 10;
         b = 20;
         c = a+b;
     
         System.out.println(""+c);
     
       }
     
    }

    OUTPUT :-
    30

Leave a Reply

Your email address will not be published. Required fields are marked *