Arithmetic operations  

  • The number data types in programming languages (always) support arithmetic operations

  • The classic (traditional) arithmetic operations are:

    • Addition (denoted by the symbol +)

    • Subtraction (denoted by the symbol -)

    • Multiplication (denoted by the symbol *)

    • Division (denoted by the symbol /)

  • Integer division often support the remainder operation:

    • Integer remainder of a division (denoted by the symbol %)

Arithmetic operations on float and double data   

float and double data types in Java support 4 arithmetic operations: +, -, *, /

public class FloatArithm
{
    public static void main(String[] args)
    {
        double a, b, c;
        
        a = 4.2;    // Put breakpoint here to demo
        b = 2.0;
        
        c = a + b;   // 4.2 + 2.0 = 6.2
        c = a - b;   // 4.2 - 2.0 = 2.2
        c = a * b;   // 4.2 * 2.0 = 8.4
        c = a / b;   // 4.2 / 2.0 = 2.1
    }
}
  

DEMO: demo/02-elem-prog/06-arith-ops/FloatArithm.java

Arithmetic operations on int, short, byte and long data   

int, short, byte and long data types has 5 arithmetic operations: +, -, *, /, %

public class IntegerArithm
{
    public static void main(String[] args)
    {
        int a, b, c;
        
        a = 7;       // Put breakpoint here to demo
        b = 3;
        
        c = a + b;     // 7 + 3 = 10
        c = a - b;     // 7 - 3 = 4
        c = a * b;     // 7 * 3 = 21
        c = a / b;     // 7 / 3 = 2 (computes the quotient  of 7/3)
        c = a % b;     // 7 % 3 = 1 (computes the remainder of 7/3)
    }
}
  

DEMO: demo/02-elem-prog/06-arith-ops/IntegerArithm.java

  Operation precedence  

  • Just like what you have learned in Elementary School, Java imposes the following precedence on arithmetic operations:

    1. First perform multiplications/divisions operations (i.e.: *, /, %)

    2. Then perform additions/subtractions operations (i.e.: +, -)

    3. Perform arithmetic operations of equal priority from left right


  • Examples: what is stored in the variable x:

       int x;
    
       x = 1 * 2 + 3;
       x = 1 + 2 * 3;
       x = 4 - 3 + 2;
       x = 5 % 3 * 2;
       x = 5 / 3 * 2;   // Tricky!
    

DEMO: demo/02-elem-prog/06-arith-ops/OperPrio.java

  Programming trick: use % to check if number of even/odd  

  • How to test for divisibility:

    • A number x is divisible by k if:

        The remainder of  x/k  is equal to  0 
      

  • Even numbers are divisible by 2

    Example:

        public static void main(String[] args)
        {
            int a = 6;
            int b;
            
            b = a % 2;     // If a even --> b = 0
                           // If a odd  --> b = 1 
        }

DEMO: demo/02-elem-prog/06-arith-ops/OddEven.java

Common use of %:    convert length in inchesfeet + inches

A common use of the remainder % operation is to convert length in # inches to # feet + # inches:

public class InchesToFeetPlusInches
{
    public static void main(String[] args)
    {
        int lengthInInches = 58;
        int feet, inches;
        
        feet = lengthInInches / 12;     // # feet
        inches = lengthInInches % 12;   // # inches 
    }
}
  

DEMO: demo/02-elem-prog/06-arith-ops/InchesToFeetPlusInches.java

Common use of %:    convert time in secondsminutes + seconds

The remainder % operation can also be used to convert time in # seconds to # minutes + # seconds:

public class SecondsToMS
{
    public static void main(String[] args)
    {
        int timeInSeconds = 76;
        int minutes, seconds;
        
        minutes =  timeInSeconds / 60;    
        seconds =  timeInSeconds % 60;
    }
}
  

DEMO: demo/02-elem-prog/06-arith-ops/SecondsToMS.java

Question:    what if we use timeInSeconds = 3667 --- convert to hours, minutes and seconds

Coverting to larger units:    use % to convert time in seconds to hours + minutes + seconds

"Staged" conversion: (1) # seconds # Hrs + # RemSeconds and then (2) # RemSeconds # min + # sec

public class SecondsToHMS
{
    public static void main(String[] args)
    {
        int timeInSeconds = 3667;
        int hours, minutes, seconds;
	int remainingSeconds;            // Help variable

        // Stage 1       
        hours            = timeInSeconds / 3600;    
        remainingSeconds = timeInSeconds % 3600;

        // Stage 2
        minutes = remainingSeconds / 60;
        seconds = remainingSeconds % 60;  
    }
} 

DEMO: demo/02-elem-prog/06-arith-ops/SecondsToHMS.java

Exercise:    convert cents into dollar, quarters, dimes, nickles and cents (pennies)

 


  I have provide "homeworks" for you to practice the material learned in this course.

  The homeworks can be found at this website:

      http://www.cs.emory.edu/~cheung/OutSchool/apcs/HW/

  This website also contains the answers to the homeworks.
  You should only look at the answer AFTER doing the homework to check you work.



Do HW1: http://www.cs.emory.edu/~cheung/OutSchool/apcs/HW/01-change-cents.html

Exponentiation    Java uses a method inside the Math class

 

import java.lang.Math;       // Optional --- see comment

public class Exponentiation
{
    public static void main(String[] args)
    {
       double a, b, c;

       a = 3.0;
       b = 2.0;

       c = Math.pow( a, b );  // ab
    }
} 

DEMO: demo/02-elem-prog/06-arith-ops/Exponentiation.java

Comment:   the Java compiler will automatically import all classes in java.lang.*

Shorthand operators:    +=, -=, etc

//  x += y    x = x + y
//  x -= y    x = x - y
//  x *= y    x = x * y,  and so on

//  x++       x = x + 1
//  x--       x = x - 1

public class ShortHandOp
{
    public static void main(String[] args)
    {
        int  a, b;
        
        a = 4; 
        b = 2;

        a += b;    // Same as:  a = a + b -- Try: -=, *=, /=

        a++;
       
    }
}  

DEMO: demo/02-elem-prog/06-arith-ops/ShortHandOp.java

Most common: +=, -=, ++ and --