Tuesday, March 12, 2013

Java Math basics

Maths Operators

The maths operators in the figure below, probably look familiar.

The basic set of Java arithmetic operators is presented in the figure.

  • Multiplication

  • Division

  • Modulo

  • Addition

  • Subtraction

The operators have been split into two groups to indicate their ‘precedence’ - the order in which they are obeyed.

 

Order of precedence

Multiplication, division and modulo operations are carried out before addition and subtraction.

If a calculation contains operators of the same precedence, the calculation is performed from left to right.

int j;
    int n = 2;
    j = n + 5; // Set to to 7
    j = n * 2; // Set to 4
    j = 3 + 3 * 2; // Set to 9
    j = n * ( 5 + 2 ) * 2 // Set to 28
    j = 25 - 3 // Set to 22

We can use parenthesis (brackets) to group calculations and force them to be carried out in a particular order.

Wherever brackets are used, the items within them are calculated first.

For instance, the sum 10 + 20 multiplied by 100 produces the result 3000. It could be tempting to write that equation as follows:

     10 + 20 * 100

However, due to the order of precedence in which arithmetic operations are performed, the sum is performed as if it had been written 10 + ( 20 * 100 ) producing the result 2010.

Note: Spaces are only required on either side of an operator for easy readability.

Integer Division

The Integer data type is a whole-number numeric data type. Where integer division is being performed, only the integer part of the result is returned.

For instance, if you type 13 / 5 into a calculator, you will see the result is 2.6.
However, when you perform integer division in a programming language, such as Java, the result of the same calculation is 2 - only the integer (whole number) part is returned as the result.

Double Division

The double data type is a floating point numeric data type..

Where double division is performed, the result is returned as a floating point number. Once again, using the example of 15 / 5, the result returned from double division is 2.6.

The % operator (modulo)

This operator returns the remainder of division of one number by another number.

int examples

For instance, 10 / 2 has a remainder of 0. This is because 10 is divided by 2 exactly.

Another example, 15 / 2 has a remainder of 1. This is because, after division has taken place (15 can be divided 7 times), 1 is left over.

More examples:

    int x;
    x = 12 % 4; // Set to 0
    x = 12 % 3; // Set to 0
    x = 12 % 5; // Set to 2
    x = 50 % 7; // Set to 1
    x = 100 % 33; // Set to 1
    x = 100 % 5; // Set to 0
    x = 30 % 9; // Set to 3

Remember, the remainder of an integer division only returns the integer part of the result.
 
For instance, if you were to type 4 / 3 into a calculator, you would see the result is 1.3333333 etc. However, where this calculation is being performed as integer division in a programming language, such as java, only the integer part is returned - producing the result 1.

Using % to convert cents into dollars

Let’s say we want to convert an int amount of cents (577) into dollars and cents.
The algorithm is as follows:

inputs - numOfCents
outputs - numOfDollars, centsRemaining
processing tasks -
    • get numOfCents
    • numOfDollars = numOfCents / 100
    • centsRemaining = numOfCents % 100


Remember, integer division returns the integer part of the result, so the statement

    dollars = numOfCents / 100

will return 5, in this example.

Monday, March 4, 2013

Overriding Methods

Method overriding allows us to provide our own implementation of a method in a child class.

"Overriding and Hiding Methods (The Java™ Tutorials > Interfaces and Inheritance)" ( http://bit.ly/Zn8eop )

"Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)" ( http://bit.ly/PYQDIV )