next up previous contents
Next: 2 Input, mathematical functions Up: 1 Programming Basics Previous: Simple output   Contents

Arithmetic operators

Now that you are able to initialise6 and output data, its time to do something with it in between. For numbers or numerical variables there are the operators +, -, /, and * which add, subtract (or reverse sign), multiply and divide respectively, just as in mathematics. There is also % which gives the remainder when the first number is divided by the second, e.g. the value of 4 % 3 is 1.

There is also the assignment operator, =, which you used earlier. This does not act in the same way as an equals sign in maths, instead it puts the value which is to the right of the operator into the variable which is to the left. E.g. x = 1 gives x the value 1, x = y gives x the value of y, x = 2*y + 1 gives x the value of 2y + 1, and x = x + 1 adds 1 to the value of x.

Expressions within round brackets are evaluated first, then division, multiplication, addition and subtraction operations in that order -- so you can add brackets where necessary to influence the order in which the expressions are evaluated. E.g. 2 + 4/2.0 returns the value 4 whereas (2 + 4) / 2.0 the value 3. You will also need to use brackets sometimes in situations such as x* (-y). Leaving the brackets out here would not only make the meaning unclear but also cause two operators to be placed next to each other which is not allowed.

Exercise 1.5


(5 marks)



Copy the following source file, Division.java, from the course web site:
http://www.pp.rhul.ac.uk/~george/PH2150/downloads/Division.java

(i)
Add three lines to this source file so that the program also prints out the difference of the two variables i and j, the product of i and j, and the ratio i/j. Run, and record in you lab book the results exactly. Comment on whether these are the answers you would expect?

(ii)
Alter the third line that you added so it now gives i divided by j as a whole number and a remainder. Also print out the value of ((double)i)/j. Again record the exact output of your program in your lab book.



\fbox{\textbf{You do not have to type in this program.}}

/**
 * Name:
 * Date:
 * Exercise: 1.5
 * Purpose:  To demonstrate use of arithmetical operators and
 *           a common problem encountered with integer division.
 */

public class Division
{
    public static void main(String[] args)
    {
        // declare and initialise variables
        int i = 5, j = 3;

        // print the numbers and the sum of i and j to screen   
        System.out.println("The values are: i is " + i +
                           ", j is " + j);
        System.out.println("The sum of these integers is " + (i + j) );
    }
}

What this exercise aims to demonstrate a potential difficulty with dividing one integer by another. If both the numbers involved are integers, an integer result is expected so the answer is rounded down to a whole number -- thus 5/3 gives 1. If using numbers in your program you could instead write 5.0/3 or 5/3.0 to get a decimal answer. The default type for a decimal number in your code is double, and when one number in the division is double, the result is type double as well. Obviously if you are using a variable just adding .0 is not much use, so instead there is a way of converting the value of an int variable into a double before the operation is carried out. This is the (double)i that you have just made use of in exercise 1.5.


next up previous contents
Next: 2 Input, mathematical functions Up: 1 Programming Basics Previous: Simple output   Contents
RHUL Dept. of Physics