Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples


logic; the assignment operators, which do the arithmetic operations and assign the value to another variable
in one compact operation (operator); and the conditional operator, also known as a ternary operator, which
assigns a value to a variable based upon the outcome of a true or false (boolean) evaluation.
There are also the conceptually more advanced bitwise operators, used to perform operations at the
binary data (zeroes and ones) level, the application of which is beyond the scope of the book. The use
of binary data is not as common in JavaFX game programming as these other more mainstream types of
operators, each of which you will be using during this book to accomplish various programming objectives
in your pro Java games and IoT application logic.


Java Arithmetic Operators: Basic Mathematics


The Java arithmetic operators are the most commonly used operators in pro Java game programming,
especially in dynamic action-type games, where things are moving on the screen by a precise, highly
controlled number of pixels. Don’t underestimate simple arithmetic operators, as in the framework of a OOP
language. Far more complex mathematical equations can be created using Java structures, such as methods,
that leverage these basic arithmetic operators using the other powerful tools that Java offers, which we are
reviewing (learning about) during this chapter.
The only arithmetic operators, shown in Table 5-2, that you might not be that familiar with are the
Modulus operator, which will return the remainder (what is left over) after a divide operation is completed;
and the Increment or Decrement operator, which adds or subtracts one, respectively, from a value. These
operators are sometimes used to implement your counter logic. Counters (using increment and decrement
operators) were originally used for loops, which we will be covering in the next section; however, increment
and decrement operators are also extremely useful in game design as well, for point scoring, life-span loss,
game piece movement, and similar linear numeric progressions.


Table 5-2. Java Arithmetic Operators, Their Operation Type, and a Description of That Arithmetic Operation


Operator Operation Description


Plus + Addition Operation adds the operands on either side of the operator


Minus - Subtraction Operation subtracts the right operand from the left operand


Multiply * Multiplication Operation multiplies the operands on both sides of the operator


Divide / Division Operation divides the left operand by the right operand


Modulus % Remainder Operation divides the left operand by the right operand, returning
the remainder


Increment ++ Adding One Increment operation will increase the value of the operand by one


Decrement -- Subtract One Decrement operation will decrease the value of the operand by one


To implement the arithmetic operators, place the data field (variable) that you want to receive the
results of the arithmetic operation on the left side of your equals assignment operator (we will cover
assignment operators during this section of the chapter as well) and the variables that you want to perform
arithmetic operations on the right side of the equals sign. Here’s an example of adding an X and a Y variable
and assigning the result to a z variable:


Z = X + Y; // Using the Addition Operator

Free download pdf