Basic Calculations

In the same way we can mix and match Strings variables and String literals, we can mix and match number variables and number literals, for example, each of the following additions are valid:

int a = 4 + 3; //a is 7
int b = a + 7; //b is 14
int c = a + b; //c is 21

As well as the plus operator, we can also use the symbols for subtraction -, multiplication *, and division /. We should remember that working with integers will result in an integer as the result - see the example below:

int result = 9 / 4; // result is 2, not 2.25

The remainder

Sometimes it is useful to find the remainder of the division of an integer. In this case we use the modulo operator %, for example

int sweetsInPacket = 23;
int noOfChildren = 3;
int sweetsPerChild = sweetsInPacket / noOfChildren; // result is 7
int sweetsLeftForDad = sweetsInPacket % noOfChildren; //result is 2

Caveats with numbers

Integer types

If a calculation causes a integer or long value to go higher than the maximum permissible value, then the number will 'overflow', this means if we are have a integer with a value of 2147483647 (its maximum value) and add 1 to it, its value would become –2147483648 (its lowest value). If we were to add 4, it would be –2147483645 - for example:

int a = 2147483647;
a = a + 1; //a is now -2147483648

Currency

Because calculations in floating point numbers do not use (or emulate) the base 10 numbering system humans use, calculations involving decimal numbers can result in unwanted side-effects for calculations involving currency. Consider the following example:

double x = 4.4;
double y = 6.6;
double z = y/x;
One would consider z should equal 1.5, however if we output the value we would see it as 1.4999999999999998

One solution for calculations using currency is to work with the integer (or long) data type, but work with values in pence instead of pounds (or the smallest unit in other currencies).

Values that do not change

Sometimes we may create values that should not, under any circumstances, be changed later in the code, examples of such values might include Pi, or the number of minutes in an hour. To indicate that a value may not be changed, we simply add the keyword final before the variable type as follows:

final double pi = 3.14159265359

Task - integer calculations

  1. Using a new command line project as a starting point, Creating a variable to store the number of days in a year as follows:
    int daysInAYear = 365;
  2. Create and set values for other variables to store the following values:
    • Days in a week
    • Hours in a day
    • Minutes in an hour
    • Seconds in a minute
    • Milliseconds in a second (there are 1000)
    These 5 values should remain the only literals in your code for the rest of the task (i.e. do not type any more numbers)
  3. Create variables (calculated using the values declared above) for the following values:
    • Seconds in an hour
    • Seconds in a year
  4. For each of the variables above output (print) them and verify they are as follows:
    • Seconds in an hour: 3600
    • Seconds in a year: 31536000
  5. Calculate the number of whole weeks in a year (using existing variables), and verify the answer is 52
  6. Change the value for the variable containing the number of days in a week to 5, and verify the total weeks in the year is now 73
  7. Change the number of days in a week back to 7
  8. Without adding any new integer literals to the code, calculate the number of hours in a year, minus one week, and verify the answer is 8592
  9. Using the 'long' data type, create a new variable to calculate the number of milliseconds in a year (using the existing variables), and output it. This should be 1000 times the value you got for seconds in a year, e.g. 31536000000, but it may not be depending on how you have written your code
  10. If you've discovered that the calculation for the number of milliseconds in a year does not yield the correct answer, then change the data type for the variable holding the number of milliseconds in a second to use 'long', and try again - verify the answer is correct.

Mixing number types in calculations

You've already seen that changing the type of data used in calculations can have an effect on the output - unless we specified that 'milliseconds in a seconds' variable was 'long' the calculation took place in the limited range of an integer, and the calculation went wrong when its maximum value was exceeded.

Java (unlike some languages) allows us to mix numeric data types when doing calculations. Arguably this makes in simpler to perform calculations, but as we've already seen can allow errors to be introduced.

It is not enough, therefore to specify the data type we want for the result of a calculation, we must consider the types of values used in the calculation.

Task - mixing number types

  1. For each of the lines of code, verify what the result is. Beware, one line of code will result in an error.
    int resultA = 9 / 4;
    int resultB = 9.0 / 4.0;
    double resultC = 9 / 4;
    double resultD = 9.0 / 4.0;
    double resultE = 9.0 / 4;
    double resultF = 9 / 4.0;
  2. For each line of code above, consider the reason for the result being as it is.