156 CHAPTER 5: Introduction to Java: Objects, Methods, Classes, and Interfaces
Creating Your HelloUniverse Class: Galaxy
Now that we have gone over the main Java constructs and concepts, let’s get some real-world
experience with Java and code a Galaxy.java class for our Android HelloUniverse application.
We will use our Hello Universe app as an example of how to create a functional Java class from
scratch, and create a class that will define and manage our Galaxy objects, so that you can see
these concepts in action! This Galaxy.java class will also be something that we can utilize and build
upon as we progress throughout the book, so it is the perfect example.
Defining a Galaxy Class: Variables and Methods
First, let’s define some of the attributes or characteristics of our Hello Universe app’s Galaxy class
(and thus of the Galaxy objects that will be created using this Galaxy class’s constructor method) as
shown in Table 5-2.
Table 5-2. Galaxy Class Data Fields (Variables) Along with Their Data Type Specifier Values
Class Variable Name Data Type Specifier Used, and Information Held in the Variable
Galaxy Name String data type containing a text value, such as “Milky Way”
Galaxy SolarSystems An integer value representing the total number of solar systems
Galaxy Planets An integer value representing total number of habitable planets
Galaxy Colonies An long data value representing the total number of colonies
Galaxy Population A double value representing total number of sentient life forms
Galaxy Fleets An integer value representing a total number of starship fleets
Galaxy Starships An integer value representing the total number of starships
As you can see, our Galaxy object(s) will need to have a name, number of solar systems, number of
habitable planets, a number of habitable colonies, a population of life forms, a number of starship
fleets, and a number of starships that are in that Galaxy object. This Galaxy object will help keep
track of our “high-level” Galaxy information for each Galaxy object that we add to our Hello Universe
simulation.
Next, let’s define our Galaxy object behaviors, or functions, that can be attributed to our Galaxy
objects, such as “getting” and “setting” the number of colonies in the galaxy, the total life form
population, the total number of starship fleets, and the total number of starships in the galaxy, as
seen in Table 5-3.