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 />

To code the for loop to move the InvinciBagel 40 pixels, using a while loop structure, looks like this:<br />

int x = 0;<br />

while(x < 40) {<br />

invinciBagelX++;<br />

invinciBagelY++;<br />

x++<br />

}<br />

The only difference between a do-while loop and a while loop is that, with the latter, the loop logic programming<br />

statements are performed before, instead of after, the evaluation. Thus, using a do-while loop programming<br />

structure, the previous example would be written as follows:<br />

int x = 0;<br />

do {<br />

invinciBagelX++;<br />

invinciBagelY++;<br />

x++<br />

} while(x < 40);<br />

As you can see, the <strong>Java</strong> programming logic structure is inside curly braces, following the <strong>Java</strong> do keyword,<br />

with the while statement after the closing brace. Note that the while evaluation statement (and therefore the entire<br />

construct) must be terminated with a semicolon.<br />

If you want to make sure that the programming logic inside the while loop structure is performed at least one<br />

time, use the do-while, as the evaluation is performed after the loop logic is executed. If you want to make sure that<br />

the logic inside the loop is only executed after (whenever) the evaluation is successful, which is the safer way to code<br />

things, use the while loop structure.<br />

<strong>Java</strong> Objects: Virtual Reality, Using <strong>Java</strong> Constructs<br />

I saved the best, <strong>Java</strong> objects, for last, because they can be constructed in one fashion or another using all the concepts<br />

that I have covered thus far in the chapter and because they are the foundation of OOP language (in this case, <strong>Java</strong> 8).<br />

The fact is, everything in the <strong>Java</strong> 8 programming language is based on <strong>Java</strong>’s Object superclass (I like to call it the<br />

masterclass), which is in the java.lang package, so an import statement for it would reference java.lang.Object, the full<br />

pathname for the <strong>Java</strong> Object class.<br />

<strong>Java</strong> objects are used to “virtualize” reality by allowing the objects you see all around you in everyday life, or,<br />

in the case of your game, objects you are creating out from your imagination, to be realistically simulated. This is<br />

done by using the data fields (variables and constants) and the methods that you have been learning about in this<br />

chapter. These <strong>Java</strong> programming constructs will make up the object characteristics, or attributes (constants); states<br />

(variables); and behaviors (methods). The <strong>Java</strong> class construct organizes each object definition (constants, variables,<br />

and methods) and gives birth to an instance of that object, using the constructor method for the class that designs and<br />

defines the object via the various <strong>Java</strong> keywords and constructs.<br />

Creating an InvinciBagel Object: Attributes, States, and Behavior<br />

Let’s put together an example of an InvinciBagel object that shows how constants define characteristics, variables<br />

define states, and methods define behaviors. We will do this using <strong>Java</strong> coding constructs that you have learned about<br />

thus far in the chapter, including constants, variables, and methods that you have already defined, to some extent.<br />

www.it-ebooks.info<br />

69

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

Saved successfully!

Ooh no, something went wrong!