Program Logic

Unit 3 Index

Multiplicative Operators

Multiplicative Operators

Multiplication and division are typical mathematical operations used in everyday life...and used every day in programming. The modulo operator is also a multiplicative operator, however, we have a separate chapter on modulo.

The Multiplicative Operators

In math, multiplicative refers to both multiplying and dividing. In programming, the symbols used for multiplicative operations differ from those used in math.

Multiplying

Do not worry, validating and converting a String variable value to a number value is easy with some built-in help from the programming language. We will go into that in detail later.

Remember, we have naming standards for our variables. My examples do not always follow those standards for the sake of clarity. However, you should always follow the Northwood Tech Software Development Standards. Please carefully review the code below. In these examples, the plus sign is concatenating a String literal with a variable.

// Multiplying numbers

// Declare and initialize variables
int numberOne = 25;
int numberTwo = 100;
int multiplyResult = 0;

// Multiplying literal numbers directly
System.out.print("You can multiply number literals: ");
System.out.println(75 * 20);

// Multiplying number variables
multiplyResult = numberOne * numberTwo;
System.out.println("The result of multiplying " + numberOne + " times "
        + numberTwo + " is: " + multiplyResult);

Below are the results that would be printed to the console window based on the code snippet above. Please try to match the results with the code.


Dividing in Java (IMPORTANT! Read Carefully)

Java does something called Integer division. As you know, an int can only hold a whole number, so if there is a remainder after dividing, it is just dropped; no rounding. In order to maintain precision, when dividing in Java, at least one of your numbers should be a decimal number literal or a double data type. I recommend you view the YouTube video int and double Division (Java Tutorial) by Bill Barnum (2:47) for a deeper dive into dividing in Java.

Please carefully review the code below.

// Declare and initialize variables
int numberFour = 250;
double numberFive = 7.0;

double divisionResult = 0.0;

// Dividing literal numbers directly
System.out.println("You can divide number literals: ");
System.out.println(75/34.0);

// Dividing number variables
divisionResult = numberFour / numberFive;
System.out.println("The result of " + numberFour + " divided by "
        + numberFive + " is: " + divisionResult);

Below are the results that would be printed to the console window based on the code snippet above. Please try to match the results with the code.

In reality, we do not use literal numbers that often in programming. Typically, you get the value from an external source such as end-user input.

Combining Operators

I am sure you realize that just like math, you can have a combination of multiplying, dividing, adding, and subtracting in the same statement. You should use parentheses for your needed order of operations. Java does the calculations inside parentheses first.

What you Learned

What's next?

The next chapter in Math Operators is Operator Precedence.