Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 5 ■ a Java primer: introduCtion to Java ConCepts and prinCiples

of a class (inside the curly {} brackets) are the data fields (variables). These variables will hold the states,
or characteristics, of your Car object. In this case, you will have six data fields, which will define the car’s
current gear, current speed, current direction, fuel type, color, and drivetrain (two-wheel or four-wheel
drive), as specified earlier for this Car object. So, with six variables from Figure 5-3 in place, a Car class
definition will initially look something like this:


class Car {
int speed = 15;
int gear = 1;
int drivetrain = 4;
String direction = "N";
String color = "Red";
String fuel = "Gas";
}


Notice how the example spaces out the curly braces: { } on their own lines, as well as indenting certain
lines. This is done as a Java programming convention so that you can visualize the organization of the code
constructs that are contained within your Java class structure inside of those curly braces more easily and
more clearly, analogous to a “bird’s eye view” of your Java 9 code construct.
Since we specified a starting value using the equals sign for all of these variables, remember that these
variables will all contain this default or starting data value. These initial data values will be set (in the system
memory) as the Car object’s default values at construction since these are set as your class’s “starting”
variable data values.
The next part of your Java class definition file will contain your methods. Java methods will define how
your Car object will function, that is, how it will “operate” on the variables that you defined at the top of
the class that hold the Car object’s current “state of operation.” Method “calls” will invoke the variable state
changes, and methods can also “return” data values to the entity that “calls” or “invokes” the method, such
as data values that have been successfully changed or even the result of an equation.
For instance, there should be a method to allow you to shift gears by setting the object’s gear variable or
attribute to a different value. This method would be declared as void since it performs a function but does
not return any. In this Car class and Car object definition example, we will have four methods, as defined in
Figure 5-3.
The .shiftGears() method will set the Car object’s gear attribute to the newGear value that was passed
into the .shiftGears() method. You should allow an integer to be passed into this method to allow “user
error,” just as you would have when you are driving your car in the real world when a user might accidentally
shift from first to fourth gear.


void shiftGears (int newGear) {
gear = newGear;
}


The .accelerateSpeed() method takes your object speed state variable and then adds your
acceleration factor to that speed variable, which will cause your object to accelerate. This is done by taking
your object’s current speed setting, or state, and adding an acceleration factor to it and then setting the result
of this addition operation back into the original speed variable so that the object’s speed state now contains
the new (accelerated) speed value.


void accelerateSpeed (int acceleration) {
speed = speed + acceleration;
}

Free download pdf