Learn about Java packages, variables, and ways that objects interact with each other through methods. In the articles Wombat Object Basics and Wombat Classes Basics, you learned about objects, classes, methods, and language syntax. Now, in Part 3 of the Young Developer series, you'll learn what is involved in writing a Java application that relies on other Java classes (or Java API), what variables are, how to use variables, and how objects interact through methods. As in the last article, to follow along, you will need these installed on your computer: This article is aimed at anyone interested in Java programming who is from the age of 10 to 100 and who has no programming experience. We recommend that you have read and followed the instructions in Wombat Object Basics and Wombat Classes Basics before you move on to this article. Looking at Packages and Variables in Ants
For this tutorial, open the Ants scenario by going to the Scenario menu, and selecting Open. The scenarios folder should automatically be visible. Choose ants, and then click Open as shown in Figure 1.
A new window opens and displays the Ants scenario. If you do not see the sand-colored world on the screen, click the Compile All button at the bottom of the scenario main screen. Click Run and watch as ants leave their anthills and search for the piles of food. As they discover food and bring it back to the anthills, they create scented paths, or rather pheromone paths, that become more and more visible as more ants find their way to the food and back to the anthill. As with the Wombat scenario, you can place more ants or anthills on the scene. When you click Run on the ants scenario, you should see something similar to Figure 2.
Import Statements and Packages
Right-click the Ant object under Actor classes, and select Open Editor. Notice the first lines of code:
These are called import statements. A Java application uses import statements so that it can use other ready-made classes from the Java API, as well as other developer-created classes, or classes you create. The first statement indicates that this application is in the greenfoot package. Packages are how Java classes are organized. Many packages are available to you, such as classes that handle math problems, that organize data in objects called Collections, and that allow you to iterate, or repeat, a process. In other words, packages bundle classes by function. Fundamental classes are in the The Java API library, which contains many packages for your use, is huge, and you'll want to get familiar with what classes are available. The best way to do this is to take some time to read the API documentation. For this article, look up the Variables and Fields
Next, in the Next in the class, you'll see regular sentences marked by Take a look at the following chunk of code, reading the comments that explain what the code is describing:
All these lines of code make use of variables, and these particular variables define the fields in this
But before understanding a field, you need to know more about variables. So what is a variable? In programming, a variable is assigned a value, which may or may not change. These are called variables because the values they represent usually do change. In the above code, the variable These particular variables define the state of an object. This is similar to real-world objects, where a ball may have the state of being red or perhaps the state of being 3 feet in diameter. You might assign the variable Pheromones are a scent that ants use to find food and to let other ants know where they can collect the food. In this application, the The variables are often initially set to zero, or left blank, and then those definitions change as the user plays the game. Types of Variables
You may have noticed the keywords Local variables are those that are used only in a method or block of code, as mentioned earlier in this article. Access modifiers declare a variable either Ant Objects Interact With Food Objects Through Methods
Now that the variables have been declared, they can be used in methods so that this scenario does something! Scroll down in the The point of the ant scenario is to collect food and return it to the anthill. Note the following
In this method, a variable food of type
Let's take a look at the The food doesn't actually do anything but sit there, so this class just defines what
Notice that these fields establish the size and color of the food. Color is created through the Scroll down in the The Summary
In this article, you learned how packages organize classes and how to include import statement at the top of a class. You also learned about variables and their types, and how objects interact with each other through methods. Open some of the other scenarios in Greenfoot, and take a look at the class code for each scenario object. See what variables are defined, what the methods do to interact with other methods, and what else you can learn just by looking at the code. Experiment and change some of the methods to see if you can change the behavior of objects. You can also look on the Internet for more scenarios for Greenfoot, some of which are quite complex and sophisticated, and download them. Most of all, have fun! For More Information
| ||||||||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||