Wombat Classes Basics (Young Developers Series, Part 2)

   
Updated 2010  

Learn where Java code is written and saved, how classes relate to one another, and how to use the Greenfoot code editor.

In the Wombat Object Basics article, you learned what an object is, what methods are for, and a bit about the syntax that is used in code. In this article, you will learn where that code is written and saved, how classes relate to one another, and you'll learn to use the Greenfoot code editor. In addition, certain words in this article are linked, so you can learn more than is being taught here.

As in the last article, to follow along you will need:

  • The Java SE software installed on your computer
  • Greenfoot installed on your computer

This article is aimed at anyone interested in Java programming who is between the ages of 10-100 and has no programming experience. It is recommended that you have read and followed Wombat Object Basics before moving on in this article.

Where Is the Code?

Now that you understand that Java programs are made up of a lot of Java objects, and they interact through methods that provide the instructions for doing things, you are ready to learn about the code. Let's not waste any time.

Open Greenfoot. If the Wombats scenario is not already open, Click Scenario in the top menu, choose Open, select wombats, and then click Open.

At the top of the world area, right-click wombatWorld and select void populate(). By invoking that void populate() method, you should now see a few wombats and lots of leaves in your wombat world as shown in Figure 1.

Figure 1. Wombat World

So, where are these directions coming from for the void populate() method? In Java programs, all definitions of objects and all instructions for methods are written into files called classes. A class is a text file, saved with a .java extension. In that file, the code is written, and later compiled.

Take a look at the Wombat class by opening the Greenfoot editor. Right-click the Wombat name and select Open editor, as shown in Figure 2.

Figure 2. Open Editor Command

Now, you should see the Wombat class and all of its code in the editor, as shown in Figure 3.

Figure 3. Greenfoot Code Editor With Wombat Class Displayed

Don't let all these lines of code make you nervous. Scroll up and down and take a look at the code. Something you'll notice right off is that there are a lot of English words. There is also some punctuation, just like in the sentences you're used to reading. But Java syntax is a bit different than what you use in writing English. There are enough similarities, though, to make learning Java programming much easier.

Java Class Details

We're not going to cover all of this code, but you'll learn some important basics here. A Java class defines an object. For this program, the developer decided to create Wombat objects, and he wrote a class that defines what a wombat is and what it can do. In other words, a class is like a recipe for a pie. In a recipe, you list all of the ingredients that need to go into the pie, and you detail the instructions for what to do with the ingredients. A class does the same thing, but it's written a bit differently.

Objects and Inheritance

Scroll down from the top a bit, to this line of code:

public class Wombat extends Actor

This is a class declaration. The declaration declares, or simply says, this class is going to create an object named Wombat. The extends Actor part of the code tells us that there is a class named Actor that this Wombat class inherits from. Starting to sound complicated? It's not. In fact, inheritance simplifies code so you can reuse classes. Inheritance in Java programs works very much like inheritance works for people.

In the real world, you inherit a lot from your parents. You have a body with two legs, two arms, two eyes, and so on. So, in a Java program, let's say there is a Person class. This class declares a Person as an object that has a human-shaped body, two legs, two arms, and so forth, and it has the methods eat(), walk(), sleep(). Of course, there would be many more methods, but you get the idea.

Figure 4. The Jane class extends Person

Let's say you want to create a Jane object. Jane is also a person, so if you extend the Jane class from the Person class, then Jane has all those methods in the Person class available to her. In addition, you can give Jane methods and traits that are individual to her, and are not necessarily common to all people, as shown in Figure 4. Extending prevents you from having to start from scratch and rewrite these methods for any Person objects you create while allowing you to add unique features to each object.

So, let's get back to Wombat objects. Each Wombat extends the Actor class, which means every Wombat automatically can use anything defined in the Actor class. In real life, people understand you better if they understand a bit about your parents, and a bit about human beings in general. Likewise, we need to understand the Actor class to better understand Wombat objects.

Java Documentation, or Application Programming Interface ( API)

To learn about the Actor class, you also get to learn about Java documentation, or API. Close the Greenfoot editor you opened, and then open a new editor by right-clicking the Actor under Actor classes. In the editor you'll see some wonderful information about the Actor class, and what you get by extending the Wombat class from it.

The important thing in the description to note is that objects such as Wombat need to inherit from the Actor class, and that one of the most important methods of this class is the act() method. The act() method is invoked by the user clicking the Act or Run button in Greenfoot, so you need to inherit that method into the Wombat object, just as you needed to inherit the eat() method from the Person class.

Scroll down through this documentation to see what other methods are available. Don't be intimidated by this documentation. You'll get very familiar with it as you learn more and more about Java programming.

Making Wombats Move

Back to the Wombat object. Open an editor for the Wombat class again by right-clicking Wombat and selecting Open editor. Scroll down to the act() method, as shown here:

 public void act()
    {
        if(foundLeaf()) {
            eatLeaf();
        }
        else if(canMove()) {
            move();
        }
        else {
            turnLeft();
        }
    }
 

As you recall, the act() method is from the Actor class. But the details of what happens when the act() method is invoked weren't specifically defined in the Actor class. That is so each object that extends Actor can have individual behavior. For instance, you could create an object named Monsters, and give that object different instructions in the act() method than the Wombat object uses. Additionally, you needn't use the methods from the Actor class.

If you look at the code for the Leaf class in the editor, you'll see that no methods are defined. That's because the Leaf objects don't do anything but sit there until they get eaten. They don't have any methods!

So, what are the details of this act() method in the Wombat class? Look again at that method detailed above. It's easy enough to read: If a Wombat finds a leaf, it eats the leaf, or it simply moves along. If it can't move any farther, it will turn left. How does it do all that?

Scroll down some more in the Wombat class, and you'll see the instructions for a few more methods: foundLeaf(), eatLeaf(), move(), canMove(), turnLeft(), setDirection, gentleness(). We won't get into the details of these methods yet, except to say these instructions are what tell the Wombat how to interact in the program. If you want to change the behavior of a Wombat, you'd change one of these methods. You'll do that later . . . .

More on Java Programming Syntax

Syntax refers to the rules we use to decide what order to put things in, so that they make sense. For example, we have rules in the English language about the order of words in sentences, so that the words make sense together. As mentioned earlier, Java programming syntax is a bit different from English sentences syntax. A few things worth noticing:

  • Lines of code are organized into expressions, statements, and blocks . You'll need to learn about these, so click on the links when you have time.
  • Each expression or statement ends with a semicolon ;
  • Blocks are enclosed in curly braces {}
  • Commas are used in much the same way that they are used in English sentences
  • Comments about the code start with double slashes //
Just for Fun

For the fun of it, change the behavior of the wombats. If your code editor is not already open, open it by either double-clicking Wombat under Actor classes, or right-clicking Wombat and selecting Open editor. Scroll down to the act() method and replace the current method with this new act() method:

     public void act()
    {
        if(foundLeaf()) {
            eatLeaf();
        }
        else if(canMove()) {
            move();
        }
        else {
            turnRandom();
        }
    }
 

Notice the change in the act() method: Now the method calls turnRamdom() instead of turnLeft. Naturally, you need to add this new turnRandom() method after the one above:

  /**
     * Turn in a random direction.
     */
    public void turnRandom()
    {
        // get a random number between 0 and 3...

        int turns = Greenfoot.getRandomNumber(4);
       // ...an turn left that many times.
        for(int i=0; i <turns; i++) {
            turnLeft();
        }
    }
 

Click Compile at the top of the editor. If no errors occur, go back to your main screen, click Compile All, populate the world again, and then click Run. Now the Wombats move in other directions besides left to go after the leaves. If you had errors, you can use the code for the entire class with these changes made by clicking here.

What is happening when you compile? In the Java programming language, all source code is first written in plain class text files that end with the .java extension. Those source files are then compiled into .class files by the javac compiler. A .class file does not contain code that is native to your processor. Instead it contains bytecodes - the machine language of the Java Virtual Machine (Java VM). You'll learn more about this when you start writing your own Java programs.

Summary

You learned a lot in this article! Now you should understand the following concepts:

  • The details for objects are written into files called classes. One program may have many classes because it may have many objects.
  • Each class defines the object and its behavior.
  • Methods detail the behavior and provide the instructions for what that object can do and how it can interact with other objects.
  • Some objects inherit behavior and traits, just like people do, by extending to a parent class, such as Wombat extends Actor
  • You change the behavior of an object by changing the instructions in the method or adding new methods.
  • Java programming code uses special syntax and is written in expressions, statements, and blocks.
  • Java is a compiled programming language.

In the next article, you'll learn about Java packages and import statements, variables and fields, and more.

For More Information

With everything you have learned in these two articles, you should be ready to begin exploring the Java Tutorial and learning more about the Java programming language. Its beginning lessons will make sense to you now that you have the Wombat basics to put it into context.


Young Developer Series: