
Module 3.2: Java Objects 2 | Creature Invasion
Our Donut "blueprint" class is just the beginning of our adventure into Object-Oriented Java. This module builds on the essential concepts to explore how we can make two different types of Objects, each with clever methods which allow the passing of entire Objects between classes and methods.
This is our concluding module for this course. The concepts explored in here will be directly applicable to your work on the final Object Project.
prerequisites |
Best to make sure you've completely digested Java Objects Module 3.1, including--most importantly--the readings about Java Objects. |
module contents |
wb_incandescentConcept Review: Object-Oriented Fundamentals |
learning objectives |
|
setup your workspace |
Create a new package called objects2_creatures inside your working NetBeans project. Inside that package, create three new classes as follows: ![]() The skeleton of both Creature and SizedDonut are provided in the sections below. You'll be coding the CreatureLand's main method along with Eric in a video tutorial. |
motorcycleConcept Review: Object-Oriented fundamentals
Video tutorial segment 1: Review of Object concepts
In case you made your Donut-like objects a while back, this video's review of how Objects work in Java may come in handy. With the CIT-111 class at West Hills, Eric diagrams and discusses the core features of the object creation process using the new and pointer/"handle" variables
Object-Oriented fundamentals readings
arrow_upward back up to contents
extensionPreparation Step 1: Building a Creature class
Code up the following skeleton of the Creature class. Please INCLUDE the comments that are in this listing so that you have notes to refer back to as you proceed through the module. We'll be fleshing out the guts of several of the methods in this class later in the module.

Diagramming our Creature class
Your instructor wrote this class skeleton which means the class name was chosen, member variables and their type added, and the method signatures for each action the class can take are written. Return to the code and verify that the methods don't have any code in them yet!
We can still diagram the structure of a class even with incomplete methods. This allows us to communicate to other coders what our object "knows" (mem vars) and "does" (methods). Here's our creature class diagrammed using the Unified Modeling Language format for Object diagrams:

Let NetBeans' class navigator help!
This style of class diagram is so common that you'll see it used by programmers in most Object-Oriented languages. Not only does NetBeans contain extensions that can draw a UML diagram from only your Java code.
While the UML style is great for sharing your ideas with other folks, NetBeans contains an dialog box that shows you the member variables and methods in that particular class. The list is contained in a mini-window called "Navigator". If it isn't sitting in the lower left corner of your NetBeans workspace, click Window >> Navigator to re-activate it. For example, when we view our Creature class in Navigator, we see this:

You can jump to any member variable or method listed by simply clicking the name. This is super handy when the classes you write span hundreds of lines and dozens of methods! Whoa!
arrow_upward back up to contents
motorcyclePreparation Step 2: Refactoring our Donut objects to make SizedDonut creatures.
Building on our work from last module, let's create a SizedDonut class that includes a special member variable called sizeInmm that our Creatures will use to determine if they will eat the donut or not.

We'll work more with this code later in the lesson!
arrow_upward back up to contents
motorcycleCore Concept 1: Dissecting our Creature objects
Video tutorial segment 2:
When we write classes that we will use to create Objects in java, we are designing member variables and methods that do stuff with the data in those member variables. Explore with the West Hills Java class how the Creature class has been designed.
This video is explanation heavy: don't tune out, though! Code along and tinker with Eric as the concepts are explained.
Key points from video tutorial
- Our method eatDonut takes in a reference to a SizedDonut as an input parameter. We must have an instance of a SizedDonut already made before calling this method.
- Objects "know things" with member variables, and "do things", with methods
- We diagram our classes in Java using the Unified Modeling Language (UML). Knowing how to translate class diagrams into Java is a core skill
- We can create member variables that are declared public which means any class that uses our object can read and write this member variable without restriction.
- In practice, member variables in Java are most often declared private and are accessed through public methods (often written in pairs called "getters" (read data from member variable) and "setters" (write data to member variable). This is called encapsulation and is very common in Java.
arrow_upward back up to contents
motorcycleExercise 1: Coding up our CreatureLand's main method
Video tutorial segment 3
Segment 3: We have written two object blueprints: one called Creature and one called SizedDonut. We will use the blueprints to create instances of each class and we will do all of this inside CreatureLand which contains the program's main method.
Code along with the video!
No guts were given for the main method in CreatureLand so we need to write that together. Before jumping into this video tutorial, make sure you have your package and class's setup as described at the top of this module. Have CreatureLand ready roll since you'll code along with the class.
Key points from video tutorial
- When we are writing "blueprint" classes, they aren't stand-alone programs. There is no main method in Creature or SizedDonut. Enter CreatureLand: Our blueprints are converted to Objects with the keyword new.
- Remember: new generates a segment of space in RAM for that particular Object of that particular type. We need a pointer/handle variable to access that Object, which we can name anything we want but which must be typed to line up with the object it points/handles to. To store a SizedDonut, therefore, we need a pointer variable declared to be of type SizeDdonut! This is the power of custom typing which is a feature of all major object-oriented languages today.
- Once we instantiate our objects with new, we can access that variable with the pointer/handle variable and then the small-but-mighty access (.) operator. For example, to give a species to our newly created Creature object pointed to by penguin, we write: penguin.species = "Emperor Penguin.
But it doesn't do anything!
If you run the code in CreatureLand as of the end of this video, you would not see any output in the console window because the guts of our methods inside Creature and SizedDonut have nothing in them! In the next segments, we'll see how we can test the flow of execution through our various methods and, of course, write the actual code that implements the relationships between the classes and the methods.
arrow_upward back up to contents
motorcycleExercise 2: Digesting pointer/handle variables
Video tutorial segment 4: Mastering the essentials of point/handle variables
When we create an object, we must be able to access that chunk of RAM (short-term storage) and we do this through a fancy variable known in this class as a handle variable. Its also called a reference variable or a pointer variable. These are all names for the same concept.
confusing terminology note |
Because many Object-oriented languages (Java, C++, Python, C#) create objects and must then manipulate and reference them in code, the notion of a pointer variable is common across the languages. Each language group, however, uses different terms: this course uses handle variable because it seems to be the most descriptive. The same idea of using a variable to access an Object stored on the Heap in memory is also called a reference variable or a pointer variable. |
This is an exhaustive screen cast which digests each line of CreatureLand demonstrating how central the idea of a handle variable is to the way we program Java as object-oriented coders. Skim through the exercise steps following this tutorial so your brain has an idea of what you'll be doing with the skills reinforced in the screen cast here:
Super mini-project: Tinker with handle variables
program objective |
This is an intermediate step in creating a working model of a Creature eating a SizedDonut. The methods in our blueprints have no guts yet (they are just empty blocks of code). We are making sure we have some more practice with object creation and access before moving on. Write two new methods inside CreatureLand which will accept an object reference to either our Creature objects or SizedDonut and display the value of each of the member variables. |
specification 1 |
Create a method called displayCreatureStats which takes as an input parameter a Creature object and returns nothing. Remember: These methods are not in "blueprint" classes so they STILL HAVE the static modifier. Inside the guts of the method, access each of the object's member variables and send the result to the console with println(). Provide hard-coded text in your calls to println() so that the output to the console is interpretable (i.e. print "name of creature:" before outputting the Creature object's name member variable's value. |
specification 2 |
Create another method called public static void displaySizedDonutStats that functions just like displayCreatureStats except is specific to SizedDonut objects. Each member variable should be sent to the console with nice formatting for easy reading. Remember, you'll need to add the input parameter to this method's name. |
specification 3 |
With two methods ready to roll, you can now call each method from main and pass references to our Creature and SizedDonut objects to the appropriate methods for display of their internal values (using, of course, our handle variables). In other words, we are sending our handle variable penguin down to displayCreatureStats. This method knows how to ask any Creature object passed in for the values of its member variables name, species and biteSizeInPercent. |
tinkering ideas |
Make a bunch of instances of each object type and send them down to each respective method to see how the handle variables actually work. Create a Creature named "Leslie" of type "sloth" who takes really big bites of SizedDonut objects. Make sure you can see all of Leslie's stats by using your new method! |
Mini-project Help: Sample code
The following button will expose working code for a displayCreatureStats method. Note that you should always make your code your own, even if you use an aid.
arrow_upward back up to contents
motorcycleExercise 3: Coding the guts of eatDonut and simulateEating methods
Work along with Eric as we trace how execution flows through our three classes and their methods. We'll also code up the basic guts of our two key methods in our two "blueprint" classes. Be sure to have your classes all set up so you can pause and work through the exercises as they are done in the video.
As of the end of this video, you should have a working program that demonstrates that your Creature objects can, indeed, consume SizedDonut objects. This should be displayed on the console for easy checking!
arrow_upward back up to contents
motorcycleExercise 4: Implementing method logic in Creature and SizedDonut
We'll explore the power of private member variables which are only accessed through public methods. The methods will test their input parameters against some rules in the specification. Only if the input value passes muster will it be written into its own classes precious, private member variable.
The segement will also demonstrate how to provide messages to the user about the checks going on inside the public accessor methods (also called "getter" and "setter" methods).We can provide the user with clues about why their operations may not have been successful so they can try again more intelligently.
pro tip |
Code that helps its user is a Good Thing. |
key term: methods |
arrow_upward back up to contents
motorcycleModule Project: Making picky eaters out of our Creature objects
This is a culminating activity for this module. Half the process is probably just digesting the language used in the specification. Learning to convert English into Java. The video tutorials and the sample GitHub code will be your guide through this.
warning |
This is the trickiest project we've done, so don't be too hard on yourself if you put a few hours into it and come up a little short. Object-oriented manipulations are at the heart of Java, but are also, in many ways, advanced programming concepts. Do your best, share your code, and keep tinkering at your own pace. |
Project specification:
Study this specification document carefully and code up your work to meet its requirements.
program objective |
Write code in our Creature class's eatDonut(SizedDonut donutToEat) method that implements picky eater logic: if a donut is less than a minimum edible size, no "bite" should be taken. |
program specification 1 |
Add a private member variable of an appropriate name that stores our Creature object's minimum SizedDonut preference. Since the member variable is private, the user of our Creature class needs a way to set the Creature object's minimum bite size. This should happen in a method called something like setMinDonutSizeinmm. Its guts should implement a basic sanity check on the minimum size value that is passed into the "setter" method (such as to make sure the size is positive). Any values that pass the test should be stored in the private member variable. We also want to allow the user of our Creature class to check any given Creature object's minimum SizedDonut size in mm. This should occur in a method called something like getMinDonutSize. |
program specification 2 |
Test your new structures inside class Creature by adjusting your code in CreatureLand's main method. Now our SizedDonut's member variable called sizeInmm matters to our Creature objects who consume them. This means the SizedDonut needs a value stored in its member variable before it should be considered for dinner by a Creature. To make this process work, we need to set each instance of Creature to have a value for its member variable which stores this. Since your new member variable is private, you'll set an instance's value in main using the public method. |
Sample output
The following output was produced with code that meets the above specifications. Note that the CreatureLand class contains methods which print out the values of the member variables of their respective object types. This was specified in Exercise 2's mini project.
Analyze this output closely: note that it tests the logic specified: our Creature only takes a bite of the SizedDonut object named Rolando because its size was above our Creature object named Pablo's minimum preferred size.

Submitting your project
Upload all three files individually to your GitHub account. This can be done a few ways: the best is to "click and drag" your entire package directory that NetBeans used to store your .java files. Then a new directory with all your .java files will be created in GitHub and your code repo will stay organized.
If you have a dedicated directory for this module's code, you can open that directory in your own repo online, grab the URL to that page, and paste that into your tracking spreadsheet entry.
arrow_upward back up to contents
motorcycleExperimental Activity: Translating events in CreatureLand into Java!
Summon your creativity to try translating some of the following scenes into Java. You'll want to look at the variable "flags" that are in each photograph of a Donut object and adjust your member variables appropriately. You can write code in DonutLand or CreatureLand that does something with the member variables you'll need to create on your Donut objects.
This is a creative activity--not one that has specification. I'm piloting this for Fall 2018. If you think there is some hope to this idea, please let me know!
Scene T: Turtle Eats a Donut

Scene P: Penguin Eats a Donut

Scene O: Owl Investigates and eats a Donut


Scene R: RoboDog eats a Donut

arrow_upward back up to contents
Page created in April 2018 and last updated on 3 May 2018 and can be freely reproduced according to the site's content use agreement.