28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3 ■ A <strong>Java</strong> 8 Primer: An Introduction to <strong>Java</strong> 8 Concepts and Principles<br />

<strong>Java</strong> Arithmetic Operators<br />

The <strong>Java</strong> arithmetic operators are the most commonly used in programming, especially in arcade type games, in<br />

which things are moving on the screen by a discrete number of pixels. Many more complex equations can be created<br />

using these basic arithmetic operators, as you have already learned in math class, from primary school through college.<br />

The only arithmetic operators shown in Table 3-2 that you may not be that familiar with are the modulus<br />

operator, which will return the remainder (what is left over) after a divide operation is completed, and the increment<br />

and decrement operators, which add or subtract 1, respectively, from a value. These are used to implement your<br />

counter logic. Counters (using increment and decrement operators) were originally used for loops, (which I will be<br />

covering in the next section); however, these increment and decrement operators are also extremely useful for game<br />

programming (point scoring, life span loss, game piece movement, and similar progressions).<br />

Table 3-2. <strong>Java</strong> Arithmetic Operators, Their Operation Type, and a Description of the Arithmetic Operation<br />

Operator Operation Description<br />

Plus + Addition Adds the operands on either side of the operator<br />

Minus – Subtraction Subtracts the right-hand operand from the left-hand operand<br />

Multiply * Multiplication Multiplies the operands on either side of the operator<br />

Divide / Division Divides the left-hand operand by the right-hand operand<br />

Modulus % Remainder Divides the left-hand operand by the right hand-operand, returning remainder<br />

Increment ++ Add 1 Increases the value of the operand by 1<br />

Decrement -- Subtract 1 Decreases the value of the operand by 1<br />

To implement the arithmetic operators, place the data field (variable) that you want to receive the results of the<br />

arithmetic operation on the left side of the equals assignment operator and the variables that you want to perform<br />

arithmetic operations on the right side of the equals sign. Here is an example of adding an x and a y variable and<br />

assigning the result to a z variable:<br />

Z = X + Y;<br />

// Using an Addition Operator<br />

If you want to subtract y from x, you use a minus sign rather than a plus sign; if you want to multiply the x and y<br />

values, you use an asterisk rather than a plus sign; and if you want to divide x by y, you use a forward slash instead of<br />

a plus sign. Here is how those operations look:<br />

Z = X - Y; // Subtraction Operator<br />

Z = X * Y; // Multiplication Operator<br />

Z = X / Y; // Division Operator<br />

You will be using these arithmetic operators quite a bit, so you will get some great practice with these before you<br />

are finished with this book! Let’s take a closer look at relational operators next, as sometimes you will want to compare<br />

values rather than calculate them.<br />

62<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!