Pro Java 9 Games Development Leveraging the JavaFX APIs

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

you have learned about during this chapter. In this section I will give you an idea as to how this can be done,
and I think you will find it very interesting if you are new to Java 9.
One way to think about Java objects is like they are nouns, that is, things (objects) that exist in and of
themselves! The object behaviors, created using methods, are like verbs, that is, things that the nouns can do.
As an example, let’s consider that very popular object in all of our lives: the car. We could very well add this
car to our board game as one of the GamePieces or as another component of the board game altogether.
Let’s define the Car object attributes next. Some characteristics, or attributes, that do not change and
are held in constants could be defined as follows:


•    Color (Candy Apple Red)

•    Engine type (gas, diesel, hydrogen, propane, or electric)

•    Drivetrain type (2WD or 4WD)

Some states that change, define the car in real time, and are held in variables could be defined as follows:

•    Direction (N, S, E, or W)

•    Speed (15 miles per hour)

•    Gear setting (1, 2, 3, 4, or 5)

The following are some things that a car should be able do, that is, the car’s behaviors, defined as methods:

•    Accelerate

•    Shift gears

•    Apply the brake

•    Turn the wheels

•    Turn on the stereo

•    Use the headlights

•    Use the turn signals

You get the idea. Now stop daydreaming about your new GamePiece, and let’s get back down to
learning about Objects!
Figure 5-3 shows the Java object structure, using this car as an example. It shows the characteristics, or
attributes, of the car that are central to defining the Car object and the behaviors that can be used with the
Car object.

Free download pdf