Saturday, 12 December 2009

Java Introduction Part 3 - Arithmetic Operations

This is the third part on the Java Introduction posts. In this post I would like to discuss about the arithmetic operators that the Java language supports. Even though the theory behind them is simple, it is vital and basic knowledge that needs to be acquired in order to write programs in Java

These arithmetic operators are summarized below:
  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Remainder
Let's take each on of the operators and present an example on how to use them in a program.

In order to add two integers in Java, we use the plus sign (+). Assuming that we have declared two integer variables as number1 and number2 respectively, the addition can be defined as follows:

sum = number1 + number2; // add two numbers

The subtraction in Java uses the same principle as the addition. The symbol used for subtraction is the minus sign (-). Re-using the two integer variables from the previous example, the subtraction can be defined as follows:

subtraction = number1 - number2; // subtract two numbers

For the multiplication, we use the asterisk sign (*). Again using the variables from the examples above, the multiplication can be defined as follows:

product = number1 * number2; // multiply two numbers

For the division, we use the forward slash sign (/). Again using the variables from the examples above, the division can be defined as follows:


fraction = number1 / number2; // divide two numbers

* Please note that if the division takes part between two integers, the result yields an integer quotient. So, for example, 17 / 5 evalutes to 3. Any fractional part in integer division is simply discarded - no rounding occurs.

For this reason, Java provides the remainder operator (%) which yields the remainder after the division.  Again using the variables from the examples above, the remainder can be defined as follows:


remainder = number1 % number2; // remainder of a division between two numbers

* In this, 17 / 5 evaluates to 2 (since 5 can be multiplied by 3 which evaluates to 15. To get to 17 we need 2 which is the remainder).



An example of putting all of these arithmetic operators into a simple program can be found below:

// ArithmeticOperators.java
/* Simple Java program that illustrates the use of the arithmetic operators that the Java language supports */

 public class ArithmeticOperators
 {
     // main method where the execution of a Java application begins
     public static void main (String args[])
     {
        int number1 = 17; /* Declare an integer variable number1 and assign the number 17 to it */
        int number2 = 5; /* Declare an integer variable number2 and assign the number 5 to it */
       
        // Addition
        /* Declare an integer variable sum and assign the sum of number1 and number2 to it. The value assigned to the variable sum is the number 22. */
        int sum = number1 + number2;
        System.out.println("The sum is equal to " +  sum); /* Print out the sum */
       
        // Subtraction
        /* Declare an integer variable difference and assign the difference of number1 and number2 to it. The value assigned to the variable sum is the number 12. */
        int difference = number1 - number2;
        System.out.println("The difference is equal to " +  difference); /* Print out the difference */
       
        // Multiplication
        /* Declare an integer variable product and assign the product of number1 and number2 to it. The value assigned to the variable product is the number 85. */
        int product = number1 * number2;
        System.out.println("The product is equal to " +  product); /* Print out the product */
       
        // Division
        /* Declare an integer variable fraction and assign the fraction of number1 and number2 to it. The value assigned to the variable fraction is the number 3. */
        int fraction = number1 / number2;
        System.out.println("The fraction is equal to " +  fraction); /* Print out the fraction */
       
        // Remainder
        /* Declare an integer variable remainder and assign the remainder of number1 and number2 to it. The value assigned to the variable remainder is the number 3. */
        int remainder = number1 / number2;
        System.out.println("The remainder is equal to " +  remainder); /* Print out the remainder */
       
     } // end of main method
} // end class ArithmeticOperators

No comments:

Post a Comment