Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


Coding your for loop that moves the GamePiece 40 pixels, using a while loop structure, would look
like this:


int x = 0;
while(x < 40) {
invinciBagelX++;
invinciBagelY++;
x++;
}


The only difference between a do-while loop and a while loop is that in a do-while loop the loop logic
programming statements are performed before the evaluation, instead of after the evaluation, as they are in
the while loop. Thus, the previous example would be written with a do-while loop programming structure
that has a Java programming logic structure inside of curly braces, after the Java do keyword, with the while
statement after the closing brace, coded as follows:


int x = 0;
do {
invinciBagelX++;
invinciBagelY++;
x++;
}
while(x < 40);


You should also take notice that for the do {...} while(...); construct, the while evaluation statement
(and thus the entire do-while programming construct) needs to be finished with a semicolon, whereas the
while(...){...} structure does not.
If you wanted to make sure that the programming logic inside of the while loop structure is at the very
least performed one time, use the do-while, as the evaluation is performed after the loop logic is executed.
If you wanted to make sure that the logic inside of the loop is executed only after, or whenever, the evaluation
is successful (which is the safer way to code things), use the while loop structure.


Java Objects: Virtualizing Reality Using OOP in Java


The reason I saved the best for last, Java Objects, is because they can be constructed in one fashion or
another using all of the concepts that I have covered thus far in the chapter and because they are the
foundation of the Object-Oriented Programming (OOP) language, in this case, Java 7, 8, and 9. Everything in
the Java programming language is based on the Java language’s Object superclass (I like to call it the master
class), which is in the java.lang package, so an import statement for it would reference java.lang.Object,
which is the full pathname to the Java Object class. All other Java classes are created, or rather, subclassed
using this class because everything in Java is an Object.
Note that your Java compiler automatically imports this java.lang package for you! Java objects are
used to “virtualize” reality by allowing objects you see around you in everyday life (or, in the case of your
game, objects that you are creating out of your imagination) to be realistically simulated. This is done
by using data fields (variables and constants) and the methods that you’ve been learning about during
this chapter. These Java programming constructs will make up the object characteristics or attributes
(constants), states (variables), and behaviors (methods).
The Java class construct will organize each object definition (constants, variables, and methods) and
will give birth to an instance of that object. It does this by using the constructor method for the class, which
designs and defines your object, and by using the various Java keywords and programming constructs that

Free download pdf